-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (104 loc) · 2.7 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
package main
import (
"database/sql"
"fmt"
"lommix/wichtelbot/server"
"lommix/wichtelbot/server/components"
"lommix/wichtelbot/server/store"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide a command.")
return
}
command := os.Args[1]
db, err := sql.Open("sqlite3", "wichtel.db")
if err != nil {
fmt.Println("Failed to open DB Connection: ", err)
return
}
switch command {
// -------------------------------------
// database commands
// -------------------------------------
case "init":
store.SchemaUp(db)
// -------------------------------------
// start dev server
// -------------------------------------
case "dev":
tmpl := &components.Templates{}
err = tmpl.Load()
if err != nil {
fmt.Println("Failed to load Templates: ", err)
return
}
snippets := &components.Snippets{}
err = snippets.Load()
if err != nil {
fmt.Println("Failed to load Snippets: ", err)
return
}
app := server.AppState{
Db: db,
Templates: tmpl,
Mode: server.Debug,
Snippets: snippets,
Sessions: &components.CookieJar{},
}
println("starting cleaner")
go app.CleanupRoutine()
println("starting listeneing on 3000")
app.RegisterHandler()
http.ListenAndServe(":3000", nil)
// -------------------------------------
// start production server in tls mode
// -------------------------------------
case "prod":
components.LoadEnv()
cert := os.Getenv("SSL_CERT")
key := os.Getenv("SSL_KEY")
http_port := os.Getenv("HTTP_PORT")
https_port := os.Getenv("HTTPS_PORT")
if cert == "" || key == "" || http_port == "" || https_port == "" {
fmt.Println("Please provide SSL_CERT, SSL_KEY, HTTP_PORT, HTTPS_PORT")
return
}
tmpl := &components.Templates{}
err = tmpl.Load()
if err != nil {
fmt.Println("Failed to load Templates: ", err)
return
}
snippets := &components.Snippets{}
err = snippets.Load()
if err != nil {
fmt.Println("Failed to load Snippets: ", err)
return
}
app := server.AppState{
Db: db,
Templates: tmpl,
Mode: server.Prod,
Snippets: snippets,
Sessions: &components.CookieJar{},
}
println("starting cleaner")
go app.CleanupRoutine()
println("starting http redirect")
go http.ListenAndServe(":"+http_port, http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, "https://"+request.Host, http.StatusMovedPermanently)
}))
println("starting https")
app.RegisterHandler()
err := http.ListenAndServeTLS(":"+https_port, cert, key, nil)
if err != nil {
fmt.Println(err)
}
default:
fmt.Println("Invalid command\nOptions are\n'init' 'dev' 'prod' ")
}
}