feat: added strapi echo routes
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"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
|
||||
}
|
||||
|
||||
// Helper method to get bool param or default value
|
||||
func GetBoolParam(c *echo.Context, name string, defaultValue bool) bool {
|
||||
value, err := strconv.ParseBool(c.ParamOr(name, strconv.FormatBool(defaultValue)))
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
@@ -8,10 +8,7 @@ import (
|
||||
)
|
||||
|
||||
// GetAllPosts returns a list of all posts
|
||||
func GetAllPosts(c *echo.Context, s *services.StrapiService) error {
|
||||
pageSize := GetIntParam(c, "pageSize", 10)
|
||||
page := GetIntParam(c, "page", 1)
|
||||
|
||||
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)
|
||||
@@ -21,10 +18,7 @@ func GetAllPosts(c *echo.Context, s *services.StrapiService) error {
|
||||
}
|
||||
|
||||
// GetFeaturedPosts returns a list of featured posts
|
||||
func GetFeaturedPosts(c *echo.Context, s *services.StrapiService) error {
|
||||
pageSize := GetIntParam(c, "pageSize", 10)
|
||||
page := GetIntParam(c, "page", 1)
|
||||
|
||||
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)
|
||||
@@ -44,10 +38,7 @@ func GetPost(c *echo.Context, s *services.StrapiService, slug string) error {
|
||||
}
|
||||
|
||||
// GetPostSummaries returns post summaries
|
||||
func GetPostSummaries(c *echo.Context, s *services.StrapiService) error {
|
||||
pageSize := GetIntParam(c, "pageSize", 10)
|
||||
page := GetIntParam(c, "page", 1)
|
||||
|
||||
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)
|
||||
|
||||
@@ -12,8 +12,9 @@ func NewCharmSlog() *slog.Logger {
|
||||
// 1. Initialize Charmbracelet
|
||||
options := log.Options{
|
||||
ReportTimestamp: true,
|
||||
ReportCaller: true,
|
||||
ReportCaller: false,
|
||||
Level: log.DebugLevel,
|
||||
Prefix: "ECHO",
|
||||
}
|
||||
handler := log.NewWithOptions(os.Stderr, options)
|
||||
|
||||
|
||||
@@ -3,14 +3,38 @@ package routes
|
||||
import (
|
||||
"blog/internal/cache"
|
||||
"blog/internal/echo/handlers"
|
||||
"blog/internal/echo/middleware"
|
||||
"blog/internal/services"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/labstack/echo/v5"
|
||||
"github.com/labstack/echo/v5/middleware"
|
||||
"github.com/labstack/echo-contrib/v5/echoprometheus"
|
||||
)
|
||||
|
||||
// Helper method to get int param or default value
|
||||
func GetIntQueryParam(c *echo.Context, name string, defaultValue int) int {
|
||||
value, err := strconv.Atoi(c.QueryParamOr(name, strconv.Itoa(defaultValue)))
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// Helper method to get bool param or default value
|
||||
func GetBoolQueryParam(c *echo.Context, name string, defaultValue bool) bool {
|
||||
value, err := strconv.ParseBool(c.QueryParamOr(name, strconv.FormatBool(defaultValue)))
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
|
||||
// Souces is a struct that has the sources for any data that the routes need
|
||||
type Sources struct {
|
||||
StrapiService *services.StrapiService
|
||||
@@ -23,17 +47,48 @@ func SetupRoutes(e *echo.Echo, sources Sources) {
|
||||
}
|
||||
|
||||
// Global middleware
|
||||
e.Use(middleware.ServerHandler)
|
||||
|
||||
// Remove trailing slash
|
||||
e.Pre(middleware.RemoveTrailingSlash())
|
||||
// Use GZIP compression
|
||||
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{ Level: 5, }))
|
||||
// CORS origin
|
||||
e.Use(middleware.CORS("https://blog.darwincereska.dev", "http://localhost:3000", "http://0.0.0.0:3000"))
|
||||
// Request logger
|
||||
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
|
||||
LogStatus: true,
|
||||
LogURI: true,
|
||||
LogMethod: true,
|
||||
HandleError: true,
|
||||
LogLatency: true,
|
||||
LogRemoteIP: true,
|
||||
LogURIPath: true,
|
||||
LogValuesFunc: func(c *echo.Context, v middleware.RequestLoggerValues) error {
|
||||
if v.Error == nil {
|
||||
e.Logger.Info("REQUEST", "method", v.Method, "uri", v.URI, "status", v.Status, "latency", v.Latency, "remote-ip", v.RemoteIP)
|
||||
} else {
|
||||
e.Logger.Error("REQUEST_ERROR", "error", v.Error.Error(), "method", v.Method, "uri", v.URIPath, "status", v.Status, "latency", v.Latency, "remote-ip", v.RemoteIP)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}))
|
||||
// Context timeout : DEFAULT 60s
|
||||
e.Use(middleware.ContextTimeout(time.Second * 60))
|
||||
// Prometheus metrics
|
||||
e.Use(echoprometheus.NewMiddleware("blog"))
|
||||
|
||||
// Routes
|
||||
strapiRoutes(e, sources.StrapiService)
|
||||
|
||||
// Static routes
|
||||
e.Static("/public", "web/static")
|
||||
|
||||
// Special routes
|
||||
e.GET("/api", func(c *echo.Context) error {
|
||||
// Load all routes
|
||||
// routes, _ := json.MarshalIndent(e.Router().Routes(), "", "")
|
||||
e.GET("/api*", func(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, e.Router().Routes())
|
||||
})
|
||||
|
||||
e.GET("/metrics", echoprometheus.NewHandler())
|
||||
}
|
||||
|
||||
// Setup Strapi routes
|
||||
@@ -43,11 +98,33 @@ func strapiRoutes(e *echo.Echo, s *services.StrapiService) {
|
||||
|
||||
// GET /api/posts/all
|
||||
posts.GET("/all", func(c *echo.Context) error {
|
||||
return handlers.GetAllPosts(c, s)
|
||||
pageSize := GetIntQueryParam(c, "pageSize", 10)
|
||||
page := GetIntQueryParam(c, "page", 1)
|
||||
|
||||
return handlers.GetPostSummaries(c, s, pageSize, page)
|
||||
})
|
||||
|
||||
// GET /api/posts/featured
|
||||
posts.GET("/featured", func(c *echo.Context) error {
|
||||
return handlers.GetFeaturedPosts(c, s)
|
||||
pageSize := GetIntQueryParam(c, "pageSize", 10)
|
||||
page := GetIntQueryParam(c, "page", 1)
|
||||
|
||||
return handlers.GetFeaturedPosts(c, s, pageSize, page)
|
||||
})
|
||||
|
||||
// GET /api/posts/tag/:tag
|
||||
posts.GET("/tag/:tag", func(c *echo.Context) error {
|
||||
tag := c.Param("tag")
|
||||
pageSize := GetIntQueryParam(c, "pageSize", 10)
|
||||
page := GetIntQueryParam(c, "page", 1)
|
||||
|
||||
return handlers.GetPostsByTag(c, s, tag, pageSize, page)
|
||||
})
|
||||
|
||||
// GET /api/posts/post/:slug
|
||||
posts.GET("/post/:slug", func(c *echo.Context) error {
|
||||
slug := c.Param("slug")
|
||||
|
||||
return handlers.GetPost(c, s, slug)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user