gin-utils

module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT

README

gin-utils

Tools and template for constructing applications using gin.

Can provide:

  • Graceful shutdown of gin server
  • Redirection of gin log messages to zerolog
  • Some simple gin handlers
  • Template application using gin and zerolog
  • System utility:
    • Configuration struct with JSON and YAML struct tags.

Go Report Card GitHub GitHub release (latest by date) Go Reference

Graceful Shutdown

Support graceful shutdown of gin during an interrupt signal. This tool captures the Linux interrupt and kill signals, so it won't work (completely) with Apple or Windows.

There is a demo program located in demo/shutdown/shutdown.go.

See package shutdown documentation for more details.

Logging via zerolog

Support for connecting gin logging to zerolog. This includes request-logging middleware and the capture and reprocessing of stderr and stdout streams.

There is a demo program located in demo/ginzero/ginzero.go.

See package ginzero documentation for more details.

Handlers

Various support elements for configuring gin handlers are described in the following sections.

See package handler documentation for more details.

Simple Handlers

A small collection of simpler gin handlers is provided in the handler package. These include:

  • Ping handler to return a 200 "Pong!" response.
  • Exit handler to send a SIGINT signal to the current process, thereby ending the service.
Handler Wrapper

The handler.Wrapper mechanism can encapsulate an arbitrary object instantiating handler.CanServe. Once created, this object can return a gin.HandlerFunc to be used when configuring gin. The main use is to pass in options to be invoked within the handler function thus returned, which at this time is just a Cross-Origin Resource Sharing (CORS) flag.

Results

The handler.ErrorResult function will return a generic error page.

Application Template

There is an executable template application in cmd/template/template.go.

The template utilizes:

  • ginzero to redirect all gin messaging through zerolog,
  • shutdown to implement graceful shutdown, and
  • handler for a few simple handlers (remove later)

This template is intended to be copied into another project as a starting point.

System Utility

Configuration

The system.Config struct collects gin configuration items in one place. At the current time there is only Port.

The Config.AddFlagsToSet() method will configure a port flag in the specified flag.FlagSet.

Directories

Path Synopsis
cmd
demo
ginzero
demo of ginzero package to support using [zerolog] with [gin].
demo of ginzero package to support using [zerolog] with [gin].
shutdown
demo of shutdown package to support graceful server shutdown.
demo of shutdown package to support graceful server shutdown.
pkg
ginzero
Package ginzero provides tools for using [zerolog] from within [gin] applications.
Package ginzero provides tools for using [zerolog] from within [gin] applications.
handler
Package handler provides a small set of simple handlers.
Package handler provides a small set of simple handlers.
shutdown
Package shutdown provides a mechanism for graceful shutdown of [gin].
Package shutdown provides a mechanism for graceful shutdown of [gin].
system
Package system provides system-level utilities.
Package system provides system-level utilities.

Jump to

Keyboard shortcuts

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