-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
93 lines (73 loc) · 1.96 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
package main
import (
"appconf"
"appsec"
"embedfiles"
"filefunc"
"initializers"
"log"
"os"
"routers"
)
var WD string
var https bool
func init() {
WD = getWD()
configFile := WD + "/.app" // check for .app file
if !filefunc.IsExists(configFile) {
log.Println("No .app file found. Creating one...")
appconf.WriteDefaultConfig(WD)
}
appconf.ReadConfig() // read the .app file
// Create the http.FileSystem using the embedded files
embedfiles.GetWebFS()
embedfiles.EmbedFilesToDisk()
err := embedfiles.EmbedFiles()
if err != nil {
log.Fatal(err)
}
dataFolder := appconf.GetVal("root_folder") + "/data"
if !filefunc.IsExists(dataFolder) {
log.Println("No data folder found. Creating one...")
filefunc.CreateFolder(dataFolder)
filefunc.CreateFile(appconf.GetVal("db_path"))
}
dbpath := appconf.GetVal("db_path")
initializers.ConnectToDB(dbpath)
initializers.SyncDB()
certFile := appconf.GetVal("root_folder") + "/app.crt"
keyFile := appconf.GetVal("root_folder") + "/app.key"
if !filefunc.IsExists(certFile) || !filefunc.IsExists(keyFile) {
log.Println("No RSA files found. Creating key pair ...")
err := appsec.GenerateTLS(keyFile, certFile, "2048")
if err != nil {
log.Fatal(err)
}
}
https = appconf.GetVal("https") == "true"
}
func getWD() string {
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
// fmt.Printf("WorkDir: %s", wd)
return wd
}
func main() {
// set new JWT_SECRET environment variable if in release mode
err := os.Setenv("JWT_SECRET", appsec.GetSecret())
if err != nil {
log.Fatal(err)
}
r := routers.SetupRouter()
if https {
certFile := appconf.GetVal("root_folder") + "/app.crt"
keyFile := appconf.GetVal("root_folder") + "/app.key"
log.Println("Starting HTTPS server on port " + appconf.GetVal("port"))
r.RunTLS(":"+appconf.GetVal("port"), certFile, keyFile)
} else {
log.Println("Starting HTTP server on port " + appconf.GetVal("port"))
r.Run(":" + appconf.GetVal("port"))
}
}