-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
54 lines (44 loc) · 1.51 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
package main
// This is an example program on how to use the app package. It uses to FileServer
// handler to serve all files on a given directory.
//
// To run this program in the commandline you could use:
// go run ./app/examples/servefiles/ -log-human -log-level=debug
import (
"context"
"net/http"
"github.com/arquivei/foundationkit/app"
"github.com/arquivei/foundationkit/log"
)
var version = "development" // Should be replaced on compilation phase
var config struct {
Log log.Config
HTTP struct {
Port string `default:"8000"`
}
Dir string `default:"."`
}
func main() {
app.SetupConfig(&config)
ctx := log.SetupLoggerWithContext(context.Background(), config.Log, version)
// New app
app.NewDefaultApp(ctx)
// Some inicialization, could take a while.
// It's a good practice to initialize everything before calling RunAndWait because
// readiness probe is already up and reporting the app is not ready yet.
httpServer := &http.Server{Addr: ":" + config.HTTP.Port, Handler: http.FileServer(http.Dir(config.Dir))}
// You can register the shutdown handlers at any order, but do it before starting the app
app.RegisterShutdownHandler(
&app.ShutdownHandler{
Name: "http_server",
Priority: app.ShutdownPriority(100),
Handler: httpServer.Shutdown,
Policy: app.ErrorPolicyAbort,
},
)
// Run the main loop until it finishes or receives termination signal
// On this point the readiness probe starts returning success.
app.RunAndWait(func() error {
return httpServer.ListenAndServe()
})
}