-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_assets.go
124 lines (106 loc) · 2.81 KB
/
http_assets.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
package main
import (
_ "embed"
"strings"
"time"
"github.com/gofiber/fiber/v2"
)
// ineffassign: ignore
//
//go:embed embed/build/a.js
var embedADotJS []byte
// ineffassign: ignore
//
//go:embed embed/build/a.src.js
var embedASrcDotJS []byte
// ineffassign: ignore
//
//go:embed embed/build/l.js
var embedLDotJS []byte
// ineffassign: ignore
//
//go:embed embed/build/l.src.js
var embedLSrcDotJS []byte
// ineffassign: ignore
//
//go:embed embed/build/amp.json
var embedAmpDotJSON []byte
func httpAppAssetsHelper(c *fiber.Ctx) *errorMessage {
if strings.Contains(c.Request().URI().String(), "?") {
return &errorQueryStringDisabled
}
version := c.Params("version")
date, dateErr := time.Parse("20060102", version)
if dateErr != nil {
return &errorAssetsVersionFailed
}
min := time.Now().AddDate(0, 0, -6)
max := time.Now().AddDate(0, 0, 3)
if date.Before(min) || date.After(max) {
return &errorAssetsVersionFailed
}
return nil
}
func httpAppAssets(
app *fiber.App,
conf *config,
) {
embedADotJS = replaceCollectorURL(embedADotJS, conf.collectorURL)
app.Get("/_/:version/a.js", func(c *fiber.Ctx) error {
e := httpAppAssetsHelper(c)
if e != nil {
return httpErrorResponse(c, *e)
}
staticCache(c)
c.Set(fiber.HeaderContentType, mimetypeJS)
return c.Send(embedADotJS)
})
embedASrcDotJS = replaceCollectorURL(embedASrcDotJS, conf.collectorURL)
app.Get("/_/:version/a.src.js", func(c *fiber.Ctx) error {
e := httpAppAssetsHelper(c)
if e != nil {
return httpErrorResponse(c, *e)
}
staticCache(c)
c.Set(fiber.HeaderContentType, mimetypeJS)
return c.Send(embedASrcDotJS)
})
embedLDotJS = replaceCollectorURL(embedLDotJS, conf.collectorURL)
app.Get("/_/:version/l.js", func(c *fiber.Ctx) error {
e := httpAppAssetsHelper(c)
if e != nil {
return httpErrorResponse(c, *e)
}
staticCache(c)
c.Set(fiber.HeaderContentType, mimetypeJS)
return c.Send(embedLDotJS)
})
embedLSrcDotJS = replaceCollectorURL(embedLSrcDotJS, conf.collectorURL)
app.Get("/_/:version/l.src.js", func(c *fiber.Ctx) error {
e := httpAppAssetsHelper(c)
if e != nil {
return httpErrorResponse(c, *e)
}
staticCache(c)
c.Set(fiber.HeaderContentType, mimetypeJS)
return c.Send(embedLSrcDotJS)
})
embedAmpDotJSON = replaceCollectorURL(embedAmpDotJSON, conf.collectorURL)
app.Get("/amp.json", func(c *fiber.Ctx) error {
if strings.Contains(c.Request().URI().String(), "?") {
return httpErrorResponse(c, errorQueryStringDisabled)
}
staticCacheLimit(c, conf.staticCacheTTL)
c.Set(fiber.HeaderContentType, mimetypeJSON)
return c.Send(embedAmpDotJSON)
})
app.Get("/_/:version/amp.json", func(c *fiber.Ctx) error {
e := httpAppAssetsHelper(c)
if e != nil {
return httpErrorResponse(c, *e)
}
staticCache(c)
c.Set(fiber.HeaderContentType, mimetypeJSON)
return c.Send(embedAmpDotJSON)
})
}