This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
72 lines (60 loc) · 1.56 KB
/
client.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
package main
import (
"flag"
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/labstack/gommon/random"
)
var port int
var config string
func init() {
flag.StringVar(&config, "config", "/etc/combaine/client-config.yaml", "Client configuration")
}
func main() {
flag.Parse()
e := echo.New()
e.Logger.SetLevel(log.DEBUG)
cl, err := newConfigLoader(config, e.Logger)
if err != nil {
e.Logger.Fatal(err)
}
e.Use(middleware.RequestID())
e.Use(middleware.Logger())
if cl.settings.Gzip {
e.Logger.Info("Use gzip middleware")
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
}
e.GET("/ping", func(c echo.Context) error {
return c.String(http.StatusOK, "/ping ok")
})
e.GET("/exec", func(c echo.Context) error {
return c.String(http.StatusOK, cl.tasksList())
})
e.GET("/exec/:task", func(c echo.Context) error {
taskName := c.Param("task")
rid := setRequestID(c)
task, exists := cl.lookupTask(taskName)
if !exists {
return c.String(http.StatusNotFound, "Task "+taskName+" Not Found")
}
output, err := task.getOutput(rid, c)
if err != nil {
return c.String(http.StatusInternalServerError, "")
}
return c.String(http.StatusOK, output)
})
e.Logger.Fatal(e.Start(":" + strconv.Itoa(cl.settings.Port)))
}
func setRequestID(c echo.Context) string {
req := c.Request()
res := c.Response()
rid := req.Header.Get(echo.HeaderXRequestID)
if rid == "" {
rid = random.String(32)
}
res.Header().Set(echo.HeaderXRequestID, rid)
return rid
}