Repository navigation
Expand file tree
/
Copy pathAPI.go
More file actions
86 lines (72 loc) · 1.81 KB
/
Copy pathAPI.go
File metadata and controls
86 lines (72 loc) · 1.81 KB
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
package main
import (
"fmt"
"os"
"runtime"
"github.com/Sirupsen/logrus"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
// API : implements a REST API
type API struct {
host string
port uint
authEnabled bool
authKey string
httpsEnabled bool
httpsPrivKey string
httpsPubKey string
logping bool
accesslog bool
q chan interface{}
middleware LogMiddleware
mainlogger *logrus.Logger
config Config
}
func (a *API) handle() {
}
func (a *API) start() {
fmt.Printf("%+v\n", a)
e := echo.New()
e.Logger = LogMiddleware{r: a.mainlogger}
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
if a.authEnabled {
return key == a.authKey, nil
}
return true, nil
}))
e.Use(a.middleware.middlewareHook())
e.HideBanner = true
e.HidePort = true
e.GET("/ping", func(c echo.Context) error {
p := &Ping{Ping: "ok"}
return c.JSON(200, p)
})
e.GET("/config", func(c echo.Context) error {
return c.JSON(200, a.config)
})
e.GET("/config/api", func(c echo.Context) error {
return c.JSON(200, a.config.API)
})
e.GET("/config/daemons", func(c echo.Context) error {
return c.JSON(200, a.config.Daemons)
})
e.GET("/config/pidzero", func(c echo.Context) error {
return c.JSON(200, a.config.Pidzero)
})
e.GET("/env", func(c echo.Context) error {
return c.JSON(200, os.Environ())
})
e.GET("/stats", func(c echo.Context) error {
m := runtime.MemStats{}
runtime.ReadMemStats(&m)
return c.JSON(200, m)
})
hostport := fmt.Sprintf("%s:%v", a.config.API.Host, a.config.API.Port)
a.mainlogger.Debug(fmt.Sprintf("Starting API on %s", hostport))
if a.config.API.HTTPS.Enabled == true {
a.mainlogger.Fatal(e.StartTLS(hostport, a.httpsPubKey, a.httpsPrivKey))
} else {
a.mainlogger.Fatal(e.Start(hostport))
}
}