-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (207 loc) · 4.79 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/joho/godotenv"
"brucosijada.kset.org/app/models"
"brucosijada.kset.org/app/providers/database"
"brucosijada.kset.org/app/providers/hash"
"brucosijada.kset.org/app/providers/minio"
"brucosijada.kset.org/app/providers/requestTimer"
"brucosijada.kset.org/app/providers/session"
"brucosijada.kset.org/app/providers/viewEngine"
"brucosijada.kset.org/app/repo"
"brucosijada.kset.org/app/routes"
"brucosijada.kset.org/app/util/async"
"brucosijada.kset.org/app/version"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/helmet/v2"
flag "github.com/spf13/pflag"
)
//go:embed assets/*
var assets embed.FS
//go:embed views/*
var views embed.FS
const defaultPort = 3000
const defaultHost = "0.0.0.0"
type appConfig struct {
Port int
Host string
}
func loadConfig() appConfig {
envPort, err := strconv.ParseInt(os.Getenv("PORT"), 0, 32)
if err != nil || envPort == 0 {
envPort = defaultPort
}
envHost := os.Getenv("HOST")
if envHost == "" {
envHost = defaultHost
}
var port int
var host string
flag.IntVarP(&port, "port", "p", int(envPort), "Set the port on which the server will run")
flag.StringVarP(&host, "host", "h", envHost, "Set the host to which the server will bind")
flag.Parse()
return appConfig{
Port: port,
Host: host,
}
}
func cspConfig() string {
cspEntries := map[string][]string{
"default-src": {
"'self'",
},
"img-src": {
"'self'",
},
"media-src": {
"'self'",
},
"style-src": {
"'self'",
"fonts.googleapis.com",
"'unsafe-inline'",
},
"font-src": {
"'self'",
"fonts.gstatic.com",
},
"script-src": {
"'unsafe-inline'",
},
}
cspItems := make([]string, 0, len(cspEntries))
for k, v := range cspEntries {
cspItems = append(
cspItems,
fmt.Sprintf(
"%s %s",
k,
strings.Join(v, " "),
),
)
}
return strings.Join(cspItems, "; ")
}
func main() {
if err := godotenv.Load(); err != nil {
log.Println("Error loading .env file: ", err)
}
conf := loadConfig()
_, err := async.Async().RunInParallel(
func() (interface{}, error) {
return nil, hash.HashProvider().CalibrateMemoryParam(time.Second)
},
func() (interface{}, error) {
return nil, database.DatabaseProvider().Register()
},
func() (interface{}, error) {
return nil, session.SessionProvider().Register()
},
func() (interface{}, error) {
return nil, minio.MinioProvider().Register()
},
).All()
if err != nil {
panic(err)
}
err = func() (err error) {
db := database.DatabaseProvider().Client()
var users int64
err = db.Model(models.User{}).Count(&users).Error
if err != nil {
return
}
if users == 0 {
_, err = repo.User().Create(
repo.UserCreateOptions{
Email: os.Getenv("INITIAL_USER_MAIL"),
Identity: os.Getenv("INITIAL_USER_USER"),
Password: os.Getenv("INITIAL_USER_PASS"),
Invitation: nil,
},
)
}
return
}()
if err != nil {
panic(err)
}
viewsFolder, _ := fs.Sub(views, "views")
app := fiber.New(
fiber.Config{
DisableKeepalive: true,
ReadTimeout: 10 * time.Second,
ServerHeader: "Microsoft-IIS/7.0",
AppName: fmt.Sprintf("Brucifer %d.", version.BuildTime().Year()),
Views: viewEngine.Init(viewsFolder),
BodyLimit: 200 * 1024 * 1024 * 1,
Prefork: false,
},
)
// println(db)
app.Use(recover.New())
app.Use(etag.New())
app.Use(
logger.New(
logger.Config{
Format: "[${time}] ${method} ${path}\t| ${status} ${latency}\t| ${ua}\t| ${ips} \n",
},
),
)
app.Use(
func(c *fiber.Ctx) error {
timer := requestTimer.New()
c.Locals("timer", timer)
timer.Start("app")
err := c.Next()
timer.End("app")
c.Append("Server-Timing", timer.String())
return err
},
)
app.Use(
helmet.New(
helmet.Config{
ReferrerPolicy: "no-referrer-when-downgrade",
ContentSecurityPolicy: cspConfig(),
},
),
)
app.Get(
"/favicon.ico",
func(c *fiber.Ctx) error {
c.Type("ico")
c.Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int((7*24*time.Hour).Seconds())))
favicon, _ := assets.ReadFile("assets/images/favicon.ico")
return c.Send(favicon)
},
)
assetsFs, _ := fs.Sub(assets, "assets")
app.Use(
"/assets",
func(ctx *fiber.Ctx) error {
ctx.Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int((365*24*time.Hour).Seconds())))
return ctx.Next()
},
filesystem.New(
filesystem.Config{
Root: http.FS(assetsFs),
},
),
)
routes.RegisterRoutes(app)
log.Fatal(app.Listen(fmt.Sprintf("%s:%d", conf.Host, conf.Port)))
}