Move API initialization

This commit is contained in:
Ivan R. 2023-07-22 15:19:04 +05:00
parent 2c08171c7a
commit 3c9e30375e
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C
2 changed files with 77 additions and 66 deletions

68
main.go
View file

@ -1,7 +1,6 @@
package main
import (
"github.com/gin-gonic/gin"
"github.com/ordinary-dev/phoenix/backend"
"github.com/ordinary-dev/phoenix/config"
"github.com/ordinary-dev/phoenix/views"
@ -31,69 +30,6 @@ func main() {
logrus.Fatalf("%v", err)
}
r := gin.Default()
r.LoadHTMLGlob("templates/*")
r.Static("/assets", "./assets")
r.GET("/signin", func(c *gin.Context) {
views.ShowLoginForm(c)
})
r.POST("/signin", func(c *gin.Context) {
views.AuthorizeUser(c, db, cfg)
})
protected := r.Group("/")
protected.Use(func(c *gin.Context) {
views.AuthMiddleware(c, cfg)
})
// Main page
protected.GET("/", func(c *gin.Context) {
views.ShowMainPage(c, db)
})
protected.GET("/settings", func(c *gin.Context) {
views.ShowSettings(c, db)
})
// Create new user
protected.POST("/users", func(c *gin.Context) {
views.CreateUser(c, db, cfg)
})
// Create new group
protected.POST("/groups", func(c *gin.Context) {
views.CreateGroup(c, db)
})
// Update group
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
protected.POST("/groups/:id/put", func(c *gin.Context) {
views.UpdateGroup(c, db)
})
// Delete group
// HTML forms cannot be submitted using the DELETE method without javascript.
protected.POST("/groups/:id/delete", func(c *gin.Context) {
views.DeleteGroup(c, db)
})
// Create new link
protected.POST("/links", func(c *gin.Context) {
views.CreateLink(c, db)
})
// Update link.
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
protected.POST("/links/:id/put", func(c *gin.Context) {
views.UpdateLink(c, db)
})
// Delete link
// HTML forms cannot be submitted using the DELETE method without javascript.
protected.POST("/links/:id/delete", func(c *gin.Context) {
views.DeleteLink(c, db)
})
r.Run(":8080")
engine := views.GetGinEngine(cfg, db)
engine.Run(":8080")
}

75
views/main.go Normal file
View file

@ -0,0 +1,75 @@
package views
import (
"github.com/gin-gonic/gin"
"github.com/ordinary-dev/phoenix/config"
"gorm.io/gorm"
)
func GetGinEngine(cfg *config.Config, db *gorm.DB) *gin.Engine {
engine := gin.Default()
engine.LoadHTMLGlob("templates/*")
engine.Static("/assets", "./assets")
engine.GET("/signin", func(c *gin.Context) {
ShowLoginForm(c)
})
engine.POST("/signin", func(c *gin.Context) {
AuthorizeUser(c, db, cfg)
})
protected := engine.Group("/")
protected.Use(func(c *gin.Context) {
AuthMiddleware(c, cfg)
})
// Main page
protected.GET("/", func(c *gin.Context) {
ShowMainPage(c, db)
})
protected.GET("/settings", func(c *gin.Context) {
ShowSettings(c, db)
})
// Create new user
protected.POST("/users", func(c *gin.Context) {
CreateUser(c, db, cfg)
})
// Create new group
protected.POST("/groups", func(c *gin.Context) {
CreateGroup(c, db)
})
// Update group
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
protected.POST("/groups/:id/put", func(c *gin.Context) {
UpdateGroup(c, db)
})
// Delete group
// HTML forms cannot be submitted using the DELETE method without javascript.
protected.POST("/groups/:id/delete", func(c *gin.Context) {
DeleteGroup(c, db)
})
// Create new link
protected.POST("/links", func(c *gin.Context) {
CreateLink(c, db)
})
// Update link.
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
protected.POST("/links/:id/put", func(c *gin.Context) {
UpdateLink(c, db)
})
// Delete link
// HTML forms cannot be submitted using the DELETE method without javascript.
protected.POST("/links/:id/delete", func(c *gin.Context) {
DeleteLink(c, db)
})
return engine
}