-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticsvr.go
54 lines (46 loc) · 1.06 KB
/
staticsvr.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
// author: wongoo
// since: 2019-10-08
package main
import (
"fmt"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
"github.com/vogo/logger"
"os"
"strconv"
"time"
)
func main() {
port := 9999
staticDir, err := os.Getwd()
if err != nil {
logger.Fatal(err)
}
if len(os.Args) > 1 {
port, err = strconv.Atoi(os.Args[1])
if err != nil {
logger.Fatal(err)
}
}
if len(os.Args) > 2 {
staticDir = os.Args[2]
}
logger.Infof("serve static on port %d at directory %s", port, staticDir)
httpRouter := router.New()
fs := &fasthttp.FS{
Root: staticDir,
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: false,
AcceptByteRange: false,
CacheDuration: time.Minute,
}
httpRouter.GET("/*filepath", fs.NewRequestHandler())
server := &fasthttp.Server{
Handler: httpRouter.Handler,
ReadTimeout: 3600 * time.Second,
WriteTimeout: 3600 * time.Second,
MaxRequestBodySize: 1 << 20,
}
logger.Info(server.ListenAndServe(fmt.Sprintf(":%d", port)))
}