-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
server.go
65 lines (55 loc) · 1.96 KB
/
server.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package routes
import (
"fmt"
"log"
"os"
oapimiddleware "github.com/deepmap/oapi-codegen/pkg/middleware"
"github.com/labstack/echo/v4"
middleware "github.com/labstack/echo/v4/middleware"
)
type Logger interface {
Infof(template string, args ...interface{})
Debugf(template string, args ...interface{})
Warnf(template string, args ...interface{})
Errorf(template string, args ...interface{})
Fatalf(template string, args ...interface{})
}
// Start web server on the main thread. It exits the application if it fails setting up.
func StartWebServer(port string, routesImplementation StrictServerInterface, logger Logger) error {
e := echo.New()
baseURL := "/api/v1"
handler := NewStrictHandler(routesImplementation, nil)
RegisterHandlersWithBaseURL(e, handler, baseURL)
// Request validator
swagger, err := GetSwagger()
if err != nil {
log.Fatalf("Error loading swagger spec\n: %s", err)
os.Exit(1)
}
swagger.Servers = nil
e.Group(baseURL).Use(oapimiddleware.OapiRequestValidator(swagger))
e.Use(middleware.CORS())
e.Use(middleware.RequestID())
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
Skipper: func(c echo.Context) bool {
return c.Path() == "/api/v1/healthz" || c.Path() == "/api/v1/readyz"
},
LogRequestID: true, LogMethod: true, LogURI: true, LogStatus: true, LogLatency: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
if v.Status < 400 {
logger.Infof("%d %s %s %s [%s]", v.Status, v.Method, v.URI, v.Latency.String(), v.RequestID)
} else if v.Status >= 400 && v.Status < 500 {
logger.Warnf("%d %s %s %s [%s]", v.Status, v.Method, v.URI, v.Latency.String(), v.RequestID)
} else {
logger.Errorf("%d %s %s %s [%s]", v.Status, v.Method, v.URI, v.Latency.String(), v.RequestID)
}
return nil
},
}))
// Start REST API server
return e.Start(fmt.Sprintf("0.0.0.0:%s", port))
}