-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
61 lines (51 loc) · 1.38 KB
/
server.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
package main
import (
"fmt"
"log"
"net/http"
"scheduler-booking/api"
"scheduler-booking/data"
"scheduler-booking/service"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
"github.com/jinzhu/configor"
)
var Config AppConfig
func main() {
configor.New(&configor.Config{ENVPrefix: "APP", Silent: true}).Load(&Config, "config.yml")
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
fmt.Println(Config.Server.Cors)
if len(Config.Server.Cors) > 0 {
c := cors.New(cors.Options{
AllowedOrigins: Config.Server.Cors,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token", "Remote-Token", "X-Requested-With"},
AllowCredentials: true,
MaxAge: 300,
})
r.Use(c.Handler)
}
dao := data.NewDAO(Config.DB)
service := service.NewService(dao)
api := api.NewAPI(service)
api.InitRoutes(r)
if Config.Server.ResetFrequence > 0 {
go func() {
f := time.Duration(Config.Server.ResetFrequence) * time.Minute
ticker := time.NewTicker(f)
for range ticker.C {
log.Println("Reset data...")
dao.RestartData()
}
}()
}
log.Printf("Starting webserver at port " + Config.Server.Port)
err := http.ListenAndServe(Config.Server.Port, r)
if err != nil {
log.Println(err.Error())
}
}