-
Notifications
You must be signed in to change notification settings - Fork 4
/
router.go
56 lines (47 loc) · 1.52 KB
/
router.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
// Package cutil - Content managed by Project Forge, see [projectforge.md] for details.
package cutil
import (
"net/http"
"net/url"
"slices"
"github.com/CAFxX/httpcompression"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"projectforge.dev/projectforge/app/lib/telemetry/httpmetrics"
"projectforge.dev/projectforge/app/util"
)
var AppRoutesList = map[string][]string{}
func WireRouter(r *mux.Router, notFound http.HandlerFunc, logger util.Logger) (http.Handler, error) {
p := httpmetrics.NewMetrics(util.AppKey, logger)
r.Handle(p.MetricsPath, promhttp.Handler()).Methods(http.MethodGet)
r.PathPrefix("/").HandlerFunc(notFound)
var ret http.Handler = p.WrapHandler(r)
includeCompression := util.GetEnvBool("compression_enabled", false)
if includeCompression {
compressedTypes := []string{
"application/gzip", "application/octet-stream", "application/zip",
"audio/aac", "audio/mpeg", "audio/ogg",
"image/gif", "image/jpeg", "image/png", "image/webp",
"video/mpeg", "video/mp4", "video/webm",
}
compress, err := httpcompression.DefaultAdapter(httpcompression.ContentTypes(compressedTypes, true))
if err != nil {
return nil, err
}
ret = compress(ret)
}
return ret, nil
}
func AddRoute(method string, path string) {
curr := AppRoutesList[method]
if !slices.Contains(curr, path) {
curr = append(curr, path)
slices.Sort(curr)
AppRoutesList[method] = curr
}
}
func URLAddQuery(u *url.URL, k string, v string) {
q := u.Query()
q.Set(k, v)
u.RawQuery = q.Encode()
}