Skip to content

andreascandle/FlexiResponseGo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FlexiResponseGo

Go Report Card GoDoc Coverage Status MIT License GitHub release Issues Pull Requests Contributors Code Size Lines of Code Last Commit

Streamline API Response Management in Go

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.

Features

  • 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.
  • Framework Support:

    • Adapters for popular Go frameworks:
      • Fiber
      • Gin
      • Echo
      • Native HTTP
  • Observability:

    • Metrics collection with Prometheus.
    • Distributed tracing with OpenTelemetry.
  • Extensibility:

    • Easy integration into new frameworks.
    • Centralized configuration and dynamic updates.

Features

  • 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.
  • 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:
  • Dynamic Configuration:
    • Runtime settings for metadata, logging levels, and behaviors.

Installation

Install FlexiResponseGo via go get:

go get github.com/andreascandle/FlexiResponseGo

Installation

import "github.com/andreascandle/FlexiResponseGo"

Usage

1. Setup Configuration

Update global settings for metadata and logging:

import "github.com/andreascandle/FlexiResponseGo/config"

func main() {
    conf := config.GetConfig()
    conf.UpdateMetadata("serviceName", "MyService")
    conf.UpdateLogLevel("debug")
}

2. Framework-Specific Adapters

Fiber Example:

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")
}

Gin Example:

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")
}

Echo Example:

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")
}

net/http

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)
}

Observability

  • Distributed Tracing: Add tracing using OpenTelemetry.
  • Metrics Tracking: Export metrics to Prometheus for better API monitoring.

Prometheus Metrics

Expose metrics at /metrics:

import (
    "github.com/andreascandle/FlexiResponseGo/observability"
    "net/http"
)

func main() {
    http.Handle("/metrics", observability.HTTPHandlerForMetrics())
    http.ListenAndServe(":9090", nil)
}

OpenTelemetry Tracing

Initialize tracing for your service:

import "github.com/andreascandle/FlexiResponseGo/observability"

func main() {
    shutdown := observability.InitTracer("MyService")
    defer shutdown()
}

Testing

Run the test suite using:

go test ./tests/... -v

Ensure you have all dependencies installed for full testing.

Contributing

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.

How to Contribute

  • Fork the repository.
  • Create a feature branch.
  • Submit a pull request with detailed descriptions of changes.

License

This project is licensed under the MIT License.

Acknowledgments

Special thanks to the Go developer community and contributors to:

  • Gorilla Mux
  • Fiber
  • Gin
  • Echo
  • OpenTelemetry
  • Prometheus
  • less

About

Streamline API Response Management in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages