quite a lot of things
This commit is contained in:
@@ -3,8 +3,10 @@ package server
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"billit/cmd/web"
|
||||
"github.com/a-h/templ"
|
||||
"billit/internal/api"
|
||||
"billit/internal/auth"
|
||||
"billit/internal/web"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
@@ -22,27 +24,106 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
MaxAge: 300,
|
||||
}))
|
||||
|
||||
// Static files
|
||||
fileServer := http.FileServer(http.FS(web.Files))
|
||||
e.GET("/assets/*", echo.WrapHandler(fileServer))
|
||||
|
||||
e.GET("/web", echo.WrapHandler(templ.Handler(web.HelloForm())))
|
||||
e.POST("/hello", echo.WrapHandler(http.HandlerFunc(web.HelloWebHandler)))
|
||||
// ========================================
|
||||
// Auth Setup
|
||||
// ========================================
|
||||
userStore := auth.NewDBUserStore(s.db)
|
||||
authService := auth.NewService(userStore)
|
||||
authHandlers := web.NewAuthHandlers(authService)
|
||||
|
||||
e.GET("/", s.HelloWorldHandler)
|
||||
// ========================================
|
||||
// API Routes (JSON responses) - Health only, products/invoice via web UI
|
||||
// ========================================
|
||||
apiHandlers := api.NewHandlers(s.db)
|
||||
apiGroup := e.Group("/api")
|
||||
{
|
||||
apiGroup.GET("/health", apiHandlers.HealthHandler)
|
||||
}
|
||||
|
||||
e.GET("/health", s.healthHandler)
|
||||
// ========================================
|
||||
// Public Web Routes (no auth required)
|
||||
// ========================================
|
||||
e.GET("/", authHandlers.LoginPageHandler)
|
||||
e.POST("/login", authHandlers.LoginHandler)
|
||||
e.GET("/register", authHandlers.RegisterPageHandler)
|
||||
e.POST("/register", authHandlers.RegisterHandler)
|
||||
e.GET("/logout", authHandlers.LogoutHandler)
|
||||
|
||||
// ========================================
|
||||
// Protected Web Routes (auth required)
|
||||
// ========================================
|
||||
protected := e.Group("")
|
||||
protected.Use(authHandlers.AuthMiddleware)
|
||||
|
||||
// Home
|
||||
homeHandlers := web.NewHomeHandlers(s.db)
|
||||
protected.GET("/home", homeHandlers.HomePageHandler)
|
||||
|
||||
// Account routes
|
||||
accountHandlers := web.NewAccountHandlers(s.db, authService)
|
||||
protected.GET("/account", accountHandlers.AccountPageHandler)
|
||||
protected.POST("/account/details", accountHandlers.UpdateDetailsHandler)
|
||||
protected.POST("/account/password", accountHandlers.ChangePasswordHandler)
|
||||
|
||||
// Buyer routes
|
||||
buyerHandlers := web.NewBuyerHandlers(s.db)
|
||||
protected.GET("/buyer", buyerHandlers.BuyerListHandler)
|
||||
protected.GET("/buyer/create", buyerHandlers.BuyerCreatePageHandler)
|
||||
protected.POST("/buyer/create", buyerHandlers.BuyerCreateHandler)
|
||||
protected.GET("/buyer/edit/:id", buyerHandlers.BuyerEditPageHandler)
|
||||
protected.POST("/buyer/edit/:id", buyerHandlers.BuyerUpdateHandler)
|
||||
protected.DELETE("/buyer/:id", buyerHandlers.BuyerDeleteHandler)
|
||||
|
||||
// Invoices list
|
||||
invoicesHandlers := web.NewInvoicesHandlers(s.db)
|
||||
protected.GET("/invoice", invoicesHandlers.InvoicesListHandler)
|
||||
|
||||
// Product routes (web UI)
|
||||
productHandlers := web.NewProductHandlers(s.db)
|
||||
protected.GET("/product", productHandlers.ProductListHandler)
|
||||
protected.GET("/product/create", productHandlers.ProductCreatePageHandler)
|
||||
protected.POST("/product/create", productHandlers.ProductCreateHandler)
|
||||
protected.GET("/product/edit/:sku", productHandlers.ProductEditPageHandler)
|
||||
protected.POST("/product/edit/:sku", productHandlers.ProductUpdateHandler)
|
||||
protected.DELETE("/product/:sku", productHandlers.ProductDeleteHandler)
|
||||
|
||||
// Billing routes (web UI)
|
||||
billingHandlers := web.NewBillingHandlers(s.db)
|
||||
protected.GET("/billing", billingHandlers.BillingPageHandler)
|
||||
protected.POST("/billing/calculate", billingHandlers.CalculateBillHandler)
|
||||
protected.POST("/billing/generate", billingHandlers.GenerateBillHandler)
|
||||
protected.GET("/billing/add-row", billingHandlers.AddProductRowHandler)
|
||||
|
||||
// Invoice view (protected - only owner can view)
|
||||
protected.GET("/invoice/:id", billingHandlers.ShowInvoiceHandler)
|
||||
|
||||
// Legacy health check (kept for backward compatibility)
|
||||
e.GET("/health", apiHandlers.HealthHandler)
|
||||
|
||||
// Custom 404 handler for Echo HTTP errors
|
||||
e.HTTPErrorHandler = func(err error, c echo.Context) {
|
||||
if he, ok := err.(*echo.HTTPError); ok {
|
||||
switch he.Code {
|
||||
case http.StatusNotFound:
|
||||
_ = web.RenderNotFound(c, "")
|
||||
return
|
||||
case http.StatusInternalServerError:
|
||||
_ = web.RenderServerError(c, "")
|
||||
return
|
||||
}
|
||||
}
|
||||
// Default error handler for other cases
|
||||
e.DefaultHTTPErrorHandler(err, c)
|
||||
}
|
||||
|
||||
// Catch-all for undefined routes (must be last)
|
||||
e.RouteNotFound("/*", func(c echo.Context) error {
|
||||
return web.RenderNotFound(c, "")
|
||||
})
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (s *Server) HelloWorldHandler(c echo.Context) error {
|
||||
resp := map[string]string{
|
||||
"message": "Hello World",
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func (s *Server) healthHandler(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, s.db.Health())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user