-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (53 loc) · 1.34 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"github.com/kutsuzawa/slackbot/events"
"github.com/kutsuzawa/slackbot/lib"
"github.com/kutsuzawa/slackbot/handler"
"github.com/kutsuzawa/slackbot/models"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func healthHandler(c echo.Context) error {
return c.String(http.StatusOK, "OK")
}
func checkEnv() error {
envs := []string{"CHANNEL", "SLACKWEBHOOK", "TOKEN"}
for _, v := range envs {
if os.Getenv(v) == "" {
err := fmt.Errorf("env variable %s is not defined", v)
return err
}
}
return nil
}
func main() {
if err := checkEnv(); err != nil {
log.Fatal(err)
}
slack := models.NewSlack(os.Getenv("SLACKWEBHOOK"), os.Getenv("CHANNEL"), os.Getenv("TOKEN"))
eventMap := make(map[string]handler.Event)
eventMap["pull_request"] = &events.PR{}
controller := handler.NewEventController(eventMap, slack, &lib.Util{})
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.POST("/event", controller.EventHandler)
e.GET("/", healthHandler)
debugPort := os.Getenv("DEBUG_PORT")
if debugPort == "" {
debugPort = "6060"
}
go func() {
e.Logger.Print(http.ListenAndServe(fmt.Sprintf(":%s", debugPort), nil))
}()
port := os.Getenv("PORT")
if port == "" {
port = "3030"
}
e.Logger.Fatal(e.Start(fmt.Sprintf(":%s", port)))
}