-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
46 lines (39 loc) · 778 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
43
44
45
46
package sigma
import (
"context"
"log"
"net/http"
"time"
"github.com/go-chi/chi/v5"
)
type (
Server struct {
mux *chi.Mux
srv *http.Server
}
)
func NewServer(addr string) *Server {
mux := chi.NewRouter()
return &Server{
mux: mux,
srv: &http.Server{
Addr: addr,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 20 * time.Second,
IdleTimeout: 10 * time.Second,
},
}
}
func (s *Server) Start() error {
log.Println("starting http server on", s.srv.Addr)
return s.srv.ListenAndServe()
}
func (s *Server) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.srv.Shutdown(ctx); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}