/
info.go
89 lines (70 loc) · 2.06 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package api
import (
"net/http"
"runtime"
"time"
"github.com/labstack/echo"
)
// ApplicationInfo contains info about the app
type ApplicationInfo struct {
Version string
GOVersion string
BuildDate string
StartTime time.Time
}
// MemoryInfo some memory stats
type MemoryInfo struct {
// Bytes of allocated heap objects.
Alloc uint64 `json:"allocated"`
// Cumulative bytes allocated for heap objects.
TotalAlloc uint64 `json:"totalAllocated"`
// The total bytes of memory obtained from the OS.
System uint64 `json:"system"`
// The number of pointer lookups performed by the runtime.
Lookups uint64 `json:"lookups"`
// The cumulative count of heap objects allocated.
// The number of live objects is Mallocs - Frees.
Mallocs uint64 `json:"mallocs"`
// The cumulative count of heap objects freed.
Frees uint64 `json:"frees"`
// The number of completed GC cycles.
NumGC uint32 `json:"numGC"`
}
// InfoResponse is the info response
type InfoResponse struct {
// Version of the application
Version string `json:"version"`
// Go runtime version
RuntimeVersion string `json:"runtimeVersion"`
// The build date of the server application
BuildDate string `json:"buildDate"`
// Uptime of the server
Uptime string `json:"uptime"`
// Memory info
MemoryInfo *MemoryInfo `json:"memoryInfo,omitempty"`
}
// The InfoAPI provides handlers for managing projects.
type InfoAPI struct {
Info ApplicationInfo
}
// GetApplicationInfo gets application info
func (api *InfoAPI) GetApplicationInfo(ctx echo.Context) error {
memStats := &runtime.MemStats{}
runtime.ReadMemStats(memStats)
ir := InfoResponse{
Version: api.Info.Version,
RuntimeVersion: api.Info.GOVersion,
BuildDate: api.Info.BuildDate,
Uptime: time.Since(api.Info.StartTime).String(),
MemoryInfo: &MemoryInfo{
Alloc: memStats.Alloc,
TotalAlloc: memStats.TotalAlloc,
System: memStats.Sys,
Lookups: memStats.Lookups,
Mallocs: memStats.Mallocs,
Frees: memStats.Frees,
NumGC: memStats.NumGC,
},
}
return ctx.JSON(http.StatusOK, ir)
}