/
mmic.go
68 lines (58 loc) · 1.68 KB
/
mmic.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
65
66
67
68
package mmic
import (
log "github.com/Sirupsen/logrus"
"fmt"
"github.com/eternnoir/mmic/mmic/config"
"github.com/eternnoir/mmic/mmic/handlers"
"github.com/eternnoir/mmic/mmic/handlers/trello"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware"
"net/http"
)
type Mmic struct {
Port string
HttpServer *echo.Echo
RouteMap map[string]handlers.Handler
}
func NewMmic(config *config.MmicConfig) (*Mmic, error) {
log.Infof("Create Mnic by mmic config: %#v", config)
mmic := &Mmic{Port: config.Port, HttpServer: echo.New()}
mmic.RouteMap = make(map[string]handlers.Handler)
err := mmic.bindHandlers(config)
if err != nil {
return nil, err
}
return mmic, nil
}
func (mmic *Mmic) bindHandlers(config *config.MmicConfig) error {
err := mmic.bindTrelloHandlers(config.TrelloConfigs)
if err != nil {
log.Errorf("Bind handler fail. %s", err)
return err
}
return nil
}
func (mmic *Mmic) bindTrelloHandlers(configs []config.TrelloConfig) error {
for _, config := range configs {
if _, inmap := mmic.RouteMap[config.RoutePath]; inmap {
return fmt.Errorf("Rout path %s already exist.", config.RoutePath)
}
th := trello.NewTrelloHandler(config)
log.Debugf("Bind %#v", config)
mmic.HttpServer.Post(config.RoutePath, th)
mmic.HttpServer.Head(config.RoutePath, emptyHandler())
mmic.RouteMap[config.RoutePath] = th
}
return nil
}
func (mmic *Mmic) Start() {
mmic.HttpServer.Use(middleware.Logger())
mmic.HttpServer.Use(middleware.Recover())
mmic.HttpServer.Run(standard.New(":" + mmic.Port))
}
func emptyHandler() echo.HandlerFunc {
return func(c echo.Context) error {
return c.String(http.StatusOK, "Hi")
}
}