-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
64 lines (53 loc) · 1.18 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
58
59
60
61
62
63
64
package server
import (
"context"
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/tennashi/goem"
"github.com/tennashi/goem/server/handler"
)
// Run is ...
func Run(ctx context.Context, config *goem.Config) error {
s := newServer(config)
return s.run(ctx)
}
type server struct {
config *goem.Config
}
func newServer(config *goem.Config) *server {
return &server{config}
}
func (s *server) run(ctx context.Context) error {
log.Println("server intializing")
mdr := goem.NewMaildirRoot(s.config.RootDir)
r := newRouter(mdr)
hs := &http.Server{
Addr: ":" + s.config.Server.Port,
Handler: r,
}
log.Println("server intialized")
log.Printf("server running on localhost:%v", s.config.Server.Port)
eCh := make(chan error)
go func() {
defer close(eCh)
if err := hs.ListenAndServe(); err != nil {
eCh <- err
}
}()
select {
case <-ctx.Done():
log.Println("server shuting down")
return hs.Shutdown(ctx)
case err := <-eCh:
return err
}
}
func newRouter(mdr *goem.MaildirRoot) *chi.Mux {
r := chi.NewRouter()
h := handler.New(mdr)
r.Get("/maildir/", h.ListMaildir)
r.Get("/maildir/{dirName}", h.ListMail)
r.Get("/maildir/{dirName}/{key}", h.GetMail)
return r
}