-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
49 lines (42 loc) · 1.3 KB
/
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
43
44
45
46
47
48
49
// Copyright 2017 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// serve-dir serves a directory over HTTP and logs the request to stderr.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/maruel/serve-dir/loghttp"
)
func getWd() string {
wd, _ := os.Getwd()
return wd
}
func main() {
// TODO(maruel): Change to -http, so it can bind to localhost.
port := flag.Int("port", 8010, "port number")
quiet := flag.Bool("q", false, "don't print log lines")
rootDir := flag.String("root", getWd(), "root directory")
timeout := flag.Int("timeout", 24*60*60, "write timeout in seconds; default 24h")
maxHdrSize := flag.Int("max_size", http.DefaultMaxHeaderBytes, "max header transfer size")
log.SetFlags(log.Lmicroseconds)
flag.Parse()
h := http.FileServer(http.Dir(*rootDir))
if !*quiet {
h = &loghttp.Handler{Handler: h}
}
s := &http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: h,
// read timeout is always 10s, since it should be GETs only.
ReadTimeout: 10. * time.Second,
WriteTimeout: time.Duration(*timeout) * time.Second,
MaxHeaderBytes: *maxHdrSize,
}
log.Printf("Serving %s on port %d", *rootDir, *port)
log.Fatal(s.ListenAndServe())
}