-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdfx_test.go
341 lines (287 loc) · 8.05 KB
/
stdfx_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Copyright 2024 Christoph Hoopmann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stdfx_test
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/choopm/stdfx"
"github.com/choopm/stdfx/configfx"
"github.com/choopm/stdfx/globals"
"github.com/choopm/stdfx/loggingfx"
"github.com/choopm/stdfx/loggingfx/zerologfx"
"github.com/go-viper/mapstructure/v2"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx"
"golang.org/x/sync/errgroup"
)
// version is provided by `-ldflags "-X main.version=1.0.0"`
var version string = "unknown"
var configContent = `
log:
format: color
level: info
output: stdout
timeFormat: "2006-01-02T15:04:05Z07:00"
webserver:
host: 0.0.0.0
port: 8080
routes:
- path: /
content: hello world
- path: /example
content: example from tests
`
// TestExampleWebserver tests the same things as examples/webserver
func TestExampleWebserver(t *testing.T) {
d, _ := t.Deadline()
ctx, cancel := context.WithDeadline(context.Background(), d)
defer cancel()
// create config file
tempDir, err := os.MkdirTemp(os.TempDir(), "go-test")
require.Nil(t, err)
configFile := filepath.Join(tempDir, "server.yaml")
require.Nil(t, os.WriteFile(configFile, []byte(configContent), 0644))
defer os.RemoveAll(tempDir)
// update os.Args as if the user started us using arguments
globals.RootFlagConfigPathDefault = tempDir
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{os.Args[0], "-c", tempDir, "server"}
// build app
app := fx.New(
// logging
zerologfx.Module,
fx.WithLogger(zerologfx.ToFx),
fx.Decorate(zerologfx.Decorator[Config]),
// viper configuration
fx.Provide(stdfx.ConfigFile[Config]("server")),
// cobra commands
fx.Provide(
stdfx.AutoRegister(stdfx.VersionCommand(version)),
stdfx.AutoRegister(stdfx.ConfigCommand[Config]),
stdfx.AutoRegister(serverCommand),
stdfx.AutoCommand, // add registered commands to root
),
// app start
fx.Invoke(stdfx.Unprivileged), // abort when being run as root
fx.Invoke(stdfx.Commander), // run root cobra command
)
// start the app
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return app.Start(ctx)
})
time.Sleep(time.Second * 3)
// test http requests
client := http.DefaultClient
req, err := http.NewRequest("GET", "http://localhost:8080/example", nil)
require.Nil(t, err)
res, err := client.Do(req)
require.Nil(t, err)
assert.Equal(t, 200, res.StatusCode)
body, err := io.ReadAll(res.Body)
require.Nil(t, err)
assert.Contains(t, string(body), "example from tests")
// stop the app
cancel()
// we should not see any errors
assert.Nil(t, g.Wait())
}
// serverCommand returns a *cobra.Command to start the server from a ConfigProvider
func serverCommand(
configProvider configfx.Provider[Config],
logger *zerolog.Logger,
) *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "server starts the server",
RunE: func(cmd *cobra.Command, args []string) error {
// fetch the config
cfg, err := configProvider.Config()
if err != nil {
return err
}
// create server instance
server, err := NewServer(cfg, logger)
if err != nil {
return err
}
// start server using context
return server.Start(cmd.Context())
},
}
return cmd
}
// Config struct stores all config data.
type Config struct {
Logging loggingfx.Config `mapstructure:"log"`
// Webserver defines the http server config
Webserver WebserverConfig `mapstructure:"webserver"`
// Routes defines the webserver routes
Routes []*Route `mapstructure:"routes" default:"[]"`
}
// Validate validates the Config
func (c *Config) Validate() error {
if err := c.Webserver.Validate(); err != nil {
return err
}
for i, route := range c.Routes {
if err := route.Validate(); err != nil {
return fmt.Errorf("route %d (%s): %s", i, route.Path, err)
}
}
return nil
}
// WebserverConfig holds the webserver config
type WebserverConfig struct {
// Host is the listening host to use when starting a server
Host string `mapstructure:"host" default:"0.0.0.0"`
// Port is the listening port to use when starting a server
Port int `mapstructure:"port" default:"8080"`
}
// Validate validates the HTTPConfig
func (c *WebserverConfig) Validate() error {
if len(c.Host) == 0 {
return fmt.Errorf("missing webserver.host")
}
if c.Port == 0 {
return fmt.Errorf("missing webserver.port")
}
return nil
}
// Route maps paths to content
type Route struct {
// Path is the webserver path to register
Path string `mapstructure:"path"`
// Content is the content to deliver on this path
Content any `mapstructure:"content"`
}
// Validate validates the config
func (c *Route) Validate() error {
if len(c.Path) == 0 {
return fmt.Errorf("missing path")
}
if c.Content == nil {
return fmt.Errorf("missing content")
}
return nil
}
// DecodeHook returns the composite decoding hook for decoding Config
func (c *Config) DecodeHook() mapstructure.DecodeHookFunc {
return mapstructure.ComposeDecodeHookFunc(
// knx group addresses listed as an example
// knxGroupAddressDecoder(),
)
}
// // knxGroupAddressDecoder returns a decoder for knx group addresses.
// // It parses strings of "1/2/3" into cemi.GroupAddr.
// func knxGroupAddressDecoder() mapstructure.DecodeHookFunc {
// // groupAddressDecoder returns a DecodeHookFunc that converts
// // string to cemi.GroupAddress or error.
// return func(
// f reflect.Type,
// t reflect.Type,
// data interface{},
// ) (interface{}, error) {
// if f.Kind() != reflect.String {
// return data, nil
// }
// if t != reflect.TypeOf(cemi.GroupAddr(0)) {
// return data, nil
// }
// // Convert it by parsing
// return cemi.NewGroupAddrString(data.(string))
// }
// }
// LoggingConfig returns the loggingfx.Config.
// This implements an interface to support log decorators.
func (c *Config) LoggingConfig() loggingfx.Config {
return c.Logging
}
// Server state struct
type Server struct {
config *Config
log *zerolog.Logger
}
// NewServer creates a new *Server instance using a provided config
func NewServer(config *Config, logger *zerolog.Logger) (*Server, error) {
// validate config
if config == nil {
return nil, errors.New("missing config")
}
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("config: %s", err)
}
// init logger if missing
if logger == nil {
l := zerolog.Nop()
logger = &l
}
s := &Server{
config: config,
log: logger,
}
return s, nil
}
// Start starts the server using ctx
func (s *Server) Start(ctx context.Context) error {
s.log.Trace().
Interface("config", s.config).
Msg("initializing server")
// register routes
for _, route := range s.config.Routes {
http.HandleFunc(route.Path, func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, route.Content)
})
}
s.log.Trace().
Msg("starting server")
g, ctx := errgroup.WithContext(ctx)
// build and start webserver
addr := net.JoinHostPort(s.config.Webserver.Host,
strconv.Itoa(s.config.Webserver.Port),
)
server := &http.Server{Addr: addr, Handler: nil}
// shutdown hook, registered before starting
context.AfterFunc(ctx, func() {
_ = server.Close()
})
g.Go(func() error {
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
})
// wait for started tasks
s.log.Info().
Str("addr", addr).
Msg("server is running")
if err := g.Wait(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
s.log.Info().Msg("server stopped")
return nil
}