-
Notifications
You must be signed in to change notification settings - Fork 433
/
Copy pathserver.go
52 lines (40 loc) · 1.19 KB
/
server.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
package internal
import (
"context"
"fmt"
"time"
log "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin"
v1 "github.com/tailwarden/komiser/internal/api/v1"
"github.com/tailwarden/komiser/models"
)
func loggingMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
startTime := time.Now()
ctx.Next()
endTime := time.Now()
latencyTime := endTime.Sub(startTime)
reqMethod := ctx.Request.Method
reqUri := ctx.Request.RequestURI
statusCode := ctx.Writer.Status()
clientIP := ctx.ClientIP()
log.WithFields(log.Fields{
"method": reqMethod,
"uri": reqUri,
"status": statusCode,
"latency": latencyTime,
"ip": clientIP,
}).Info("HTTP request")
ctx.Next()
}
}
func runServer(address string, port int, telemetry bool, cfg models.Config, configPath string, accounts []models.Account) error {
log.Infof("Komiser version: %s, commit: %s, buildt: %s", Version, Commit, Buildtime)
r := v1.Endpoints(context.Background(), telemetry, analytics, db, cfg, configPath, accounts)
r.Use(loggingMiddleware())
if err := r.Run(fmt.Sprintf("%s:%d", address, port)); err != nil {
return err
}
log.Infof("Server started on %s:%d", address, port)
return nil
}