-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (47 loc) · 1.27 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
package main
import (
"flag"
"github.com/chiefsend/api/background"
"github.com/chiefsend/api/controllers"
m "github.com/chiefsend/api/models"
"github.com/joho/godotenv"
"log"
"os"
"path/filepath"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
{
autoMigrate := flag.Bool("auto-migrate", false, "pass -auto-migrate=true if you want gorm to auto migrate the database")
flag.Parse()
if *autoMigrate {
// set/test database connection
db, err := m.GetDatabase()
if err != nil || db == nil {
log.Fatal("Cannot connect database")
}
// Migrate the schema (temporary)
if err := db.AutoMigrate(&m.Share{}); err != nil {
log.Fatal("Cannot migrate database")
}
if err := db.AutoMigrate(&m.Attachment{}); err != nil {
log.Fatal("Cannot migrate database")
}
}
}
// check if file structure is there
if err := os.MkdirAll(filepath.Join(os.Getenv("MEDIA_DIR"), "temp"), os.ModePerm); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(os.Getenv("MEDIA_DIR"), "data"), os.ModePerm); err != nil {
log.Fatal(err)
}
// start the server(s)
log.Print("starting background workers ...")
go background.StartBackgroundWorkers()
log.Print("starting server ...")
controllers.StartServer()
}