-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexample_test.go
197 lines (162 loc) · 5.32 KB
/
example_test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package httpserver_test
import (
"fmt"
golog "log"
"net/http"
"os"
"strings"
"github.com/go-chi/chi/v5"
"github.com/acronis/go-appkit/config"
"github.com/acronis/go-appkit/httpserver"
"github.com/acronis/go-appkit/httpserver/middleware"
"github.com/acronis/go-appkit/log"
"github.com/acronis/go-appkit/profserver"
"github.com/acronis/go-appkit/restapi"
"github.com/acronis/go-appkit/service"
)
/*
Add "// Output:" in the end of Example() function and run:
$ go test ./httpserver -v -run Example
Application and pprof servers will be ready to handle HTTP requests:
$ curl localhost:8888/healthz
{"components":{"component-a":true,"component-b":false}}
$ curl localhost:8888/metrics
# Metrics in Prometheus format
$ curl localhost:8888/api/my-service/v1/hello
{"message":"Hello"}
$ curl 'localhost:8888/api/my-service/v2/hi?name=Alice'
{"message":"Hi Alice"}
*/
func Example() {
if err := runApp(); err != nil {
golog.Fatal(err)
}
}
func runApp() error {
cfg, err := loadAppConfig()
if err != nil {
return fmt.Errorf("load config: %w", err)
}
logger, loggerClose := log.NewLogger(cfg.Log)
defer loggerClose()
var serviceUnits []service.Unit
// Create HTTP server that provides /healthz, /metrics, and /api/{service-name}/v{number}/* endpoints.
httpServer, err := makeHTTPServer(cfg.Server, logger)
if err != nil {
return err
}
serviceUnits = append(serviceUnits, httpServer)
if cfg.ProfServer.Enabled {
// Create HTTP server for profiling (pprof is used under the hood).
serviceUnits = append(serviceUnits, profserver.New(cfg.ProfServer, logger))
}
return service.New(logger, service.NewCompositeUnit(serviceUnits...)).Start()
}
func makeHTTPServer(cfg *httpserver.Config, logger log.FieldLogger) (*httpserver.HTTPServer, error) {
const errorDomain = "MyService" // Error domain is useful for distinguishing errors from different services (e.g. proxies).
apiRoutes := map[httpserver.APIVersion]httpserver.APIRoute{
1: func(router chi.Router) {
router.Get("/hello", v1HelloHandler())
},
2: func(router chi.Router) {
router.Get("/hi", v2HiHandler(errorDomain))
},
}
opts := httpserver.Opts{
ServiceNameInURL: "my-service",
ErrorDomain: errorDomain,
APIRoutes: apiRoutes,
HealthCheck: func() (httpserver.HealthCheckResult, error) {
// 503 status code will be returned if any of the components is unhealthy.
return map[httpserver.HealthCheckComponentName]httpserver.HealthCheckStatus{
"component-a": httpserver.HealthCheckStatusOK,
"component-b": httpserver.HealthCheckStatusOK,
}, nil
},
}
httpServer, err := httpserver.New(cfg, logger, opts)
if err != nil {
return nil, err
}
// Custom routes can be added using chi.Router directly.
httpServer.HTTPRouter.Get("/custom-route", customRouteHandler)
return httpServer, nil
}
func loadAppConfig() (*AppConfig, error) {
// Environment variables may be used to configure the server as well.
// Variable name is built from the service name and path to the configuration parameter separated by underscores.
_ = os.Setenv("MY_SERVICE_SERVER_TIMEOUTS_SHUTDOWN", "10s")
_ = os.Setenv("MY_SERVICE_LOG_LEVEL", "info")
// Configuration may be read from a file or io.Reader. YAML and JSON formats are supported.
cfgReader := strings.NewReader(`
server:
address: ":8888"
timeouts:
write: 1m
read: 15s
readHeader: 10s
idle: 1m
shutdown: 5s
limits:
maxBodySize: 1M
log:
requestStart: true
profServer:
enabled: true
address: ":8889"
log:
level: warn
format: json
output: stdout
`)
cfgLoader := config.NewDefaultLoader("my_service")
cfg := NewAppConfig()
err := cfgLoader.LoadFromReader(cfgReader, config.DataTypeYAML, cfg)
return cfg, err
}
func v1HelloHandler() func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
logger := middleware.GetLoggerFromContext(r.Context())
restapi.RespondJSON(rw, map[string]string{"message": "Hello"}, logger)
}
}
func v2HiHandler(errorDomain string) func(rw http.ResponseWriter, r *http.Request) {
return func(rw http.ResponseWriter, r *http.Request) {
logger := middleware.GetLoggerFromContext(r.Context())
name := r.URL.Query().Get("name")
if len(name) < 3 {
apiErr := restapi.NewError(errorDomain, "invalidName", "Name must be at least 3 characters long")
restapi.RespondError(rw, http.StatusBadRequest, apiErr, middleware.GetLoggerFromContext(r.Context()))
return
}
restapi.RespondJSON(rw, map[string]string{"message": fmt.Sprintf("Hi %s", name)}, logger)
}
}
func customRouteHandler(rw http.ResponseWriter, r *http.Request) {
logger := middleware.GetLoggerFromContext(r.Context())
if _, err := rw.Write([]byte("Content from the custom route")); err != nil {
logger.Error("error while writing response body", log.Error(err))
}
}
type AppConfig struct {
Server *httpserver.Config
ProfServer *profserver.Config
Log *log.Config
}
func NewAppConfig() *AppConfig {
return &AppConfig{
Server: httpserver.NewConfig(),
ProfServer: profserver.NewConfig(),
Log: log.NewConfig(),
}
}
func (c *AppConfig) SetProviderDefaults(dp config.DataProvider) {
config.CallSetProviderDefaultsForFields(c, dp)
}
func (c *AppConfig) Set(dp config.DataProvider) error {
return config.CallSetForFields(c, dp)
}