Refactor handlers into separate packages and implement dependency injection.
IDK if this shit is good or even logical, but it works and I can wrap my head around it.
This commit is contained in:
@@ -53,7 +53,7 @@ func main() {
|
||||
|
||||
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
||||
engine := gin.Default()
|
||||
r := server.SetupRouter(engine)
|
||||
r := server.SetupRouter(engine, appContext)
|
||||
err := r.Run(":8080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@@ -1,12 +1,11 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
// "github.com/gin-gonic/gin"
|
||||
// "gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AppWebContext struct {
|
||||
Db *gorm.DB
|
||||
GinEngine *gin.Engine
|
||||
|
||||
}
|
||||
// type AppWebContext struct {
|
||||
// Db *gorm.DB
|
||||
// GinEngine *gin.Engine
|
||||
// }
|
35
internal/handlers/admin/adminDash.go
Normal file
35
internal/handlers/admin/adminDash.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handlers_admin
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AdminDashboardHandler struct {
|
||||
AppContext *internal.AppContext
|
||||
}
|
||||
|
||||
func NewAdminDashboardHandler(appContext *internal.AppContext) *AdminDashboardHandler {
|
||||
return &AdminDashboardHandler{
|
||||
AppContext: appContext,
|
||||
}
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) AdminHomePage(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "AdminDashboardHomePage",
|
||||
})
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) ApproveMedia(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "ApproveMedia",
|
||||
})
|
||||
}
|
||||
|
||||
func (appContext *AdminDashboardHandler) RejectMedia(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "RejectMedia",
|
||||
})
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func AdminDashboardHandler (c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "AdminDashboard",
|
||||
})
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func ApproveMediaHandler (c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "ApproveMedia",
|
||||
})
|
||||
}
|
23
internal/handlers/home/mainPage.go
Normal file
23
internal/handlers/home/mainPage.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package handlers_home
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type MainPageHandler struct{
|
||||
AppContext *internal.AppContext
|
||||
}
|
||||
|
||||
func NewMainPageHandler(appContext *internal.AppContext) *MainPageHandler {
|
||||
return &MainPageHandler{
|
||||
AppContext: appContext,
|
||||
}
|
||||
}
|
||||
|
||||
func (appContext *MainPageHandler) HomePageHandler(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "HomePage",
|
||||
})
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func MainPageHandler (c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "MainPage",
|
||||
})
|
||||
}
|
@@ -1,9 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func RejectMediaHandler (c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"YouAreOn": "RejectMedia",
|
||||
})
|
||||
}
|
@@ -1,24 +1,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"CatsOfMastodonBotGo/internal/handlers"
|
||||
|
||||
"CatsOfMastodonBotGo/internal"
|
||||
handlers_admin "CatsOfMastodonBotGo/internal/handlers/admin"
|
||||
handlers_home "CatsOfMastodonBotGo/internal/handlers/home"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
func SetupRouter(r *gin.Engine) *gin.Engine {
|
||||
|
||||
func SetupRouter(r *gin.Engine, appContext *internal.AppContext) *gin.Engine {
|
||||
|
||||
adminDashboardHandler := handlers_admin.NewAdminDashboardHandler(appContext)
|
||||
homePageHandler := handlers_home.NewMainPageHandler(appContext)
|
||||
|
||||
admin := r.Group("/admin")
|
||||
|
||||
admin.GET("/", handlers.AdminDashboardHandler)
|
||||
admin.POST("/approve", handlers.ApproveMediaHandler)
|
||||
admin.POST("/reject", handlers.RejectMediaHandler)
|
||||
admin.GET("/", adminDashboardHandler.AdminHomePage)
|
||||
admin.POST("/approve", adminDashboardHandler.ApproveMedia)
|
||||
admin.POST("/reject", adminDashboardHandler.RejectMedia)
|
||||
|
||||
|
||||
r.GET("/", handlers.MainPageHandler)
|
||||
r.GET("/", homePageHandler.HomePageHandler)
|
||||
|
||||
return r
|
||||
}
|
||||
|
Reference in New Issue
Block a user