This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
99 lines (83 loc) · 2.26 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
package main
import (
"github.com/emvi/logbuch"
ghandlers "github.com/gorilla/handlers"
"github.com/gorilla/mux"
conf "github.com/muety/mailwhale/config"
"github.com/muety/mailwhale/service"
"github.com/muety/mailwhale/web/handlers"
"github.com/muety/mailwhale/web/routes/api"
"github.com/rs/cors"
"github.com/timshannon/bolthold"
"net/http"
"time"
)
var (
config *conf.Config
store *bolthold.Store
userService *service.UserService
)
func main() {
config = conf.Load()
store = conf.LoadStore(config.Store.Path)
defer store.Close()
// Set log level
if config.IsDev() {
logbuch.SetLevel(logbuch.LevelDebug)
} else {
logbuch.SetLevel(logbuch.LevelInfo)
}
// Services
userService = service.NewUserService()
// Global middlewares
recoverMiddleware := ghandlers.RecoveryHandler()
loggingMiddleware := handlers.NewLoggingMiddleware(logbuch.Info, []string{})
// CORS
corsHandler := cors.New(cors.Options{
AllowedOrigins: config.Web.CorsOrigins,
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
})
// Configure routing
router := mux.NewRouter().StrictSlash(true)
router.Use(recoverMiddleware, loggingMiddleware)
// Handlers
api.NewHealthHandler().Register(router)
api.NewMailHandler().Register(router)
api.NewClientHandler().Register(router)
api.NewUserHandler().Register(router)
api.NewTemplateHandler().Register(router)
handler := corsHandler.Handler(router)
// Static routes
router.PathPrefix("/").Handler(&handlers.SPAHandler{
StaticPath: "./webui/public",
IndexPath: "index.html",
ReplaceBasePath: config.Web.GetPublicUrl() + "/",
NoCache: config.IsDev(),
})
listen(handler, config)
}
func listen(handler http.Handler, config *conf.Config) {
var s4 *http.Server
s4 = &http.Server{
Handler: handler,
Addr: config.Web.ListenAddr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
go func() {
logbuch.Info("web server started, listening on %s", config.Web.ListenAddr)
if err := s4.ListenAndServe(); err != nil {
logbuch.Fatal("failed to start web server: %v", err)
}
}()
<-make(chan interface{}, 1)
}