-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
53 lines (42 loc) · 1.09 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
package http
import (
"context"
"net/http"
"github.com/go-chi/chi"
"github.com/sirupsen/logrus"
)
type Server struct {
server *http.Server
router chi.Router
logger logrus.FieldLogger
}
func NewServer(options ...func(s *Server)) *Server {
srv := &Server{server: &http.Server{}}
for _, option := range options {
option(srv)
}
return srv
}
var ErrServerClosed = http.ErrServerClosed
func (s *Server) Post(pattern string, f func(w http.ResponseWriter, r *http.Request)) {
s.router.Post(pattern, f)
}
func (s *Server) Put(pattern string, f func(w http.ResponseWriter, r *http.Request)) {
s.router.Put(pattern, f)
}
func (s *Server) Get(pattern string, f func(w http.ResponseWriter, r *http.Request)) {
s.router.Get(pattern, f)
}
func (s *Server) Delete(pattern string, f func(w http.ResponseWriter, r *http.Request)) {
s.router.Delete(pattern, f)
}
func (s *Server) Start() error {
s.logger.Infof("# starting http server #")
if err := s.server.ListenAndServe(); err != nil {
return err
}
return nil
}
func (s *Server) Shutdown(ctx context.Context) error {
return s.server.Shutdown(ctx)
}