-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
101 lines (86 loc) · 2.56 KB
/
serve.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
package bingox
import (
"context"
"fmt"
haikunator "github.com/atrox/haikunatorgo/v2"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/opentracing-contrib/go-gorilla/gorilla"
"github.com/opentracing/opentracing-go"
"github.com/shihtzu-systems/bingo/pkg/bingo"
. "github.com/shihtzu-systems/bingo/pkg/bingoctl"
"github.com/shihtzu-systems/bingo/pkg/loggerx"
"github.com/shihtzu-systems/redix"
"go.uber.org/zap"
"net/http"
"os"
"os/signal"
"time"
)
type ServeArgs struct {
Trace bool
Debug bool
SessionSecret []byte
SessionKey string
Redis redix.Redis
Logger loggerx.Factory
Boxes bingo.Boxes
Serial string
}
func Serve(args ServeArgs) {
logx := args.Logger
r := mux.NewRouter()
sessionStore := sessions.NewCookieStore(args.SessionSecret)
// root controller
root := NewRootController(logx, args.Redis, sessionStore, args.SessionKey)
r.HandleFunc(RootPath(), root.HandleRoot)
r.HandleFunc(RootPath("ping"), root.HandlePing)
// board controller
board := NewBoardController(logx, args.Redis, sessionStore, args.SessionKey, args.Boxes)
r.HandleFunc(BoardPath("{id:[a-z0-9-]+}"), board.HandleRoot)
r.HandleFunc(BoardPath("{id:[a-z0-9-]+}", "mark", "{letter:[bingo]}", "{index:[0-4]}"), board.HandleMark)
r.HandleFunc(BoardPath("{id:[a-z0-9-]+}", "check"), board.HandleCheck)
r.HandleFunc(BoardPath("{id:[a-z0-9-]+}", "recycle"), board.HandleRecycle)
// static
r.PathPrefix("/static/").Handler(
http.StripPrefix("/static/",
http.FileServer(http.Dir("./static/"))))
// server startup
namer := haikunator.New()
namer.TokenLength = 0
namer.Delimiter = " "
name := namer.Haikunate()
logx.Bg().Info("starting server",
zap.String("serial", args.Serial),
zap.String("name", name))
logx.Bg().Debug("listening on localhost:8080")
srv := &http.Server{
Addr: "0.0.0.0:8080",
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r, // Pass our instance of gorilla/mux in.
}
_ = r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
route.Handler(
gorilla.Middleware(opentracing.GlobalTracer(), route.GetHandler()))
return nil
})
// listen
go func() {
if err := srv.ListenAndServe(); err != nil {
fmt.Print(err)
}
}()
// server teardown
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel()
_ = srv.Shutdown(ctx)
logx.Bg().Debug("shutting down",
zap.String("serial", args.Serial),
zap.String("name", name))
os.Exit(0)
}