-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
62 lines (48 loc) · 1.29 KB
/
main.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
package main
import (
"flag"
"github.com/labstack/echo/middleware"
"net"
"net/http"
"os"
"github.com/labstack/echo"
"github.com/sirupsen/logrus"
)
var logger = logrus.New()
func main() {
var socketPath string
flag.StringVar(&socketPath, "socket", "/run/guest-services/backend.sock", "Unix domain socket to listen on")
flag.Parse()
_ = os.RemoveAll(socketPath)
logger.SetOutput(os.Stdout)
logMiddleware := middleware.LoggerWithConfig(middleware.LoggerConfig{
Skipper: middleware.DefaultSkipper,
Format: `{"time":"${time_rfc3339_nano}","id":"${id}",` +
`"method":"${method}","uri":"${uri}",` +
`"status":${status},"error":"${error}"` +
`}` + "\n",
CustomTimeFormat: "2006-01-02 15:04:05.00000",
Output: logger.Writer(),
})
logger.Infof("Starting listening on %s\n", socketPath)
router := echo.New()
router.HideBanner = true
router.Use(logMiddleware)
startURL := ""
ln, err := listen(socketPath)
if err != nil {
logger.Fatal(err)
}
router.Listener = ln
router.GET("/hello", hello)
logger.Fatal(router.Start(startURL))
}
func listen(path string) (net.Listener, error) {
return net.Listen("unix", path)
}
func hello(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, HTTPMessageBody{Message: "hello"})
}
type HTTPMessageBody struct {
Message string
}