Finish migration
This commit is contained in:
@ -7,7 +7,7 @@ import (
|
||||
"templ-tests/app/templates"
|
||||
"templ-tests/app/types"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var pcInteractive templates.PageContext = templates.PageContext{
|
||||
@ -15,22 +15,24 @@ var pcInteractive templates.PageContext = templates.PageContext{
|
||||
ActivePage: "interactive",
|
||||
}
|
||||
|
||||
func Interactive(c *gin.Context) {
|
||||
templates.Interactive(pcInteractive, data.CatBreeds).Render(c, c.Writer)
|
||||
func Interactive(f *fiber.Ctx) error {
|
||||
Render(f, templates.Interactive(pcInteractive, data.CatBreeds))
|
||||
return nil
|
||||
}
|
||||
|
||||
func InteractiveSwapContent(c *gin.Context) {
|
||||
contentIndexStr := c.Query("content")
|
||||
func InteractiveSwapContent(f *fiber.Ctx) error {
|
||||
contentIndexStr := f.Query("content")
|
||||
contentIndex, err := strconv.Atoi(contentIndexStr)
|
||||
if err != nil {
|
||||
contentIndex = 0
|
||||
}
|
||||
templates.InteractiveSwapContent(pcInteractive, contentIndex).Render(c, c.Writer)
|
||||
Render(f, templates.InteractiveSwapContent(pcInteractive, contentIndex))
|
||||
return nil
|
||||
}
|
||||
|
||||
func FilterCatBreeds(c *gin.Context) {
|
||||
breedQuery := strings.ToLower(c.Query("breed"))
|
||||
countryQuery := c.Query("country")
|
||||
func FilterCatBreeds(f *fiber.Ctx) error {
|
||||
breedQuery := strings.ToLower(f.Query("breed"))
|
||||
countryQuery := f.Query("country")
|
||||
|
||||
var catBreeds []types.CatBreed = []types.CatBreed{}
|
||||
|
||||
@ -48,14 +50,15 @@ func FilterCatBreeds(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
templates.RenderCatBreedsTable(catBreeds).Render(c, c.Writer)
|
||||
Render(f, templates.RenderCatBreedsTable(catBreeds))
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateForm(c *gin.Context) {
|
||||
func ValidateForm(f *fiber.Ctx) error {
|
||||
content := templates.ValidateFormContent{
|
||||
Validated: true,
|
||||
NumValue: c.PostForm("number-value"),
|
||||
StrValue: c.PostForm("string-value"),
|
||||
NumValue: f.FormValue("number-value"),
|
||||
StrValue: f.FormValue("string-value"),
|
||||
}
|
||||
|
||||
numValue, err := strconv.Atoi(content.NumValue)
|
||||
@ -83,5 +86,6 @@ func ValidateForm(c *gin.Context) {
|
||||
content.StrValueError = "String length is more than 10"
|
||||
}
|
||||
|
||||
templates.RenderInteractiveForm(content).Render(c, c.Writer)
|
||||
Render(f, templates.RenderInteractiveForm(content))
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -1,70 +1 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func isError(c *gin.Context, err error) bool {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"details": err.Error(),
|
||||
})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func raiseError(c *gin.Context, errCode int, message string) {
|
||||
c.AbortWithStatusJSON(errCode, gin.H{
|
||||
"details": message,
|
||||
})
|
||||
}
|
||||
|
||||
func raiseBadRequestError(c *gin.Context, message string) {
|
||||
raiseError(c, http.StatusBadRequest, message)
|
||||
}
|
||||
|
||||
func raiseNotFoundError(c *gin.Context, message string) {
|
||||
raiseError(c, http.StatusNotFound, message)
|
||||
}
|
||||
|
||||
func raiseInternalError(c *gin.Context, message string) {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"details": "Internal server error. We will we will fix it!",
|
||||
})
|
||||
}
|
||||
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
allowHeaders := [12]string{
|
||||
"Content-Type",
|
||||
"Content-Length",
|
||||
"Accept-Encoding",
|
||||
"X-CSRF-Token",
|
||||
"Authorization",
|
||||
"accept",
|
||||
"origin",
|
||||
"Cache-Control",
|
||||
"X-Requested-With",
|
||||
"x-timezone",
|
||||
"Access-Control-Allow-Origin",
|
||||
"Access-Control-Max-Age",
|
||||
}
|
||||
|
||||
return func(c *gin.Context) {
|
||||
allowOrigin := "*"
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", allowOrigin)
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", strings.Join(allowHeaders[:], ", "))
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@ -53,13 +53,13 @@ func initRouter(app *fiber.App) {
|
||||
app.Get("/", handlers.Home)
|
||||
app.Get("/about", handlers.About)
|
||||
|
||||
// interactiveRouter := router.Group("/interactive")
|
||||
// {
|
||||
// interactiveRouter.GET("", handlers.Interactive)
|
||||
// interactiveRouter.GET("/swap-content", handlers.InteractiveSwapContent)
|
||||
// interactiveRouter.GET("/filter-cat-breeds", handlers.FilterCatBreeds)
|
||||
// interactiveRouter.POST("/validate-form", handlers.ValidateForm)
|
||||
// }
|
||||
interactiveGroup := app.Group("/interactive")
|
||||
{
|
||||
interactiveGroup.Get("", handlers.Interactive)
|
||||
interactiveGroup.Get("/swap-content", handlers.InteractiveSwapContent)
|
||||
interactiveGroup.Get("/filter-cat-breeds", handlers.FilterCatBreeds)
|
||||
interactiveGroup.Post("/validate-form", handlers.ValidateForm)
|
||||
}
|
||||
|
||||
app.Static("/static", STATIC_PATH)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user