-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
161 lines (134 loc) · 3.36 KB
/
http.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
_ "embed"
"fmt"
"net"
"net/url"
"os"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/redis/go-redis/v9"
)
func rawHeaderLog(b []byte) []string {
s := string(b)
s = strings.ReplaceAll(s, "\r", "")
return strings.Split(s, "\n")
}
func replaceCollectorURL(in []byte, collectorURL *url.URL) []byte {
str := string(in)
str = strings.ReplaceAll(str, collectorURLReplacement, collectorURL.String())
return []byte(str)
}
func httpErrorResponse(c *fiber.Ctx, errMsg errorMessage) error {
c.Status(errMsg.code)
return c.JSON(errMsg.msg)
}
func staticCache(c *fiber.Ctx) {
c.Set(fiber.HeaderCacheControl, "public, max-age=31536000, immutable")
c.Set(nginxXAccelExpires, "31536000")
}
func staticCacheLimit(c *fiber.Ctx, ttl uint) {
c.Set(fiber.HeaderCacheControl, "public, max-age=86400")
c.Set(nginxXAccelExpires, "86400")
}
func noCache(c *fiber.Ctx) {
c.Set(fiber.HeaderCacheControl, "no-cache, no-store, must-revalidate")
c.Set(fiber.HeaderPragma, "no-cache")
c.Set(fiber.HeaderExpires, "0")
c.Set(nginxXAccelExpires, "0")
}
func getClientIP(c *fiber.Ctx) net.IP {
ipObjectStored := c.Locals("ip")
if ipObjectStored != nil {
return ipObjectStored.(net.IP)
}
ipString := c.Get("x-real-ip")
if ipString == "" {
ipString = c.IP()
}
ipObject := net.ParseIP(ipString)
c.Locals("ip", ipObject)
return ipObject
}
func newHTTPServer(
conf *config,
geoParser *geoParser,
refererParser *refererParser,
userAgentParser *userAgentParser,
projectsManager *projects,
redisClient *redis.Client,
) *fiber.App {
preforkS := os.Getenv("ENABLE_PREFORK")
prefork := false
if preforkS == "1" {
prefork = true
}
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
StrictRouting: true,
BodyLimit: 2 * 1024 * 1024,
Prefork: prefork,
ErrorHandler: func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
ip := getClientIP(c)
defer conf.getLogger().
Error().
Str("error", err.Error()).
Str("ef", fmt.Sprintf("%+v", err)).
Str("ip", ip.String()).
Str("method", c.Method()).
Str("path", c.Path()).
Str("qs", string(c.Request().URI().QueryString())).
Str("body", string(c.Request().Body())).
Strs("headers", rawHeaderLog(c.Request().Header.RawHeaders())).
Int("status_code", code).
Send()
return httpErrorResponse(c, errorInternalServerError)
},
})
// recover
app.Use(recover.New())
// init middleware
app.Use(func(c *fiber.Ctx) error {
ip := getClientIP(c)
defer conf.getLogger().
Trace().
Str("ip", ip.String()).
Str("method", c.Method()).
Str("url", c.Context().URI().String()).
Msg("http_access")
return c.Next()
})
recordHandler := func(c *fiber.Ctx) error {
return httpRecord(
c,
conf,
refererParser,
geoParser,
userAgentParser,
projectsManager,
redisClient,
)
}
app.Post("/", recordHandler)
app.Get("/", recordHandler)
httpAppAssets(app, conf)
// 404
app.Use(func(c *fiber.Ctx) error {
code := fiber.StatusNotFound
defer conf.getLogger().
Trace().
Str("ip", getClientIP(c).String()).
Str("method", c.Method()).
Str("url", c.Context().URI().String()).
Int("status_code", code).
Send()
c.Status(code)
return c.JSON(code)
})
return app
}