feat: added first api route
This commit is contained in:
58
internal/echo/handlers/post_handler.go
Normal file
58
internal/echo/handlers/post_handler.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"blog/internal/services"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
// GetAllPosts returns a list of all posts
|
||||
func GetAllPosts(c *echo.Context, s *services.StrapiService, pageSize, page int) error {
|
||||
posts, err := s.GetAllPosts(c.Request().Context(), pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, posts)
|
||||
}
|
||||
|
||||
// GetFeaturedPosts returns a list of featured posts
|
||||
func GetFeaturedPosts(c *echo.Context, s *services.StrapiService, pageSize, page int) error {
|
||||
posts, err := s.GetFeaturedPosts(c.Request().Context(), pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, posts)
|
||||
}
|
||||
|
||||
// GetPost returns a specific post
|
||||
func GetPost(c *echo.Context, s *services.StrapiService, slug string) error {
|
||||
post, err := s.GetPost(c.Request().Context(), slug)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, post)
|
||||
}
|
||||
|
||||
// GetPostSummaries returns post summaries
|
||||
func GetPostSummaries(c *echo.Context, s *services.StrapiService, pageSize, page int) error {
|
||||
posts, err := s.GetPostSummaries(c.Request().Context(), pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, posts)
|
||||
}
|
||||
|
||||
// GetPostsByTag returns a list of posts by tag
|
||||
func GetPostsByTag(c *echo.Context, s *services.StrapiService, tag string, pageSize, page int) error {
|
||||
posts, err := s.GetPostsByTag(c.Request().Context(), tag, pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, posts)
|
||||
}
|
||||
33
internal/echo/middleware/middleware.go
Normal file
33
internal/echo/middleware/middleware.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
func ServerHandler(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
start := time.Now()
|
||||
|
||||
// Process the request
|
||||
err := next(c)
|
||||
if err != nil {
|
||||
c.Logger().Error(err.Error())
|
||||
}
|
||||
|
||||
stop := time.Now()
|
||||
req := c.Request()
|
||||
|
||||
// Log using charmbracelet
|
||||
log.Info("Request handled",
|
||||
"method", req.Method,
|
||||
"path", req.URL.Path,
|
||||
"latency", stop.Sub(start),
|
||||
"ip", c.RealIP(),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
36
internal/echo/routes/routes.go
Normal file
36
internal/echo/routes/routes.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"blog/internal/echo/handlers"
|
||||
"blog/internal/echo/middleware"
|
||||
"blog/internal/services"
|
||||
"strconv"
|
||||
|
||||
"github.com/labstack/echo/v5"
|
||||
)
|
||||
|
||||
// Helper method to get int param or default value
|
||||
func getIntParam(c *echo.Context, name string, defaultValue int) int {
|
||||
value, err := strconv.Atoi(c.ParamOr(name, strconv.Itoa(defaultValue)))
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func SetupRoutes(e *echo.Echo, s *services.StrapiService) {
|
||||
// Global middleware
|
||||
e.Use(middleware.ServerHandler)
|
||||
|
||||
// Post routes
|
||||
posts := e.Group("/posts") // Routing group
|
||||
|
||||
// GET /posts/all
|
||||
posts.GET("/all", func(c *echo.Context) error {
|
||||
pageSize := getIntParam(c, "pageSize", 10)
|
||||
page := getIntParam(c, "page", 1)
|
||||
|
||||
return handlers.GetAllPosts(c, s, pageSize, page)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user