-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (101 loc) · 3.08 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"context"
"fmt"
"go-graphql-mongo-server/config"
"go-graphql-mongo-server/dbmigration"
"go-graphql-mongo-server/logger"
"go-graphql-mongo-server/models"
"go-graphql-mongo-server/routes"
"net/http"
"os"
"strings"
"time"
"github.com/adammck/venv"
"github.com/gorilla/handlers"
"github.com/robfig/cron/v3"
"github.com/urfave/negroni"
)
type Service struct {
HTTPServer http.Server
}
func main() {
// Initialize config
config.InitializeConfig(venv.OS())
// Initialize Logger
logger.Initialize()
// Initialize Database
models.InitializeDB()
//Run DB Migration
err := dbmigration.RunDbSchemaMigration("")
if err != nil {
panic(fmt.Errorf("error while running migration : %v", err))
}
// Initialize Cron
setUpCronJobs()
// Configure Server
service := &Service{}
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"})
// Get CORS Allowed Origins from config
str := config.Store.CORSAllowOrigins
CORSAllowOrigins := []string{}
if str != "" {
CORSAllowOrigins = strings.Split(str, ",")
}
origins := handlers.AllowedOrigins(CORSAllowOrigins)
logger.Log.Info("CORS Allow Origins: ", CORSAllowOrigins)
router := routes.NewRouter()
n := negroni.New(negroni.NewRecovery(), negroni.NewLogger())
n.UseHandler(router)
service.HTTPServer = http.Server{
Addr: ":" + config.Store.ServicePort,
Handler: handlers.CORS(origins, headers, methods)(n),
ReadHeaderTimeout: 10 * time.Second,
// Uncomment the next line to disable HTTP 2
// TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
err = service.Run()
if err != nil {
logger.Log.Fatal("Error starting the server", err)
}
}
func (s *Service) Run() error {
logger.Log.Info("Starting the server on port ", config.Store.ServicePort)
if checkIfHTTPSCertExists() {
logger.Log.Info("HTTPS Enabled")
return s.HTTPServer.ListenAndServeTLS(config.Store.HTTPSCert.CertFilePath, config.Store.HTTPSCert.KeyFilePath)
}
logger.Log.Info("HTTPS Disabled")
return s.HTTPServer.ListenAndServe()
}
func checkIfHTTPSCertExists() bool {
if config.Store.HTTPSCert.CertFilePath == "" || config.Store.HTTPSCert.KeyFilePath == "" {
return false
}
if _, err := os.Stat(config.Store.HTTPSCert.CertFilePath); os.IsNotExist(err) {
logger.Log.Error("HTTPS Cert File does not exist: ", config.Store.HTTPSCert.CertFilePath)
return false
}
if _, err := os.Stat(config.Store.HTTPSCert.KeyFilePath); os.IsNotExist(err) {
logger.Log.Error("HTTPS Key File does not exist: ", config.Store.HTTPSCert.KeyFilePath)
return false
}
config.Store.HTTPSCert.HTTPSEnabled = true
return true
}
func (s *Service) Shutdown() error {
if err := s.HTTPServer.Shutdown(context.Background()); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}
// Set up all cron jobs
func setUpCronJobs() {
cronJob := cron.New()
_, err := cronJob.AddFunc("@every 5m", routes.CheckDbConnection)
if err != nil {
logger.Log.Error(err)
}
cronJob.Start()
}