FlexiResponseGo is a robust and extensible shared library for managing HTTP responses in Go projects. It standardizes response structures, integrates structured logging, and supports multiple web frameworks, including Fiber, Gin, Echo, and native HTTP. The library also includes built-in support for observability with Prometheus metrics and OpenTelemetry tracing.
-
Standardized Responses:
- Unified success and error response formats across all frameworks.
- Supports metadata, field-specific validation errors, and trace IDs.
-
Logging Integration:
- Structured logging using
zap
. - Automatic logging of incoming requests and outgoing responses.
- Structured logging using
-
Framework Support:
- Adapters for popular Go frameworks:
- Fiber
- Gin
- Echo
- Native HTTP
- Adapters for popular Go frameworks:
-
Observability:
- Metrics collection with Prometheus.
- Distributed tracing with OpenTelemetry.
-
Extensibility:
- Easy integration into new frameworks.
- Centralized configuration and dynamic updates.
- Standardized Responses:
- Unified success and error response formats across all frameworks.
- Supports metadata, field-specific validation errors, and trace IDs.
- Logging Integration:
- Structured logging using
zap
. - Automatic logging of incoming requests and outgoing responses.
- Structured logging using
- Framework Agnostic: Seamless integration with:
- Observability:
- Metrics collection with Prometheus.
- Distributed tracing with OpenTelemetry.
- Extensibility:
- Easy integration into new frameworks.
- Centralized configuration and dynamic updates.
- Advanced Error Management:
- Categorized errors with extensible codes and sanitization.
- Performance Optimized:
- High-speed JSON serialization using json-iterator/go.
- Dynamic Configuration:
- Runtime settings for metadata, logging levels, and behaviors.
Install FlexiResponseGo via go get
:
go get github.com/andreascandle/FlexiResponseGo
import "github.com/andreascandle/FlexiResponseGo"
import "github.com/andreascandle/FlexiResponseGo/config"
func main() {
conf := config.GetConfig()
conf.UpdateMetadata("serviceName", "MyService")
conf.UpdateLogLevel("debug")
}
import (
"github.com/andreascandle/FlexiResponseGo/adapters"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return adapters.FiberSuccessResponse(c, "Welcome to FlexiResponseGo!", map[string]string{"status": "ok"})
})
app.Listen(":3000")
}
import (
"github.com/andreascandle/FlexiResponseGo/adapters"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
adapters.GinSuccessResponse(c, "Welcome to FlexiResponseGo!", map[string]string{"status": "ok"})
})
r.Run(":3000")
}
import (
"github.com/andreascandle/FlexiResponseGo/adapters"
"github.com/labstack/echo/v4"
"net/http"
)
func main() {
e := echo.New()
e.GET("/success", func(c echo.Context) error {
return adapters.EchoSuccessResponse(c, "Operation successful", map[string]string{"example": "echo"})
})
e.Start(":3000")
}
import (
"net/http"
"response/adapters"
)
func main() {
http.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
return adapters.EchoSuccessResponse(c, "Operation successful", map[string]string{"example": "echo"})
})
http.ListenAndServe(":8080", nil)
}
- Distributed Tracing: Add tracing using OpenTelemetry.
- Metrics Tracking: Export metrics to Prometheus for better API monitoring.
Expose metrics at /metrics:
import (
"github.com/andreascandle/FlexiResponseGo/observability"
"net/http"
)
func main() {
http.Handle("/metrics", observability.HTTPHandlerForMetrics())
http.ListenAndServe(":9090", nil)
}
Initialize tracing for your service:
import "github.com/andreascandle/FlexiResponseGo/observability"
func main() {
shutdown := observability.InitTracer("MyService")
defer shutdown()
}
Run the test suite using:
go test ./tests/... -v
Ensure you have all dependencies installed for full testing.
We welcome contributions to FlexiResponseGo! If you’d like to report an issue, suggest a feature, or submit a code change, follow the guidelines below.
- Fork the repository.
- Create a feature branch.
- Submit a pull request with detailed descriptions of changes.
This project is licensed under the MIT License.
Special thanks to the Go developer community and contributors to:
- Gorilla Mux
- Fiber
- Gin
- Echo
- OpenTelemetry
- Prometheus
- less