-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
52 lines (43 loc) · 1.06 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
)
func main() {
var (
host string
port int
dir string
ssl bool
cert string
key string
)
flag.StringVar(&host, "host", "localhost", "host to listen on")
flag.IntVar(&port, "port", 8080, "port to listen on")
flag.StringVar(&dir, "dir", ".", "directory to serve")
flag.BoolVar(&ssl, "ssl", false, "use ssl")
flag.StringVar(&cert, "cert", "", "path to ssl certificate, requires -ssl")
flag.StringVar(&key, "key", "", "path to ssl key, requires -ssl")
flag.Parse()
if ssl && (cert == "" || key == "") {
log.Println("Error: Specify ssl cert and key paths")
flag.PrintDefaults()
os.Exit(1)
}
if !ssl && (cert != "" || key != "") {
log.Println("Error: Using -cert or -key requires -ssl")
flag.PrintDefaults()
os.Exit(1)
}
http.Handle("/", http.FileServer(http.Dir(dir)))
addr := fmt.Sprintf("%s:%d", host, port)
log.Printf("Listening on %s...", addr)
if ssl {
log.Fatal(http.ListenAndServeTLS(addr, cert, key, nil))
} else {
log.Fatal(http.ListenAndServe(addr, nil))
}
}