-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp.go
180 lines (147 loc) · 4.7 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"embed"
"html/template"
"net/http"
"strconv"
"strings"
"github.com/gofiber/adaptor/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/helmet/v2"
"github.com/gofiber/template/html"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//go:embed templates/*
var templates embed.FS
//go:embed static/*
var static embed.FS
func newHTTPServer(config *config) *fiber.App {
engine := html.NewFileSystem(http.FS(templates), ".html")
engine.Delims("[[", "]]")
engine.AddFunc(
"unescapeJS", func(s string) template.JS {
return template.JS(s)
},
)
engine.AddFunc(
"unescapeHTML", func(s string) template.HTML {
return template.HTML(s)
},
)
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Prefork: false,
Views: engine,
BodyLimit: 1024 * 1024 * 4,
ErrorHandler: func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
defer prometheusRequestError.WithLabelValues(strconv.Itoa(code)).Inc()
ip, _ := getRequestIP(c)
defer config.getLogger().
Error().
Str(logType, logTypeHTTPError).
Str(logPropertyError, err.Error()).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, c.Get(httpRequestHeaderRequestID, "")).
Str(logPropertyMethod, c.Method()).
Str(logPropertyURL, c.Request().URI().String()).
Int(logPropertyStatusCode, code).
Send()
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
return c.Status(code).SendString("Internal Server Error")
},
})
app.Use(recover.New())
app.Use(helmet.New())
handler := promhttp.HandlerFor(getPrometheusRegistry(), promhttp.HandlerOpts{})
app.Get("/metrics", adaptor.HTTPHandler(handler))
// middle ware
app.Use(func(c *fiber.Ctx) error {
c.Set("X-Robots-Tag", "noindex,nofollow")
ip, _ := getRequestIP(c)
defer config.getLogger().
Info().
Str(logType, logTypeHTTPRequest).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, c.Get(httpRequestHeaderRequestID, "")).
Str(logPropertyMethod, c.Method()).
Str(logPropertyURL, c.Request().URI().String()).
Send()
// count expect static or metrics
if !strings.Contains(c.Path(), "static") && !strings.Contains(c.Path(), "metrics") {
defer prometheusRequestTotal.Inc()
}
return c.Next()
})
app.All(config.baseURL+"/auth", func(c *fiber.Ctx) error {
configError := checkConfiguration(c)
if configError != nil {
return fiber.NewError(misconfigureStatus, "Configuration failed: "+configError.Error())
}
success := checkAuth(c, config, true)
if success {
c.Status(200)
return c.JSON("Authorized")
}
unAuthorizedStatus := getConfigUnauthorizedStatus(c)
c.Status(unAuthorizedStatus)
return c.JSON("Unauthorized")
})
app.Get(config.baseURL+"/challenge", func(c *fiber.Ctx) error {
configError := checkConfiguration(c)
if configError != nil {
return fiber.NewError(misconfigureStatus, "Configuration failed: "+configError.Error())
}
return httpChallenge(c, config)
})
app.Patch(config.baseURL+"/challenge", func(c *fiber.Ctx) error {
configError := checkConfiguration(c)
if configError != nil {
return fiber.NewError(misconfigureStatus, "Configuration failed: "+configError.Error())
}
return httpChallengePatch(c, config)
})
app.Post(config.baseURL+"/challenge", func(c *fiber.Ctx) error {
configError := checkConfiguration(c)
if configError != nil {
return fiber.NewError(misconfigureStatus, "Configuration failed: "+configError.Error())
}
return httpChallengePost(c, config)
})
// static serve
if !config.cdnStatic {
defer config.getLogger().Info().Str(logType, logTypeApp).Msg("disable cdn mode")
appConfig := filesystem.Config{
Next: func(c *fiber.Ctx) bool {
c.Set("Cache-Control", "public, max-age=14400")
return false
},
Root: http.FS(static),
}
app.Use(config.baseURL+"/challenge", filesystem.New(appConfig))
} else {
defer config.getLogger().Info().Str(logType, logTypeApp).Msg("enable cdn mode")
}
// 404
app.Use(func(c *fiber.Ctx) error {
defer prometheusRequestError.WithLabelValues("404").Inc()
ip, _ := getRequestIP(c)
defer config.getLogger().
Warn().
Str(logType, logTypeHTTPError).
Str(logPropertyIP, ip).
Str(logPropertyRequestID, c.Get(httpRequestHeaderRequestID, "")).
Str(logPropertyMethod, c.Method()).
Str(logPropertyURL, c.Request().URI().String()).
Int(logPropertyStatusCode, 404).
Send()
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
return c.Status(fiber.StatusNotFound).SendString("Not Found")
})
return app
}