-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
73 lines (61 loc) · 1.53 KB
/
Copy pathmain.go
File metadata and controls
73 lines (61 loc) · 1.53 KB
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
package main
import (
"context"
"net/http"
"time"
"github.com/mateconpizza/goairdrop/internal/application"
"github.com/mateconpizza/goairdrop/internal/cli"
"github.com/mateconpizza/goairdrop/internal/server"
"github.com/mateconpizza/goairdrop/internal/server/cleanup"
"github.com/mateconpizza/goairdrop/internal/server/middleware"
"github.com/mateconpizza/goairdrop/internal/webui"
)
const (
appName = "goaird"
version = "0.1.2"
repo = "https://github.com/mateconpizza/goairdrop"
)
func main() {
app := application.New(appName, version, repo)
if err := app.Init(); err != nil {
cli.ErrAndExit(app.Name, err)
}
if err := run(app); err != nil {
cli.ErrAndExit(app.Name, err)
}
}
func run(app *application.App) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
router := http.NewServeMux()
router, err := app.Routes(router)
if err != nil {
return err
}
if app.Flag.Webui {
ui, err := webui.New(app)
if err != nil {
return err
}
ui.Routes(router)
}
srv := server.New(
server.WithRouter(router),
server.WithAddr(app.Cfg.Server.Addr),
server.WithLogger(app.Logger),
server.WithMiddleware([]server.Middleware{
middleware.Logging,
middleware.PanicRecover,
}...),
)
cleanup.Register(
func() error {
app.Logger.Info("shutting down HTTP server")
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
return srv.Shutdown(shutdownCtx)
},
)
cleanup.Listen(ctx, cancel, app.Logger)
return srv.Start()
}