-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
36 lines (30 loc) · 1.17 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
package main
import (
"fmt"
"github.com/exlskills/demo-go-webservice/config"
"github.com/exlskills/demo-go-webservice/models"
"github.com/exlskills/demo-go-webservice/routes"
"github.com/gorilla/handlers"
"net/http"
"os"
"time"
)
var Log = config.Cfg().GetLogger()
var CORSHandler = handlers.CORS(handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}), handlers.AllowCredentials(), handlers.AllowedHeaders([]string{"x-locale", "x-api-key", "content-type", "access-control-request-headers", "access-control-request-method", "x-csrftoken"}), handlers.AllowedOrigins(config.Cfg().AllowedOrigins))
func main() {
Log.Info("Setting up database connection ...")
for {
err := models.Setup()
if err != nil {
Log.WithError(err).Error("Error setting up database connection, retrying ...")
time.Sleep(time.Second * 3)
} else {
break
}
}
Log.Info("Connected to database")
// TODO: Implement graceful stop
Log.Info("Starting HTTP server")
http.ListenAndServe(fmt.Sprintf("%s:%s", config.Cfg().ListenAddress, config.Cfg().ListenPort), CORSHandler(handlers.CombinedLoggingHandler(os.Stdout, routes.CreateRouter())))
Log.Info("Stopped HTTP server")
}