-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
49 lines (41 loc) · 1013 Bytes
/
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
package main
import (
"context"
"log"
"os"
"os/signal"
"time"
"github.com/atranimal/go-echo-boilerplate/routes"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
// Load env variables
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
e := echo.New()
// Middlewares
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
routes.RegisterRoutes(e)
//Start Server
go func() {
if err := e.Start(":1323"); err != nil {
e.Logger.Info("Shutting Down...")
}
}()
//Wait For interrupt signal gracefully shutdown the server after timeout.
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 {
e.Logger.Fatal(err)
}
}