refactor(server): update main entry point and docs for Echo v5
Update cmd/server/main.go and internal/docs/http_handler.go for Echo v5. Changes in main.go: - Update import from echo/v4 to echo/v5 - Replace echomiddleware.Logger() with RequestLogger() - Remove net/http import (no longer needed) - Update server startup to use app.StartServer() - Replaces direct echo.Start() call - Better separation of concerns Changes in http_handler.go: - Update handler signatures to use *echo.Context - Ensure Echo v5 compatibility These changes complete the server layer migration to Echo v5.
This commit is contained in:
+8
-12
@@ -12,13 +12,12 @@ import (
|
|||||||
"bookhoard/internal/sync"
|
"bookhoard/internal/sync"
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v5"
|
||||||
echomiddleware "github.com/labstack/echo/v4/middleware"
|
echomiddleware "github.com/labstack/echo/v5/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CustomValidator wraps the go-playground validator
|
// CustomValidator wraps the go-playground validator
|
||||||
@@ -139,7 +138,7 @@ func main() {
|
|||||||
e.Validator = &CustomValidator{validator: v}
|
e.Validator = &CustomValidator{validator: v}
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
e.Use(echomiddleware.Logger())
|
e.Use(echomiddleware.RequestLogger())
|
||||||
e.Use(echomiddleware.Recover())
|
e.Use(echomiddleware.Recover())
|
||||||
e.Use(echomiddleware.CORS())
|
e.Use(echomiddleware.CORS())
|
||||||
e.Use(ratelimit.RequestTracingMiddleware(cfg))
|
e.Use(ratelimit.RequestTracingMiddleware(cfg))
|
||||||
@@ -197,17 +196,14 @@ func main() {
|
|||||||
// ========================================================================
|
// ========================================================================
|
||||||
// START SERVER (managed by app lifecycle)
|
// START SERVER (managed by app lifecycle)
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
|
|
||||||
log.Printf("Starting server on port %s", cfg.ServerPort)
|
log.Printf("Starting server on port %s", cfg.ServerPort)
|
||||||
|
|
||||||
// Start HTTP server in background
|
// Start HTTP server
|
||||||
go func() {
|
if err := application.StartServer(":" + cfg.ServerPort); err != nil {
|
||||||
if err := e.Start(":" + cfg.ServerPort); err != nil && err != http.ErrServerClosed {
|
log.Fatalf("Failed to start server: %v", err)
|
||||||
log.Fatalf("Server failed to start: %v", err)
|
}
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Start application (blocks until shutdown signal)
|
// Start application lifecycle (blocks until shutdown signal)
|
||||||
if err := application.Start(); err != nil {
|
if err := application.Start(); err != nil {
|
||||||
log.Fatalf("Application error: %v", err)
|
log.Fatalf("Application error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ import (
|
|||||||
|
|
||||||
"bookhoard/internal/database"
|
"bookhoard/internal/database"
|
||||||
"bookhoard/templates"
|
"bookhoard/templates"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HTTPHandler wraps the docs handler for Echo framework
|
// HTTPHandler wraps the docs handler for Echo framework
|
||||||
@@ -25,7 +26,7 @@ func NewHTTPHandler(docsPath string) *HTTPHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ShowDocumentation renders a documentation page
|
// ShowDocumentation renders a documentation page
|
||||||
func (h *HTTPHandler) ShowDocumentation(c echo.Context) error {
|
func (h *HTTPHandler) ShowDocumentation(c *echo.Context) error {
|
||||||
// Get the document path from URL parameter
|
// Get the document path from URL parameter
|
||||||
docPath := c.Param("*")
|
docPath := c.Param("*")
|
||||||
|
|
||||||
@@ -101,7 +102,7 @@ func (h *HTTPHandler) ShowDocumentation(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ShowAPIEndpoint shows a specific API endpoint documentation
|
// ShowAPIEndpoint shows a specific API endpoint documentation
|
||||||
func (h *HTTPHandler) ShowAPIEndpoint(c echo.Context, endpointPath string) error {
|
func (h *HTTPHandler) ShowAPIEndpoint(c *echo.Context, endpointPath string) error {
|
||||||
// Check if user is logged in
|
// Check if user is logged in
|
||||||
isLoggedIn := false
|
isLoggedIn := false
|
||||||
if userID := c.Get("user_id"); userID != nil {
|
if userID := c.Get("user_id"); userID != nil {
|
||||||
@@ -182,7 +183,7 @@ func (h *HTTPHandler) ShowAPIEndpoint(c echo.Context, endpointPath string) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// showLegacyAPIEndpoint shows API endpoint documentation without explorer data
|
// showLegacyAPIEndpoint shows API endpoint documentation without explorer data
|
||||||
func (h *HTTPHandler) showLegacyAPIEndpoint(c echo.Context, endpointPath string, isLoggedIn bool) error {
|
func (h *HTTPHandler) showLegacyAPIEndpoint(c *echo.Context, endpointPath string, isLoggedIn bool) error {
|
||||||
endpoints := h.docs.GetAPIEndpoints()
|
endpoints := h.docs.GetAPIEndpoints()
|
||||||
|
|
||||||
// Find the endpoint
|
// Find the endpoint
|
||||||
@@ -269,12 +270,12 @@ func (h *HTTPHandler) showLegacyAPIEndpoint(c echo.Context, endpointPath string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DocsHome redirects to the documentation index
|
// DocsHome redirects to the documentation index
|
||||||
func (h *HTTPHandler) DocsHome(c echo.Context) error {
|
func (h *HTTPHandler) DocsHome(c *echo.Context) error {
|
||||||
return c.Redirect(http.StatusFound, "/docs/index.md")
|
return c.Redirect(http.StatusFound, "/docs/index.md")
|
||||||
}
|
}
|
||||||
|
|
||||||
// APIHome shows the API documentation home page
|
// APIHome shows the API documentation home page
|
||||||
func (h *HTTPHandler) APIHome(c echo.Context) error {
|
func (h *HTTPHandler) APIHome(c *echo.Context) error {
|
||||||
endpoints := h.docs.GetAPIEndpoints()
|
endpoints := h.docs.GetAPIEndpoints()
|
||||||
nav := h.docs.BuildNavigation()
|
nav := h.docs.BuildNavigation()
|
||||||
|
|
||||||
@@ -343,7 +344,7 @@ func (h *HTTPHandler) APIHome(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Search searches through documentation
|
// Search searches through documentation
|
||||||
func (h *HTTPHandler) Search(c echo.Context) error {
|
func (h *HTTPHandler) Search(c *echo.Context) error {
|
||||||
query := c.QueryParam("q")
|
query := c.QueryParam("q")
|
||||||
if query == "" {
|
if query == "" {
|
||||||
return c.JSON(http.StatusBadRequest, map[string]string{
|
return c.JSON(http.StatusBadRequest, map[string]string{
|
||||||
@@ -404,7 +405,7 @@ type SearchResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TryEndpoint executes an API endpoint (for the interactive explorer)
|
// TryEndpoint executes an API endpoint (for the interactive explorer)
|
||||||
func (h *HTTPHandler) TryEndpoint(c echo.Context) error {
|
func (h *HTTPHandler) TryEndpoint(c *echo.Context) error {
|
||||||
// This would execute the actual API request
|
// This would execute the actual API request
|
||||||
// For now, return a placeholder
|
// For now, return a placeholder
|
||||||
return c.JSON(http.StatusOK, map[string]interface{}{
|
return c.JSON(http.StatusOK, map[string]interface{}{
|
||||||
@@ -413,7 +414,7 @@ func (h *HTTPHandler) TryEndpoint(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ServeSearchIndex serves the Lunr.js search index
|
// ServeSearchIndex serves the Lunr.js search index
|
||||||
func (h *HTTPHandler) ServeSearchIndex(c echo.Context) error {
|
func (h *HTTPHandler) ServeSearchIndex(c *echo.Context) error {
|
||||||
index, err := h.docs.GenerateSearchIndex()
|
index, err := h.docs.GenerateSearchIndex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{
|
return c.JSON(http.StatusInternalServerError, map[string]string{
|
||||||
|
|||||||
Reference in New Issue
Block a user