-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
91 lines (76 loc) · 2.05 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"io/ioutil"
"path"
"strings"
"github.com/gramework/gramework"
"github.com/spf13/pflag"
)
var (
indexPath = pflag.StringP("indexpath", "i", "./index.html", "index file path")
staticPath = pflag.StringP("staticpath", "s", "./", "static directory path")
staticRoute = pflag.StringP("staticroute", "r", "/static", "static route")
bind = pflag.StringP("bind", "b", ":80", "port to listen")
cache = pflag.BoolP("cache", "c", false, "enable cache")
tls = pflag.BoolP("tls", "t", false, "enable TLS via letsencrypt")
dev = pflag.BoolP(
"dev",
"d",
false,
"enables the dev mode for TLS cert generation,\nwhich allows you to use self-signed certs on localhost",
)
)
func serveDir(app *gramework.App, sp, sr string, slashCnt int) {
h := app.ServeDirNoCacheCustom(sp, slashCnt, true, false, []string{"index.html"})
if *cache {
h = app.ServeDirCustom(sp, slashCnt, true, false, []string{"index.html"})
}
app.GET(path.Join(sr, "/*any"), h)
}
func regStaticHandlers(app *gramework.App, sp string, slashCnt int) {
files, err := ioutil.ReadDir(sp)
if err != nil {
app.Logger.WithError(err).WithField("path", sp).Fatal("static directory does not exist")
}
for _, file := range files {
p := path.Clean(path.Join(sp, file.Name()))
r := "/" + strings.TrimLeft(file.Name(), "/")
if strings.Contains(file.Name(), ".fasthttp.gz") || path.Clean(*indexPath) == p {
continue
}
if file.IsDir() {
serveDir(app, p, r, slashCnt)
} else {
app.ServeFile(r, p)
}
}
}
func main() {
pflag.Parse()
gramework.DisableFlags()
app := gramework.New()
app.TLSEmails = []string{
}
app.SPAIndex(*indexPath)
sp := path.Clean(*staticPath)
sr := path.Clean(*staticRoute)
slashCnt := strings.Count(sr, "/")
if len(*staticRoute) == 0 {
sr = "/"
}
if sr == "/" {
regStaticHandlers(app, sp, slashCnt)
} else {
serveDir(app, sp, sr, slashCnt)
}
if *tls {
if *dev {
app.ListenAndServeAllDev(*bind)
return
}
app.ListenAndServeAll()
return
}
app.ListenAndServe(*bind)
}