Setup boilerplate gorm , and admin part

This commit is contained in:
Nedim
2023-09-04 11:13:21 +02:00
parent eb4303bb08
commit 62de1401e5
17 changed files with 698 additions and 86 deletions

59
main.go Normal file
View File

@@ -0,0 +1,59 @@
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")
}