-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
151 lines (118 loc) · 3.62 KB
/
server.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package server
import (
"os"
"path/filepath"
"strings"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/static"
"github.com/tkdeng/goutil"
)
type ConfigData struct {
Title string
AppTitle string
Desc string
PublicURI string
Origins []string
Proxies []string
OriginErrHandler func(c fiber.Ctx, err error) error
PortHTTP uint16
PortSSL uint16
DebugMode bool
Root string
}
var Config = ConfigData{
Title: "Web Server",
AppTitle: "WebServer",
Desc: "A Web Server.",
PortHTTP: 8080,
PortSSL: 8443,
}
type App struct {
*fiber.App
}
// New loads a new server
func New(root string, config ...fiber.Config) (App, error) {
// load config file
loadConfig(root)
// compile src
compile()
if len(config) == 0 {
config = append(config, fiber.Config{
AppName: Config.AppTitle,
ServerHeader: Config.Title,
TrustProxyConfig: fiber.TrustProxyConfig{
Proxies: Config.Proxies,
},
TrustProxy: true,
EnableIPValidation: true,
})
} else {
config[0].AppName = Config.AppTitle
config[0].ServerHeader = Config.Title
if config[0].TrustProxyConfig.Proxies == nil {
config[0].TrustProxyConfig.Proxies = Config.Proxies
} else {
config[0].TrustProxyConfig.Proxies = append(config[0].TrustProxyConfig.Proxies, Config.Proxies...)
}
}
app := fiber.New(config[0])
if Config.OriginErrHandler == nil {
Config.OriginErrHandler = func(c fiber.Ctx, err error) error {
c.SendStatus(403)
return c.SendString(err.Error())
}
}
// enforce specific domain and ip origins
app.Use(VerifyOrigin(Config.Origins, Config.Proxies, Config.OriginErrHandler))
// auto redirect http to https
if Config.PortSSL != 0 {
app.Use(RedirectSSL(Config.PortHTTP, Config.PortSSL))
}
compressAssets := !Config.DebugMode
app.Get("/theme/*", static.New(Config.Root+"/theme", static.Config{Compress: compressAssets}))
app.Get("/assets/wasm/*", static.New(Config.Root+"/wasm.dist", static.Config{Compress: compressAssets}))
app.Get("/assets/*", static.New(Config.Root+"/assets", static.Config{Compress: compressAssets}))
if Config.PublicURI != "" {
app.Get(Config.PublicURI, static.New(Config.Root+"/public", static.Config{Compress: compressAssets, Browse: true}))
}
app.Use("/404", func(c fiber.Ctx) error {
url := goutil.Clean(c.Path())
if url != "/404" {
return c.Next()
}
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
c.SendStatus(404)
if buf, err := os.ReadFile(Config.Root + "/pages.dist/404/index.html"); err == nil {
return c.Send(buf)
}
return c.Send([]byte("<h1>Error 404</h1><h2>Page Not Found!</h2>"))
})
app.Use("/*", static.New(Config.Root+"/pages.dist", static.Config{Compress: compressAssets}))
return App{app}, nil
}
// Listen to both http and https ports and
// auto generate a self signed ssl certificate
// (will also auto renew every year)
//
// by using self signed certs, you can use a proxy like cloudflare and
// not have to worry about verifying a certificate athority like lets encrypt
func (app *App) Listen() error {
app.Use(func(c fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
c.SendStatus(404)
if buf, err := os.ReadFile(Config.Root + "/pages.dist/404/index.html"); err == nil {
return c.Send(buf)
}
return c.Send([]byte("<h1>Error 404</h1><h2>Page Not Found!</h2>"))
})
return ListenAutoTLS(app.App, Config.PortHTTP, Config.PortSSL, Config.Root+"/db/ssl/auto_ssl")
}
func loadConfig(root string) {
// load config file
if path, err := filepath.Abs(root); err == nil {
root = path
}
root = strings.TrimSuffix(root, "/")
goutil.ReadConfig(root+"/config.yml", &Config)
Config.Root = root
}