-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.go
61 lines (52 loc) · 1.58 KB
/
server.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
package main
import (
"flag"
"log"
"net/http"
"strings"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("h", "0.0.0.0:8080", "TCP address to listen to")
root = flag.String("d", "/usr/share/httpd", "Directory to serve static files from")
strip = flag.Int("s", 0, "Number of subdir to remove from the query")
compress = flag.Bool("c", false, "Enables transparent response compression if set to true")
)
var fsHandler fasthttp.RequestHandler
func reponseNotFound(ctx *fasthttp.RequestCtx) {
ctx.Response.SetStatusCode(http.StatusNotFound)
ctx.Response.SetBody([]byte("Not Found\n"))
}
func notFoundHandler(ctx *fasthttp.RequestCtx) {
if strings.HasSuffix(string(ctx.Request.RequestURI()), ".map") {
reponseNotFound(ctx)
return
}
ctx.Logger().Printf("File %s not found, defaulting to index.html", ctx.Path())
ctx.Request.SetRequestURI("/index.html")
fsHandler(ctx)
}
func createFsHandler() fasthttp.RequestHandler {
fs := &fasthttp.FS{
Root: *root,
Compress: *compress,
IndexNames: []string{"index.html"},
PathNotFound: notFoundHandler,
PathRewrite: fasthttp.NewPathSlashesStripper(*strip),
GenerateIndexPages: false,
AcceptByteRange: true,
}
return fs.NewRequestHandler()
}
func main() {
flag.Parse()
fsHandler = createFsHandler()
// Start HTTP server.
if len(*addr) > 0 {
log.Printf("Starting HTTP server on %q", *addr)
log.Printf("Serving files from directory %q", *root)
if err := fasthttp.ListenAndServe(*addr, fsHandler); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
}
}