-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (48 loc) · 1.23 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
package main
import (
"log"
"net/http"
"os"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"webserver/config"
"webserver/models"
app "webserver/server"
)
func main() {
// Loads the env variables
err := godotenv.Load()
if err != nil {
logrus.Warning("Cannot load .env file!")
}
config.InitDB()
// Check database connection
db := config.GetDatabaseConnection()
defer db.Close()
// Prints the version and the address of our api to the console
logrus.Info("Version is ", os.Getenv("PORT"))
logrus.Info("Starting Server on http://localhost:", os.Getenv("PORT"))
// Set log level
switch os.Getenv("LOG_LEVEL") {
case "debug":
logrus.SetLevel(logrus.ErrorLevel)
case "info":
logrus.SetLevel(logrus.ErrorLevel)
case "warn":
logrus.SetLevel(logrus.ErrorLevel)
default:
logrus.SetLevel(logrus.ErrorLevel)
}
// Creates the database schema
migrateDatabase()
// Server router on given port and attach the cors headers
server := app.NewServer()
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), server))
}
func migrateDatabase() {
db := config.GetDatabaseConnection()
// Migrate the given tables
db.AutoMigrate(&models.User{})
db.AutoMigrate(&models.Confession{})
}