feat: added logging and some routes with fmt
This commit is contained in:
27
internal/echo/handlers/params.go
Normal file
27
internal/echo/handlers/params.go
Normal file
@@ -0,0 +1,27 @@
|
||||
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,7 +8,10 @@ import (
|
||||
)
|
||||
|
||||
// GetAllPosts returns a list of all posts
|
||||
func GetAllPosts(c *echo.Context, s *services.StrapiService, pageSize, page int) error {
|
||||
func GetAllPosts(c *echo.Context, s *services.StrapiService) error {
|
||||
pageSize := GetIntParam(c, "pageSize", 10)
|
||||
page := GetIntParam(c, "page", 1)
|
||||
|
||||
posts, err := s.GetAllPosts(c.Request().Context(), pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
@@ -18,7 +21,10 @@ func GetAllPosts(c *echo.Context, s *services.StrapiService, pageSize, page int)
|
||||
}
|
||||
|
||||
// GetFeaturedPosts returns a list of featured posts
|
||||
func GetFeaturedPosts(c *echo.Context, s *services.StrapiService, pageSize, page int) error {
|
||||
func GetFeaturedPosts(c *echo.Context, s *services.StrapiService) error {
|
||||
pageSize := GetIntParam(c, "pageSize", 10)
|
||||
page := GetIntParam(c, "page", 1)
|
||||
|
||||
posts, err := s.GetFeaturedPosts(c.Request().Context(), pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
@@ -38,7 +44,10 @@ func GetPost(c *echo.Context, s *services.StrapiService, slug string) error {
|
||||
}
|
||||
|
||||
// GetPostSummaries returns post summaries
|
||||
func GetPostSummaries(c *echo.Context, s *services.StrapiService, pageSize, page int) error {
|
||||
func GetPostSummaries(c *echo.Context, s *services.StrapiService) error {
|
||||
pageSize := GetIntParam(c, "pageSize", 10)
|
||||
page := GetIntParam(c, "page", 1)
|
||||
|
||||
posts, err := s.GetPostSummaries(c.Request().Context(), pageSize, page)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, err)
|
||||
|
||||
23
internal/echo/logger/logger.go
Normal file
23
internal/echo/logger/logger.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
// NewCharmSlog returns a standard *slog.Logger powered by Charmbracelet
|
||||
func NewCharmSlog() *slog.Logger {
|
||||
// 1. Initialize Charmbracelet
|
||||
options := log.Options{
|
||||
ReportTimestamp: true,
|
||||
ReportCaller: true,
|
||||
Level: log.DebugLevel,
|
||||
}
|
||||
handler := log.NewWithOptions(os.Stderr, options)
|
||||
|
||||
// 2. Return as *slog.Logger
|
||||
// Charmbracelet's Logger implements the slog.Handler interface
|
||||
return slog.New(handler)
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
// "time"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
// "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()
|
||||
// start := time.Now()
|
||||
|
||||
// Process the request
|
||||
err := next(c)
|
||||
@@ -17,16 +17,16 @@ func ServerHandler(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
c.Logger().Error(err.Error())
|
||||
}
|
||||
|
||||
stop := time.Now()
|
||||
req := c.Request()
|
||||
// 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(),
|
||||
)
|
||||
// // Log using charmbracelet
|
||||
// log.Info("Request handlers",
|
||||
// "method", req.Method,
|
||||
// "path", req.URL.Path,
|
||||
// "latency", stop.Sub(start),
|
||||
// "ip", c.RealIP(),
|
||||
// )
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,36 +1,53 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"blog/internal/cache"
|
||||
"blog/internal/echo/handlers"
|
||||
"blog/internal/echo/middleware"
|
||||
"blog/internal/services"
|
||||
"strconv"
|
||||
"net/http"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"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
|
||||
// Souces is a struct that has the sources for any data that the routes need
|
||||
type Sources struct {
|
||||
StrapiService *services.StrapiService
|
||||
Caches []cache.Cache
|
||||
}
|
||||
|
||||
func SetupRoutes(e *echo.Echo, s *services.StrapiService) {
|
||||
func SetupRoutes(e *echo.Echo, sources Sources) {
|
||||
if sources.StrapiService == nil {
|
||||
log.Fatal("Error", "error", "strapi service is required")
|
||||
}
|
||||
|
||||
// Global middleware
|
||||
e.Use(middleware.ServerHandler)
|
||||
|
||||
// Post routes
|
||||
posts := e.Group("/posts") // Routing group
|
||||
// Routes
|
||||
strapiRoutes(e, sources.StrapiService)
|
||||
|
||||
// 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)
|
||||
// Special routes
|
||||
e.GET("/api", func(c *echo.Context) error {
|
||||
// Load all routes
|
||||
// routes, _ := json.MarshalIndent(e.Router().Routes(), "", "")
|
||||
return c.JSON(http.StatusOK, e.Router().Routes())
|
||||
})
|
||||
}
|
||||
|
||||
// Setup Strapi routes
|
||||
func strapiRoutes(e *echo.Echo, s *services.StrapiService) {
|
||||
// Post routes
|
||||
posts := e.Group("/api/posts") // Routing group
|
||||
|
||||
// GET /api/posts/all
|
||||
posts.GET("/all", func(c *echo.Context) error {
|
||||
return handlers.GetAllPosts(c, s)
|
||||
})
|
||||
|
||||
// GET /api/posts/featured
|
||||
posts.GET("/featured", func(c *echo.Context) error {
|
||||
return handlers.GetFeaturedPosts(c, s)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user