-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
118 lines (95 loc) · 2.13 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-chi/chi"
"github.com/m-lukas/github-analyser/app"
"github.com/m-lukas/github-analyser/db"
"github.com/m-lukas/github-analyser/logger"
"github.com/m-lukas/github-analyser/mailer"
"github.com/m-lukas/github-analyser/util"
"github.com/joho/godotenv"
)
/*
Server helper structure to setup the HTTP-Server
*/
type Server struct {
Router *chi.Mux
HTTPServer *http.Server
Config *ServerConfig
}
/*
ServerConfig provides a structure for setting and transmitting the Servers config.
*/
type ServerConfig struct {
Host string
Port int
APIPath string
}
func defaultServerConfig() *ServerConfig {
return &ServerConfig{
Host: "",
Port: 80,
APIPath: "/api",
}
}
func configHTTPServer(config *ServerConfig, handler http.Handler) *http.Server {
return &http.Server{
Addr: fmt.Sprintf(`%s:%d`, config.Host, config.Port),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: handler,
}
}
func runServer(server *Server) {
go func() {
err := server.HTTPServer.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}()
logger.Info(fmt.Sprintf(`Server has been started on port: %d`, server.Config.Port))
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
logger.Warn("Server was stopped!")
os.Exit(0)
}
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
}
func main() {
//start mail worker
go mailer.StartWorker()
env := os.Getenv("ENV")
setupFlag := util.ReadBoolFlag("FLAG_DO_SETUP")
metrixFlag := util.ReadBoolFlag("FLAG_DO_METRIX")
flag.String("enviroment", env, "Possible values: <dev|prod>")
err := db.Init()
if err != nil {
log.Println(err)
}
if setupFlag {
setupInit()
}
if metrixFlag {
metrixInit()
}
server := &Server{
Config: defaultServerConfig(),
}
server.Router = app.InitRouter(server.Config.APIPath)
handler := app.HandleCors(server.Router)
server.HTTPServer = configHTTPServer(server.Config, handler)
logger.Info("Server has been configurated!")
runServer(server)
}