forked from ebauman/widgetfactory
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
84 lines (62 loc) · 1.67 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
package main
import (
_ "embed"
"flag"
"github.com/ebauman/widgetfactory/database"
"github.com/ebauman/widgetfactory/metrics"
"github.com/ebauman/widgetfactory/web"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"net/http"
"os"
)
var (
dsn string
staticContentPath string
)
func init() {
logrus.SetOutput(os.Stdout)
flag.StringVar(&dsn, "dsn", "", "MySQL DSN")
flag.StringVar(&staticContentPath, "static-content-path", "./app",
"Path from where static content is served")
flag.Parse()
}
var (
requestCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "widgetfactory_requests_total",
Help: "The total number of received http requests (non-metrics)",
})
)
func main() {
logrus.Infof("Starting with mysql dsn %s", dsn)
logrus.Infof("Starting with static content path %s", staticContentPath)
prom := http.NewServeMux()
prom.Handle("/metrics", promhttp.Handler())
stopCh := make(chan error)
go func() {
logrus.Infof("Metrics on 0.0.0.0:9090")
err := http.ListenAndServe("0.0.0.0:9090", prom)
stopCh <- err
}()
app := fiber.New(fiber.Config{
AppName: "Widget Factory",
})
app.Use(func(c *fiber.Ctx) error {
logrus.Infof("Request for %s", c.Request().RequestURI())
requestCount.Inc()
return c.Next()
})
db := database.New(dsn)
go metrics.Start(db, stopCh)
svr := web.New(app, db, staticContentPath)
go func() {
logrus.Infof("Data on 0.0.0.0:8080")
err := svr.Listen("0.0.0.0:8080")
stopCh <- err
}()
err := <-stopCh
logrus.Fatal(err)
}