-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (47 loc) · 1.23 KB
/
main.go
File metadata and controls
55 lines (47 loc) · 1.23 KB
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
/*
C0D3's Simple web server.
Version Alpha 0.0.2
*/
package main
import (
"flag"
"fmt"
"log"
"net/http"
"strings"
)
func main() {
rootDir := flag.String("root", "", "Content root directory.")
httpPort := flag.Int("port", 8080, "HTTP/HTTPS Server port.")
tls := flag.Bool("tls", false, "TLS Enable.")
pKey := flag.String("pkey", "", "TLS Private Key file path.")
cert := flag.String("cert", "", "TLS Public Cert file path.")
flag.Parse()
if strings.TrimSpace(*rootDir) == "" {
flag.Usage()
log.Fatal(fmt.Errorf("%v\n", "Root directory is required."))
}
if *tls {
if *pKey == "" || *cert == "" {
log.Fatalln("When TLS mode is enabled, Private Key and Public Cert are required.")
}
} else {
log.Println("TLS mode disabled, 'pkey' and 'cert' params will be ignored.")
}
serve(*httpPort, *rootDir, *tls, *pKey, *cert)
}
func serve(port int, dir string, tls bool, private, cert string) {
host := fmt.Sprintf(":%v", port)
log.Printf("Listen at %v, serving \"%v\"", host, dir)
if tls {
err := http.ListenAndServeTLS(host, cert, private, http.FileServer(http.Dir(dir)))
if err != nil {
panic(err)
}
} else {
err := http.ListenAndServe(host, http.FileServer(http.Dir(dir)))
if err != nil {
panic(err)
}
}
}