-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
49 lines (38 loc) · 1.01 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
package server
import (
"context"
"fmt"
"strconv"
"time"
"github.com/go-playground/validator"
api "github.com/konrad-amtenbrink/feed/api/v0.1"
"github.com/konrad-amtenbrink/feed/internal/db"
"github.com/konrad-amtenbrink/feed/internal/storage"
"github.com/labstack/echo/v4"
)
type Server struct {
Echo *echo.Echo
}
func New(db db.Database, storage storage.Storage) *Server {
e := echo.New()
e.HideBanner = true
e.Validator = &CustomValidator{Validator: validator.New()}
api.SetupV0_1(e, db, storage)
return &Server{
Echo: e,
}
}
func (srv *Server) Run(port int) error {
return srv.Echo.Start(":" + strconv.Itoa(port))
}
func (srv *Server) Shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Echo.Shutdown(ctx); err != nil {
if closeErr := srv.Echo.Close(); closeErr != nil {
return fmt.Errorf("shutting down %v and closing server: %v", err, closeErr)
}
return fmt.Errorf("shutting down server: %v", err)
}
return nil
}