-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
107 lines (87 loc) · 2.45 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
102
103
104
105
106
107
package main
import (
"context"
"fmt"
"github.com/decentraland/dcl-gin/pkg/dclgin"
"github.com/gin-gonic/gin"
"github.com/decentraland/content-service/data"
"github.com/decentraland/content-service/internal/routes"
"github.com/decentraland/content-service/metrics"
"github.com/decentraland/content-service/config"
"github.com/decentraland/content-service/storage"
"github.com/ipsn/go-ipfs/core"
log "github.com/sirupsen/logrus"
"github.com/toorop/gin-logrus"
)
func main() {
conf := config.GetConfig("config")
l := newLogger()
router := gin.New()
router.Use(ginlogrus.Logger(l), gin.Recovery())
if err := setLogLevel(l, conf.LogLevel); err != nil {
l.Fatal("error setting log level")
}
l.Info("Starting server")
if conf.Metrics.Enabled {
metricsConfig := &dclgin.HttpMetricsConfig{
TraceName: conf.Metrics.AppName,
AnalyticsRateEnabled: true,
}
if traceError := dclgin.EnableTrace(metricsConfig, router); traceError != nil {
log.WithError(traceError).Fatal("Unable to start metrics")
}
defer dclgin.StopTrace()
}
InitializeHandler(router, conf, l)
addr := fmt.Sprintf("%s:%d", conf.Server.Host, conf.Server.Port)
if err := router.Run(addr); err != nil {
log.WithError(err).Fatal("Failed to start server.")
}
}
func InitializeHandler(r gin.IRouter, conf *config.Configuration, l *log.Logger) {
agent, err := metrics.Make(conf.Metrics)
if err != nil {
log.Fatal("Error initializing metrics agent")
}
// Initialize Redis client
client, err := data.NewRedisClient(conf.Redis.Address, conf.Redis.Password, conf.Redis.DB, agent)
if err != nil {
log.Fatal("Error initializing Redis client")
}
// Initialize IPFS for CID calculations
var ipfsNode *core.IpfsNode
ipfsNode, err = initIpfsNode()
if err != nil {
log.Fatal("Error initializing IPFS node")
}
sto := storage.NewStorage(&conf.Storage, agent)
routes.AddRoutes(r, &routes.Config{
Client: client,
Storage: sto,
Node: ipfsNode,
Agent: agent,
Conf: conf,
Log: l,
})
}
func initIpfsNode() (*core.IpfsNode, error) {
ctx, _ := context.WithCancel(context.Background())
return core.NewNode(ctx, nil)
}
func newLogger() *log.Logger {
l := log.New()
formatter := log.JSONFormatter{
FieldMap: log.FieldMap{
log.FieldKeyTime: "@timestamp",
},
}
l.SetFormatter(&formatter)
return l
}
func setLogLevel(logger *log.Logger, level string) error {
lvl, err := log.ParseLevel(level)
if err == nil {
logger.SetLevel(lvl)
}
return err
}