Files
old-backend/main.go
2023-09-04 11:19:59 +02:00

60 lines
1.2 KiB
Go

package main
import (
"fmt"
"net/http"
"novatech/models"
"github.com/VoidArtanis/go-rest-boilerplate/routes"
"github.com/VoidArtanis/go-rest-boilerplate/shared"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
)
var DB *gorm.DB
func main() {
// Initialize the database connection
DB, err := gorm.Open("postgres", "host=localhost user=postgres dbname=postgres sslmode=disable password=root")
if err != nil {
fmt.Println("Error opening database:", err)
return
}
defer DB.Close()
// AutoMigrate the User model
DB.AutoMigrate(&models.User{})
// Initialize Admin interface
Admin := admin.New(&admin.AdminConfig{DB: DB})
// Allow Admin to manage User resource
Admin.AddResource(&models.User{})
// Initialize HTTP request multiplexer
mux := http.NewServeMux()
// Mount admin interface to mux
Admin.MountTo("/admin", mux)
// Start the admin server in a separate goroutine
go func() {
fmt.Println("Admin server listening on :9000")
http.ListenAndServe(":9000", mux)
}()
// Db Connect and Close
shared.Init()
defer shared.CloseDb()
// Initialize Gin
r := gin.Default()
routes.InitRouter(r)
// Start the Gin server on another port
fmt.Println("Application server listening on :8080")
r.Run(":8080")
}