-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
82 lines (69 loc) · 1.82 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
package server
import (
"context"
"io/fs"
"log/slog"
"net/http"
"os/signal"
"syscall"
"time"
"github.com/fabiodcorreia/despensa-app/internal/routes"
"github.com/labstack/echo/v4"
)
type Server struct {
serverCtx context.Context
stopCtx context.CancelFunc
engine *echo.Echo
}
func NewServer(ctx context.Context, options ...ServerOption) *Server {
engine := echo.New()
engine.HideBanner = true
engine.HidePort = true
serverCtx, stopCtx := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
s := &Server{
serverCtx,
stopCtx,
engine,
}
for _, opt := range options {
opt(s)
}
return s
}
func (s *Server) AddPublic(assets fs.FS) {
s.engine.StaticFS("/", echo.MustSubFS(assets, "public"))
}
func (s *Server) AddRoutes(routes routes.Routes) {
for _, r := range routes.Routes() {
s.engine.Add(r.Method, r.Path, r.Handle)
slog.Info("Added route", "method", r.Method, "path", r.Path)
}
}
// Start the server and should be called in with go routine
//
// go s.Start("127.0.0.1:8080")
func (s *Server) Start(bindAddr string) {
if err := s.engine.Start(bindAddr); err != http.ErrServerClosed {
// s.Log().Errorf("server start : %v", err)
slog.Error("Server starting", err)
s.stopCtx() // If error on start call stop and exit
}
}
// WaitAndTerminate will wait until a termination signal is received and
// then shutdown the server.
func (s *Server) WaitAndTerminate() error {
s.wait()
return s.terminate()
}
// Wait will wait until a interrupt signal is received
func (s *Server) wait() {
defer s.stopCtx()
// Wait for interrupt signal to gracefully shutdown the server.
<-s.serverCtx.Done()
}
// Stop will tell the server to shutdown with a 10s timeout
func (s *Server) terminate() error {
ctx, cancel := context.WithTimeout(s.serverCtx, 30*time.Second)
defer cancel()
return s.engine.Shutdown(ctx)
}