forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileserver.go
58 lines (49 loc) · 1.81 KB
/
fileserver.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
// Example static file server. Serves static files from the given directory.
package main
import (
"flag"
"log"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
addrTLS = flag.String("addrTLS", "", "TCP address to listen to TLS (aka SSL or HTTPS) requests. Leave empty for disabling TLS")
byteRange = flag.Bool("byteRange", false, "Enables byte range requests if set to true")
certFile = flag.String("certFile", "./ssl-cert-snakeoil.pem", "Path to TLS certificate file")
compress = flag.Bool("compress", false, "Enables transparent response compression if set to true")
dir = flag.String("dir", "/usr/share/nginx/html", "Directory to serve static files from")
generateIndexPages = flag.Bool("generateIndexPages", true, "Whether to generate directory index pages")
keyFile = flag.String("keyFile", "./ssl-cert-snakeoil.key", "Path to TLS key file")
)
func main() {
flag.Parse()
fs := &fasthttp.FS{
Root: *dir,
IndexNames: []string{"index.html"},
GenerateIndexPages: *generateIndexPages,
Compress: *compress,
AcceptByteRange: *byteRange,
}
h := fs.NewRequestHandler()
// Start HTTP server.
if len(*addr) > 0 {
log.Printf("Starting HTTP server on %q", *addr)
go func() {
if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("error in ListenAndServe: %s", err)
}
}()
}
// Start HTTPS server.
if len(*addrTLS) > 0 {
log.Printf("Starting HTTPS server on %q", *addrTLS)
go func() {
if err := fasthttp.ListenAndServeTLS(*addrTLS, *certFile, *keyFile, h); err != nil {
log.Fatalf("error in ListenAndServeTLS: %s", err)
}
}()
}
log.Printf("Serving files from directory %q", *dir)
// Wait forever.
select {}
}