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
|
// https://seefnasrul.medium.com/create-your-first-go-rest-api-with-jwt-authentication-in-gin-framework-dbe5bda72817
|
||||||
engine := gin.Default()
|
engine := gin.Default()
|
||||||
r := server.SetupRouter(engine)
|
r := server.SetupRouter(engine, appContext)
|
||||||
err := r.Run(":8080")
|
err := r.Run(":8080")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@@ -1,12 +1,11 @@
|
|||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
// "github.com/gin-gonic/gin"
|
||||||
"gorm.io/gorm"
|
// "gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AppWebContext struct {
|
// type AppWebContext struct {
|
||||||
Db *gorm.DB
|
// Db *gorm.DB
|
||||||
GinEngine *gin.Engine
|
// 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
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"CatsOfMastodonBotGo/internal/handlers"
|
"CatsOfMastodonBotGo/internal"
|
||||||
|
handlers_admin "CatsOfMastodonBotGo/internal/handlers/admin"
|
||||||
|
handlers_home "CatsOfMastodonBotGo/internal/handlers/home"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"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 := r.Group("/admin")
|
||||||
|
|
||||||
admin.GET("/", handlers.AdminDashboardHandler)
|
admin.GET("/", adminDashboardHandler.AdminHomePage)
|
||||||
admin.POST("/approve", handlers.ApproveMediaHandler)
|
admin.POST("/approve", adminDashboardHandler.ApproveMedia)
|
||||||
admin.POST("/reject", handlers.RejectMediaHandler)
|
admin.POST("/reject", adminDashboardHandler.RejectMedia)
|
||||||
|
|
||||||
|
|
||||||
r.GET("/", handlers.MainPageHandler)
|
r.GET("/", homePageHandler.HomePageHandler)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user