ginhelper

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2024 License: MIT Imports: 27 Imported by: 19

README

Gin Helper

Note: gindump utilities are courtesy of this repo and have only been updated/modified here because of dependency issues.

  • Gin middleware/handler to dump header/body of request and response .

  • Very helpful for debugging your applications.

  • More beautiful output than httputil.DumpXXX()

Content-type support / todo

  • application/json
  • application/x-www-form-urlencoded
  • text/xml
  • application/xml
  • text/plain

Usage

All:

func main() {
    router := gin.Default()

    //use Dump() default will print on stdout
    router.Use(gindump.Dump())

    //or use DumpWithOptions() with more options
    router.Use(gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) {
	    fmt.Println(dumpStr)
    }))

    router.Post("/",myHandler)

    ...

    router.Run()
}

Group:

func main() {
    router := gin.Default()

    dumpGroup := router.Group("/group")

    //use Dump() default will print on stdout
    dumpGroup.Use(gindump.Dump())

    //or use DumpWithOptions() with more options
    dumpGroup.Use(gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) {
	    fmt.Println(dumpStr)
    }))

    dumpGroup.Post("/",myHandler)

    ...

    router.Run()
}

EndPoint:

func main() {
    router := gin.Default()

    //use Dump() default will print on stdout
    router.Post("/",gindump.Dump(),myHandler)

    //or use DumpWithOptions() with more options
    router.Post("/",gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) {
	    fmt.Println(dumpStr)
    }),myHandler)

    ...

    router.Run()
}
Output is as follows
[GIN-dump]:
Request-Header:
{
    "Content-Type": [
        "application/x-www-form-urlencoded"
    ]
}
Request-Body:
{
    "bar": [
        "baz"
    ],
    "foo": [
        "bar",
        "bar2"
    ]
}
Response-Header:
{
    "Content-Type": [
        "application/json; charset=utf-8"
    ]
}
Response-Body:
{
    "data": {
        "addr": "tpkeeper@qq.com",
        "name": "jfise"
    },
    "ok": true
}

Documentation

Overview

Package ginhelper provides some common functionality across gin servers.

Index

Constants

View Source
const HealthCheck string = "/health-check"

HealthCheck is the health check endpoint.

View Source
const MetricsEndpoint string = "/metrics"

MetricsEndpoint is used for prometheus metrics. Deprecated: use METRICS_PATH instead.

View Source
const RequestIDHeader = "X-Request-ID"

RequestIDHeader is used for tracking request ids.

View Source
const RobotsTxt string = "/robots.txt"

RobotsTxt is used for apis to disallow crawls.

Variables

View Source
var Indent = 4

Indent is the number of spaces to indent.

View Source
var Newline = "\n"

Newline is the string to use for newlines.

View Source
var StringMaxLength = 0

StringMaxLength is the maximum length of a string to display.

Functions

func BeautifyJSONBytes added in v0.5.2

func BeautifyJSONBytes(data []byte, hiddenFields []string) ([]byte, error)

BeautifyJSONBytes beautifies json bytes.

func Dump added in v0.5.2

func Dump() gin.HandlerFunc

Dump dumps the request and response.

func DumpWithOptions added in v0.5.2

func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders bool, showCookies bool, cb func(dumpStr string)) gin.HandlerFunc

DumpWithOptions dumps the request and response with options. nolint: gocognit, nestif, cyclop

func FormatToBeautifulJSON added in v0.5.2

func FormatToBeautifulJSON(v interface{}, hiddenFields []string) ([]byte, error)

FormatToBeautifulJSON dumps v to beautified json bytes.

func New

func New(logger *log.ZapEventLogger) *gin.Engine

New creates a new gin server with some sensible defaults. these include: - helmet-default handlers - request-ids (used for stack tracing) - cors (used for requests from the frontend) - health-checks - restrictive robots.txt.

func NewWithExperimentalLogger added in v0.1.12

func NewWithExperimentalLogger(ctx context.Context, logger logger.ExperimentalLogger) *gin.Engine

NewWithExperimentalLogger creates a new gin server with some sensible defaults. See New for more information.

func TraceMiddleware added in v0.5.2

func TraceMiddleware(tracer trace.Tracer, printStr bool) gin.HandlerFunc

TraceMiddleware is a middleware that traces the request/response.

func WaitForStart added in v0.8.0

func WaitForStart(parentCtx context.Context, port int) error

WaitForStart waits for the connection to be ready on a ginhelper like server times out after serverStartTimeout.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL