-
Notifications
You must be signed in to change notification settings - Fork 1
/
static.go
52 lines (44 loc) · 1.06 KB
/
static.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
package server
import (
"github.com/cjp2600/assr/config"
"github.com/valyala/fasthttp"
"strings"
)
type Static struct {
}
func NewStatic() *Static {
return &Static{}
}
func (s *Static) Run(dir string) error {
fs := &fasthttp.FS{
Root: dir,
IndexNames: []string{config.GetIndexName()},
GenerateIndexPages: true,
Compress: config.IsCompress(),
AcceptByteRange: true,
}
fsHandler := fs.NewRequestHandler()
requestHandler := func(ctx *fasthttp.RequestCtx) {
ctx.SetUserValue("current", string(ctx.Path()))
path := string(ctx.Path())
// static proxy
for postfix, contentType := range StaticPostfix {
if strings.Contains(strings.ToLower(path), postfix) {
ctx.SetContentType(contentType)
fsHandler(ctx)
return
}
}
ctx.URI().SetPath("/")
switch string(ctx.Path()) {
default:
ctx.SetContentType("text/html")
fsHandler(ctx)
}
}
// ReadBufferSize: 100 * 1024 * 1024 * 1024,
serv := fasthttp.Server{
Handler: requestHandler,
}
return serv.ListenAndServe(":" + config.GetStaticPort())
}