-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
100 lines (83 loc) · 2.25 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"time"
"github.com/adnaan/gomodest-starter/app"
"github.com/go-chi/chi"
"github.com/go-chi/valve"
)
// fileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func fileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}
func main() {
// Our graceful valve shut-off package to manage code preemption and
// shutdown signaling.
vv := valve.New()
baseCtx := vv.Context()
configFile := flag.String("config", "", "path to config file")
envPrefix := os.Getenv("ENV_PREFIX")
if envPrefix == "" {
envPrefix = "app"
}
flag.Parse()
cfg, err := app.LoadConfig(*configFile, envPrefix)
if err != nil {
log.Fatal(err)
}
r := app.Router(baseCtx, cfg)
workDir, _ := os.Getwd()
public := http.Dir(filepath.Join(workDir, "./", "public", "assets"))
fileServer(r, "/static", public)
srv := &http.Server{
ReadTimeout: time.Duration(cfg.ReadTimeoutSecs) * time.Second,
WriteTimeout: time.Duration(cfg.WriteTimeoutSecs) * time.Second,
Addr: fmt.Sprintf(":%d", cfg.Port),
Handler: r,
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// sig is a ^C, handle it
fmt.Println("shutting down..")
// first valv
vv.Shutdown(20 * time.Second)
// create context with timeout
ctx, cancel := context.WithTimeout(baseCtx, 20*time.Second)
defer cancel()
// start http shutdown
srv.Shutdown(ctx)
// verify, in worst case call cancel via defer
select {
case <-time.After(21 * time.Second):
fmt.Println("not all connections done")
case <-ctx.Done():
}
}
}()
log.Println("http server is listening...")
srv.ListenAndServe()
}