-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
57 lines (49 loc) · 1.2 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
package server
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
)
// New instantes new Chi.Router
func New() chi.Router {
router := chi.NewRouter()
return router
}
// Config represents server specific config
type Config struct {
Port string
ReadTimeoutSeconds int
WriteTimeoutSeconds int
Debug bool
}
//Start starts rest api server
func Start(r chi.Router, lg *logrus.Logger, cfg *Config) {
s := &http.Server{
Addr: cfg.Port,
ReadTimeout: time.Duration(cfg.ReadTimeoutSeconds) * time.Second,
WriteTimeout: time.Duration(cfg.WriteTimeoutSeconds) * time.Second,
Handler: r,
}
go func() {
lg.Infof("service started from port: %v", cfg.Port)
if err := s.ListenAndServe(); err != nil {
lg.Info("Shutting down the server")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 10 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
lg.Info("server stoped")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := s.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}