-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.go
92 lines (74 loc) · 2.07 KB
/
application.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
package main
import (
"net/http"
"os"
"time"
"github.com/diontristen/go-crud/api"
"github.com/diontristen/go-crud/util"
"github.com/getsentry/sentry-go"
"github.com/gorilla/mux"
"github.com/muravjov/slog/watcher"
)
func addContext(f func(w http.ResponseWriter, r *http.Request, ac *util.AppContext), ac *util.AppContext) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
f(w, r, ac)
}
}
func appMain() (exitOk bool) {
config, err := util.NewConfig()
if err != nil {
util.Error(err)
return
}
ac, acError := util.NewAppContext(config)
if acError != nil {
util.Errorf("error when creating context: %v\n", acError)
return
}
defer ac.Close()
router := mux.NewRouter()
router.HandleFunc("/v1/contact", addContext(api.GetContact, ac)).Methods(http.MethodGet)
router.HandleFunc("/v1/contact", addContext(api.PostContact, ac)).Methods(http.MethodPost, http.MethodOptions)
router.HandleFunc("/v1/contact", addContext(api.RemoveContact, ac)).Methods(http.MethodDelete)
router.HandleFunc("/v1/update/contact", addContext(api.UpdateContact, ac)).Methods(http.MethodPost, http.MethodOptions)
http.Handle("/", router)
server := &http.Server{Addr: getListenAddr(ac.Config), Handler: &util.ServerHandler{
Router: router,
}}
util.Infof("App listening address %s", server.Addr)
return util.ListenAndServe(server, func() {})
}
func getListenAddr(config util.Config) string {
listen := ":5000"
if configListen := config.Listen; configListen != nil {
listen = *configListen
}
return listen
}
func setupLogging(config util.Config) func() {
close := func() {}
if config.DebugFlag {
util.DebugFlag = true
}
if config.SentryDSN != "" {
watcher.StartWatcher(config.SentryDSN, "")
err := util.SetupSentry(config.SentryDSN)
if err != nil {
util.Errorf("SetupSentry: %s", err)
} else {
close = func() {
if !sentry.Flush(time.Second * 10) {
util.Error("Some Sentry events may not have been sent")
}
}
}
}
return close
}
func main() {
code := 0
if !appMain() {
code = 1
}
os.Exit(code)
}