/
main.go
64 lines (57 loc) · 1.64 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"flag"
"fmt"
"net/http"
"os"
"github.com/bradfitz/http2"
)
var (
dir string
port string
crtFile string
keyFile string
)
func init() {
d, err := os.Getwd()
if err != nil {
fmt.Printf("Error: %s\n", err)
}
flag.StringVar(&dir, "d", d, "The directory that should be served.")
flag.StringVar(&port, "p", ":8888", "The port/interface the server should work on.")
flag.StringVar(&crtFile, "c", "", "The certificate file for HTTPS connections.")
flag.StringVar(&keyFile, "k", "", "The private key file for HTTPS connections.")
flag.Parse()
}
func main() {
fmt.Printf("Starting nano server...\nServing directory: %s\n", dir)
if crtFile != "" && keyFile != "" {
fmt.Printf("Using certificate: %s\nUsing private key: %s\n", crtFile, keyFile)
} else {
fmt.Println("No certificate and/or key provided!")
}
srv := &http.Server{
Addr: port,
Handler: &requestLogger{http.FileServer(http.Dir(dir))},
}
http2.ConfigureServer(srv, nil)
if crtFile != "" && keyFile != "" {
fmt.Printf("Starting HTTPS server on port %s... Kill with [Ctrl]-[C]!\n", port)
if err := srv.ListenAndServeTLS(crtFile, keyFile); err != nil {
fmt.Printf("Error: %s\n", err)
}
} else {
fmt.Printf("Starting HTTP server on port %s... Kill with [Ctrl]-[C]!\n", port)
if err := srv.ListenAndServe(); err != nil {
fmt.Printf("Error: %s\n", err)
}
}
}
type requestLogger struct {
h http.Handler
}
func (l *requestLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("\"%s\" --> \"%s\" started\n", r.RemoteAddr, r.URL.Path)
l.h.ServeHTTP(w, r)
fmt.Printf("\"%s\" --> \"%s\" finished\n", r.RemoteAddr, r.URL.Path)
}