-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (47 loc) · 1.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
package main
import (
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/caarlos0/env"
"github.com/edwinvautier/go-boilerplate/database"
"github.com/edwinvautier/go-boilerplate/helpers"
"github.com/edwinvautier/go-boilerplate/routes"
"github.com/gin-gonic/gin"
cors "github.com/itsjamie/gin-cors"
log "github.com/sirupsen/logrus"
)
func main() {
// Connect to database and execute migrations
cfg := database.Config{}
if err := env.Parse(&cfg); err != nil {
log.Fatal(err)
}
err := database.Init(cfg)
helpers.DieOnError("database connection failed", err)
database.Migrate()
// Setup router
router := gin.Default()
router.Use(cors.Middleware(cors.Config{
Origins: "*",
Methods: "GET, PUT, POST, DELETE",
RequestHeaders: "Origin, Authorization, Content-Type",
ExposedHeaders: "Authorization",
MaxAge: 50 * time.Second,
Credentials: true,
ValidateHeaders: false,
}))
routes.Init(router)
go func() {
if err := router.Run(":8000"); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// ----------------- CLOSE APP -----------------
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Info("Shutting down server...")
}