-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (90 loc) · 2.52 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"net/http"
"github.com/eliezedeck/gobase/logging"
"github.com/eliezedeck/webhook-ingestor/core"
"github.com/eliezedeck/webhook-ingestor/impl"
mongodbimpl "github.com/eliezedeck/webhook-ingestor/impl/mongodb"
"github.com/eliezedeck/webhook-ingestor/parameters"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
)
func main() {
// Setup logging
logging.Init()
logging.L = logging.L.Named("WebhookIngestor")
// ... can exit here if user is doing `-help`
parameters.ParseFlags()
// Setup Web server (using Echo)
e := buildEcho()
// Setup MemoryStorage instance
var (
configStorage core.ConfigStorage
reqStorage core.RequestsStorage
)
switch parameters.ParamStorage {
case "memory":
storage := impl.NewMemoryStorage()
configStorage = storage
reqStorage = storage
logging.L.Info("Using in-memory storage")
case "mongo":
storage, err := mongodbimpl.NewStorage(parameters.ParamStorageMongoUri, parameters.ParamStorageMongoDb)
if err != nil {
panic(err)
}
configStorage = storage
reqStorage = storage
logging.L.Info("Using MongoDB as storage")
default:
panic("invalid -storage parameter, valid values are 'memory' and 'mongo'")
}
// -----------
setupWebhookPaths(e, configStorage, reqStorage)
// -----------
// Set up the Admin paths
if parameters.ParamListen == parameters.ParamAdminListen {
core.SetupAdministration(e, e, configStorage, reqStorage, parameters.ParamAdminPath)
} else {
a := buildEcho()
core.SetupAdministration(e, a, configStorage, reqStorage, parameters.ParamAdminPath)
go func() {
panic(a.Start(parameters.ParamAdminListen))
}()
}
panic(e.Start(parameters.ParamListen))
}
func buildEcho() *echo.Echo {
e := echo.New()
e.HidePort = true
e.HideBanner = true
e.Use(logging.ZapLoggerForEcho(logging.L))
e.Use(logging.RecoverWithZapLogging)
e.HTTPErrorHandler = func(err error, c echo.Context) {
if c.Response().Committed {
return
}
if c.Request().Method == http.MethodHead {
if err := c.NoContent(http.StatusInternalServerError); err != nil {
logging.L.Error("Error while returning NoContent for HEAD request", zap.Error(err))
}
return
}
erro := c.JSON(http.StatusInternalServerError, 500)
if erro != nil {
e.Logger.Error(erro)
}
}
return e
}
func setupWebhookPaths(e *echo.Echo, config core.ConfigStorage, reqStore core.RequestsStorage) {
w, err := config.GetAllWebhooks()
if err != nil {
panic(err)
}
for _, webhook := range w {
if err = webhook.RegisterWithEcho(e, reqStore); err != nil {
panic(err)
}
}
}