Core

/app/controller/clib/static.go (1.1 KB)

 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
package clib

import (
"strings"

"github.com/valyala/fasthttp"

"{{{ .Package }}}/app/controller/cutil"
"{{{ .Package }}}/assets"
)

func Favicon(rc *fasthttp.RequestCtx) {
data, contentType, err := assets.EmbedAsset("favicon.ico")
assetResponse(rc, data, contentType, err)
}

func RobotsTxt(rc *fasthttp.RequestCtx) {
data, contentType, err := assets.EmbedAsset("robots.txt")
assetResponse(rc, data, contentType, err)
}

func Static(rc *fasthttp.RequestCtx) {
p := strings.TrimPrefix(string(rc.Request.URI().Path()), "/assets")
p = strings.TrimPrefix(p, "/")
if strings.Contains(p, "../") {
rc.Error("invalid path", fasthttp.StatusNotFound)
} else {
data, contentType, e := assets.EmbedAsset(p)
assetResponse(rc, data, contentType, e)
}
}

func assetResponse(rc *fasthttp.RequestCtx, data []byte, contentType string, err error) {
if err == nil {
rc.Response.Header.SetContentType(contentType)
rc.SetStatusCode(fasthttp.StatusOK)
cutil.WriteCORS(rc)
_, _ = rc.Write(data)
} else {
rc.Error(err.Error(), fasthttp.StatusNotFound)
}
}