-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
58 lines (46 loc) · 1.31 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
package main
import (
"log"
"net/http"
"os"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"github.com/w3tecch/go-api-boilerplate/app"
"github.com/w3tecch/go-api-boilerplate/app/config"
"github.com/w3tecch/go-api-boilerplate/app/models"
)
func main() {
// Loads the env variables
err := godotenv.Load()
if err != nil {
logrus.Fatal("Error loading .env file")
}
// 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("API_VERSION"))
logrus.Info("Starting Server on http://localhost:", os.Getenv("API_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("API_PORT"), server))
}
func migrateDatabase() {
db := config.GetDatabaseConnection()
// Migrate the given tables
db.AutoMigrate(&models.User{})
}