generated from ZEISS/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (66 loc) · 1.38 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
79
80
81
82
83
84
package main
import (
"context"
"log"
"os"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/spf13/cobra"
reload "github.com/zeiss/fiber-reload"
"github.com/zeiss/pkg/server"
)
// Config ...
type Config struct {
Flags *Flags
}
// Flags ...
type Flags struct {
Addr string
}
var cfg = &Config{
Flags: &Flags{},
}
func init() {
rootCmd.PersistentFlags().StringVar(&cfg.Flags.Addr, "addr", ":3000", "addr")
rootCmd.SilenceUsage = true
}
var rootCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return run(cmd.Context())
},
}
type webSrv struct{}
func (w *webSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error {
return func() error {
app := fiber.New()
app.Use(requestid.New())
app.Use(logger.New())
app.Use(recover.New())
reload.WithHotReload(app)
app.Static("/", ".")
err := app.Listen(cfg.Flags.Addr)
if err != nil {
log.Fatal(err)
}
return nil
}
}
func run(ctx context.Context) error {
log.SetFlags(0)
log.SetOutput(os.Stderr)
webSrv := &webSrv{}
s, _ := server.WithContext(ctx)
s.Listen(webSrv, true)
err := s.Wait()
if err != nil {
return err
}
return nil
}
func main() {
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}