-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
57 lines (46 loc) · 1000 Bytes
/
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
package main
import (
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/phuslu/log"
)
const (
addr = ":8080"
)
var (
logger = &log.Logger{
TimeFormat: "15:04:05",
Caller: 2,
Writer: &log.ConsoleWriter{
ColorOutput: true,
QuoteString: true,
},
Level: log.InfoLevel,
}
)
func main() {
router := mux.NewRouter()
router.Use(LOGMiddleware)
router.HandleFunc("/health", StatusHandler)
api := router.PathPrefix("/api").Subrouter()
api.Use(JSONMiddleware)
api.HandleFunc("/info", InfoHandler)
api.HandleFunc("/calculate", CalculateHandler)
api.HandleFunc("/calculate/{number}", CalculateHandler)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./web")))
service := &http.Server{
Handler: router,
Addr: addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
logger.
Info().
Str("addr", addr).
Msg("Listening on")
logger.
Fatal().
Err(service.ListenAndServe()).
Msg("Failed to start service")
}