-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (34 loc) · 901 Bytes
/
main.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
package main
import (
"flag"
"fmt"
"os"
"github.com/valyala/fasthttp"
)
func main() {
port := 80
flag.IntVar(&port, "p", port, "define port - shorthand")
flag.IntVar(&port, "port", port, "define port")
dir, err := os.Getwd()
if err != nil {
dir = "."
}
flag.StringVar(&dir, "d", dir, "define file server directory - shorthand")
flag.StringVar(&dir, "dir", dir, "define file server directory")
flag.Parse()
fs := &fasthttp.FS{
Root: dir,
AcceptByteRange: true,
GenerateIndexPages: true,
}
fsHandler := fs.NewRequestHandler()
fmt.Printf("Starting File server on http://localhost:%v \n", port)
fmt.Printf("Serving files from directory: '%s'\n", dir)
if err := fasthttp.ListenAndServe(
fmt.Sprintf(":%v", port),
func(ctx *fasthttp.RequestCtx) { fsHandler(ctx) },
); err != nil {
fmt.Printf("error in ListenAndServe: %v\n", err)
os.Exit(1)
}
}