Update main.go to use templ components for routing
This commit is contained in:
@@ -0,0 +1,106 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bookmann/internal/config"
|
||||||
|
"bookmann/templates"
|
||||||
|
"bytes"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CustomValidator wraps the go-playground validator
|
||||||
|
type CustomValidator struct {
|
||||||
|
validator *validator.Validate
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cv *CustomValidator) Validate(i interface{}) error {
|
||||||
|
return cv.validator.Struct(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cfg := config.LoadConfig()
|
||||||
|
|
||||||
|
e := echo.New()
|
||||||
|
|
||||||
|
// Set up validator
|
||||||
|
e.Validator = &CustomValidator{validator: validator.New()}
|
||||||
|
|
||||||
|
// Middleware
|
||||||
|
e.Use(middleware.Logger())
|
||||||
|
e.Use(middleware.Recover())
|
||||||
|
e.Use(middleware.CORS())
|
||||||
|
|
||||||
|
dummyUser := templates.User{ID: "", Username: "Admin", Email: "admin@example.com"}
|
||||||
|
|
||||||
|
// Routes
|
||||||
|
e.GET("/", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.Index().Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
e.GET("/login", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.Login().Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
e.GET("/register", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.Register().Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
e.GET("/admin", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.Admin(dummyUser).Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
e.GET("/admin/", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.Admin(dummyUser).Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
e.GET("/admin/profile", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.AdminProfile(dummyUser).Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
e.GET("/admin/library", func(c echo.Context) error {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err := templates.AdminLibrary(dummyUser).Render(c.Request().Context(), &buf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return c.HTML(http.StatusOK, buf.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
log.Printf("Starting server on port %s", cfg.ServerPort)
|
||||||
|
e.Logger.Fatal(e.Start(":" + cfg.ServerPort))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user