alai-server/backend/main.go

74 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-06-29 21:26:05 -04:00
package main
import (
"fmt"
"os"
2022-07-22 23:00:31 -04:00
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/database"
"git.cromer.cl/Proyecto-Titulo/alai-server/backend/routes"
2022-06-29 21:26:05 -04:00
"github.com/joho/godotenv"
"gorm.io/gorm"
)
func main() {
err := godotenv.Load()
if err != nil {
print(err.Error())
}
allArgs := os.Args[1:]
dropDB := false
migrateDB := false
serve := false
dbOperation := false
for _, element := range allArgs {
if element == "drop" {
dropDB = true
dbOperation = true
}
if element == "migrate" {
migrateDB = true
dbOperation = true
}
if element == "serve" {
serve = true
}
}
var gdb *gorm.DB
if dbOperation {
fmt.Print("Connecting to database ... ")
gdb = database.Connect()
fmt.Println("DONE")
}
if dropDB {
fmt.Print("Dropping database ... ")
database.DropAll(gdb)
fmt.Println("DONE")
}
if migrateDB {
fmt.Print("AutoMigrating database ... ")
database.AutoMigrate(gdb)
fmt.Println("DONE")
}
if dbOperation {
fmt.Print("Closing database ... ")
database.Close(gdb)
fmt.Println("DONE")
}
if serve {
router := routes.Initialize()
fmt.Println("Serving routes ... DONE")
routes.Serve(router)
}
}