-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (54 loc) · 1.4 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
package main
import (
"context"
"log"
"os"
"spiritchat/auth"
"spiritchat/config"
"spiritchat/data"
"spiritchat/serve"
)
func isMigration() bool {
return len(os.Args) > 2 && os.Args[1] == "migrate" && (os.Args[2] == "up" || os.Args[2] == "down")
}
// true = up false = down
func getMigrationType() bool {
return os.Args[2] == "up"
}
func main() {
conf := config.ParseEnv()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Println("Establishing database connection")
store, err := data.NewDatastore(ctx, conf.PGURL, 15)
if err != nil {
log.Fatalf("Failed to initalize database: %+v", err)
return
}
defer store.Cleanup(ctx)
if isMigration() {
migrationType := getMigrationType()
if migrationType {
log.Println("Migrating up")
} else {
log.Println("Migrating down")
}
err := store.Migrate(ctx, migrationType)
if err != nil {
log.Fatal(err)
}
} else {
log.Println("Establishing OAuth API")
auth, err := auth.NewOAuth(ctx, conf.AuthConfig)
if err != nil {
log.Fatalf("Failed to initialize OAuth API: %+v", err)
return
}
server := serve.NewServer(store, auth, serve.ServerOptions{
Address: conf.HTTPAddress,
CorsOriginAllow: conf.CORSAllow,
})
log.Printf("Starting server on %s, allowing %s CORS", conf.HTTPAddress, conf.CORSAllow)
log.Println(server.Listen(ctx))
}
}