-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (43 loc) · 927 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
47
48
49
50
51
52
53
54
55
package espresso
import (
"net/http"
"github.com/googollee/module"
)
type Espresso struct {
repo *module.Repo
mux *http.ServeMux
router Router
}
func New() *Espresso {
ret := &Espresso{
repo: module.NewRepo(),
mux: http.NewServeMux(),
}
ret.router = &router{
mux: ret.mux,
}
ret.Use(logHandling, cacheAllError)
return ret
}
func (s *Espresso) AddModule(provider ...module.Provider) {
for _, p := range provider {
s.repo.Add(p)
}
}
func (s *Espresso) Use(middlewares ...HandleFunc) {
s.router.Use(middlewares...)
}
func (s *Espresso) HandleFunc(handleFunc HandleFunc) {
s.router.HandleFunc(handleFunc)
}
func (s *Espresso) WithPrefix(path string) Router {
return s.router.WithPrefix(path)
}
func (s *Espresso) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, err := s.repo.InjectTo(r.Context())
if err != nil {
panic(err)
}
r = r.WithContext(ctx)
s.mux.ServeHTTP(w, r)
}