-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
93 lines (79 loc) · 1.95 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package server
import (
"context"
"fmt"
"memoapi/memo/handler"
"memoapi/memo/repository"
"memoapi/memo/usecase"
"net/http"
"os"
"os/signal"
"time"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// Server represents server.
type Server struct {
DB *gorm.DB
Host string
Port int
ServerReady chan<- interface{}
echo *echo.Echo
shutdown chan interface{}
}
// NewServer returns new Server object.
func NewServer(port int, host string, db *gorm.DB, serverReady chan<- interface{}) *Server {
return &Server{
DB: db,
Host: host,
Port: port,
ServerReady: serverReady,
}
}
func (s *Server) setupRoute() {
e := echo.New()
repo := repository.NewPostgresMemoRepository(s.DB)
usecase := usecase.NewMemoUsecase(repo)
memoHandler := handler.NewMemoHTTPHandler(usecase)
e.POST("/memo", memoHandler.HandleCreateMemo)
e.GET("/memo", memoHandler.HandleGetAllMemo)
e.GET("/memo/:memo_id", memoHandler.HandleGetMemoByID)
s.echo = e
}
// Address return server address.
func (s *Server) GetAddress() string {
return fmt.Sprintf("%s:%d", s.Host, s.Port)
}
// Start starts server.
func (s *Server) Start() {
s.setupRoute()
go func() {
if err := s.echo.Start(s.GetAddress()); err != nil && err != http.ErrServerClosed {
log.Error(err.Error())
log.Info("shutting down the server")
}
}()
if s.ServerReady != nil {
s.ServerReady <- struct{}{}
}
s.shutdown = make(chan interface{}, 1)
defer close(s.shutdown)
quit := make(chan os.Signal, 1)
defer close(quit)
signal.Notify(quit, os.Interrupt)
select {
case sig := <-quit:
log.Info("received: ", sig)
case <-s.shutdown:
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.echo.Shutdown(ctx); err != nil {
log.Fatal("failed to graceful shutdown the server:", err)
}
}
// Shutdown shutdowns the server.
func (s *Server) Shutdown() {
s.shutdown <- struct{}{}
}