-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (61 loc) · 1.72 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"net/http"
"strings"
"github.com/chrmang-jambit/demo-container-go/pkg/api"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/spf13/viper"
)
//go:generate oapi-codegen --config oapi-config.yaml api/openapi.yaml
const (
cfg_serverip = "SERVER_IP"
cfg_serverport = "SERVER_PORT"
path_health = "/health"
path_ready = "/ready"
)
type config struct {
serverip string
serverport int
}
func main() {
maincfg := readConfig()
mainServer := echo.New()
mainServer.HideBanner = true
corrcfg := middleware.RequestIDConfig{}
mainServer.Use(middleware.RequestIDWithConfig(corrcfg))
logcfg := middleware.LoggerConfig{
Skipper: urlSkipper,
}
mainServer.Use(middleware.LoggerWithConfig(logcfg))
mainServer.Logger.SetLevel(log.DEBUG)
mainServerUri := fmt.Sprintf("%s:%d", maincfg.serverip, maincfg.serverport)
mainServer.GET(path_health, healthcheck)
mainServer.GET(path_ready, readycheck)
server := api.New()
api.RegisterHandlers(mainServer, server)
mainServer.Logger.Fatal(mainServer.Start(mainServerUri))
}
func healthcheck(ctx echo.Context) error {
ctx.Response().Status = http.StatusOK
return nil
}
func readycheck(ctx echo.Context) error {
ctx.Response().Status = http.StatusOK
return nil
}
func readConfig() config {
viper.SetDefault(cfg_serverip, "localhost")
viper.SetDefault(cfg_serverport, 8080)
viper.AutomaticEnv()
return config{
serverip: viper.GetString(cfg_serverip),
serverport: viper.GetInt(cfg_serverport),
}
}
func urlSkipper(c echo.Context) bool {
// ignore health endpoint for logging and metrics
return strings.HasPrefix(c.Path(), path_health) || strings.HasPrefix(c.Path(), path_ready)
}