forked from gdaz/assessment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
42 lines (32 loc) · 752 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
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/Dsypasit/assessment/expense"
"github.com/labstack/echo/v4"
)
func main() {
db := expense.InitDB()
handler := expense.CreateHandler(db)
e := echo.New()
e.Use(expense.AuthMiddleware)
expense.CreateRoute(e, handler)
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
log.Printf("Server up by port %v\n", os.Getenv("PORT"))
go func() {
if err := e.Start(os.Getenv("PORT")); err != nil {
e.Logger.Fatal("shutdown server")
}
}()
<-shutdown
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(ctx); err != nil {
e.Logger.Fatal(err)
}
}