phoenix/views/main.go

64 lines
1.7 KiB
Go
Raw Normal View History

2023-07-22 15:19:04 +05:00
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 {
if cfg.Production {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.New()
engine.Use(gin.Recovery())
if cfg.EnableGinLogger {
engine.Use(gin.Logger())
}
2023-07-22 15:19:04 +05:00
engine.LoadHTMLGlob("templates/*")
engine.Static("/assets", "./assets")
2023-07-22 20:49:13 +05:00
engine.Use(SecurityHeadersMiddleware)
2023-11-01 23:18:33 +05:00
engine.GET("/signin", ShowLoginForm(cfg))
2023-11-01 22:40:15 +05:00
engine.POST("/api/users/signin", AuthorizeUser(db, cfg))
2023-11-01 23:18:33 +05:00
engine.GET("/registration", ShowRegistrationForm(cfg, db))
2023-11-01 22:40:15 +05:00
engine.POST("/api/users", CreateUser(db, cfg))
2023-07-22 21:39:43 +05:00
// This group requires authorization before viewing.
2023-07-22 15:19:04 +05:00
protected := engine.Group("/")
2023-11-01 22:40:15 +05:00
protected.Use(AuthMiddleware(db, cfg))
2023-07-22 15:19:04 +05:00
// Main page
2023-11-01 23:18:33 +05:00
protected.GET("/", ShowMainPage(cfg, db))
2023-07-22 15:19:04 +05:00
2023-11-01 23:18:33 +05:00
protected.GET("/settings", ShowSettings(cfg, db))
2023-07-22 15:19:04 +05:00
// Create new group
2023-11-01 23:18:33 +05:00
protected.POST("/api/groups", CreateGroup(cfg, db))
2023-07-22 15:19:04 +05:00
// Update group
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
2023-11-01 23:18:33 +05:00
protected.POST("/api/groups/:id/put", UpdateGroup(cfg, db))
2023-07-22 15:19:04 +05:00
// Delete group
// HTML forms cannot be submitted using the DELETE method without javascript.
2023-11-01 23:18:33 +05:00
protected.POST("/api/groups/:id/delete", DeleteGroup(cfg, db))
2023-07-22 15:19:04 +05:00
// Create new link
2023-11-01 23:18:33 +05:00
protected.POST("/api/links", CreateLink(cfg, db))
2023-07-22 15:19:04 +05:00
// Update link.
// HTML forms cannot be submitted using PUT or PATCH methods without javascript.
2023-11-01 23:18:33 +05:00
protected.POST("/api/links/:id/put", UpdateLink(cfg, db))
2023-07-22 15:19:04 +05:00
// Delete link
// HTML forms cannot be submitted using the DELETE method without javascript.
2023-11-01 23:18:33 +05:00
protected.POST("/api/links/:id/delete", DeleteLink(cfg, db))
2023-07-22 15:19:04 +05:00
return engine
}