-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
72 lines (58 loc) · 1.56 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
log "github.com/sirupsen/logrus"
"github.com/archproj/slackoverflow/config"
"github.com/archproj/slackoverflow/database"
m "github.com/archproj/slackoverflow/middlewares"
"github.com/archproj/slackoverflow/routes"
)
const (
VERSION = "0.2.0"
)
func main() {
cfg, err := config.Load() // from environment
if err != nil {
log.Fatal(err)
}
e := echo.New()
db, err := database.Init(cfg)
if err != nil {
log.Fatal(err)
}
e.Use(m.EmbedInContext(cfg, db))
if cfg.Env == `PRODUCTION` {
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"https://slackoverflowmake.herokuapp.com/"},
AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
}))
} else if cfg.Env == `DEVELOPMENT` {
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"http://localhost:3000", "https://https://5c942a8c.ngrok.io"},
AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
}))
}
routes.Bind(e)
go func() {
err := e.Start(fmt.Sprintf("%s:%s", cfg.Host, cfg.Port))
if err != nil {
log.Warning("Shutting down server...", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}