forked from azurity/go-onefile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (66 loc) · 1.62 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
65
66
67
68
69
70
71
72
73
74
package main
import (
"flag"
"fmt"
"log"
"multifile/utils"
"net/http"
"os"
)
const home = "static"
const resources = "resources"
func FileCheckCreate(filename string) {
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(filename, os.FileMode(0755))
if err != nil {
log.Panicf("make %s dir error\n%s\n", filename, err)
return
}
} else {
log.Panicf("create %s dir filed\n%s\n", filename, err)
return
}
}
}
func main() {
SSL := flag.Bool("SSL", false, "open SSL or not, default is true")
Port := flag.Uint("Port", 0, "port to be used, default is 443")
init := flag.Bool("init", false, "init the environment")
flag.Parse()
if *init && (*Port != 0 || *SSL != false) {
log.Println("[X]Init don't need other agreements")
flag.PrintDefaults()
return
} else if *init {
FileCheckCreate(home)
FileCheckCreate(resources)
return
}
_, crtErr := os.Stat("resources/https.crt")
_, keyErr := os.Stat("resources/https.key")
root := utils.NewRoot(os.DirFS(home), "404.html", "index.html")
withGzipped := utils.Gzip(root)
if *SSL && (crtErr == nil && keyErr == nil) {
if *Port == 0 {
*Port = 443
}
if *Port == 443 {
go func() {
err := http.ListenAndServe(":80", http.HandlerFunc(utils.Redirect))
if err != nil {
log.Println(err)
}
}()
}
portString := fmt.Sprintf(":%d", *Port)
log.Fatal(http.ListenAndServeTLS(portString, "resources/https.crt", "resources/https.key", withGzipped))
} else {
if *Port == 0 {
*Port = 80
}
portString := fmt.Sprintf(":%d", *Port)
log.Fatal(http.ListenAndServe(portString, withGzipped))
}
}