httpgin

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 9 Imported by: 0

README

Things-Kit HTTP Gin

Gin-based HTTP Server for Things-Kit

This is the default HTTP server implementation for Things-Kit, built on the Gin framework.

Installation

go get github.com/things-kit/things-kit-httpgin

Features

  • Automatic HTTP server lifecycle management
  • Gin framework integration
  • Configuration via Viper or environment variables
  • Graceful startup and shutdown
  • Health check endpoint

Quick Start

package main

import (
    "github.com/things-kit/things-kit/app"
    "github.com/things-kit/things-kit/logging"
    "github.com/things-kit/things-kit/viperconfig"
    "github.com/things-kit/things-kit-httpgin"
    httpmodule "github.com/things-kit/things-kit-http"
)

func main() {
    app.New(
        viperconfig.Module,
        logging.Module,
        httpgin.Module,
        fx.Invoke(RegisterRoutes),
    ).Run()
}

func RegisterRoutes(server httpmodule.Server) {
    engine := server.GetEngine().(*gin.Engine)
    
    engine.GET("/hello", func(c *gin.Context) {
        c.JSON(200, gin.H{"message": "Hello, World!"})
    })
}

Configuration

Via config.yaml:

http:
  port: 8080
  mode: debug  # or "release"

Or environment variables:

export HTTP_PORT=8080
export HTTP_MODE=release

Built-in Endpoints

  • GET /health - Health check endpoint (always returns 200 OK)

Advanced Usage

Access the Gin engine directly for middleware and advanced routing:

func RegisterMiddleware(server httpmodule.Server) {
    engine := server.GetEngine().(*gin.Engine)
    
    engine.Use(gin.Recovery())
    engine.Use(gin.Logger())
    
    // Your custom middleware
    engine.Use(func(c *gin.Context) {
        // middleware logic
        c.Next()
    })
}

License

MIT License - see LICENSE file for details

Documentation

Overview

Package httpgin provides a Gin-based implementation of the Things-Kit HTTP server interface. This is the default HTTP implementation for Things-Kit, but users can provide their own implementations using different frameworks (Chi, Echo, stdlib, etc.).

Index

Constants

This section is empty.

Variables

Module provides the Gin-based HTTP server module to the application. This module implements the http.Server interface using the Gin framework.

Functions

func AsGinHandler

func AsGinHandler(constructor any) fx.Option

AsGinHandler is a generic helper to provide a Gin HTTP handler to the Fx graph. The constructor should return a type that implements the GinHandler interface.

Example:

type MyHandler struct {
    logger log.Logger
}

func NewMyHandler(logger log.Logger) *MyHandler {
    return &MyHandler{logger: logger}
}

func (h *MyHandler) RegisterRoutes(engine *gin.Engine) {
    engine.GET("/hello", func(c *gin.Context) {
        c.JSON(200, gin.H{"message": "Hello World"})
    })
}

// In main.go:
httpgin.AsGinHandler(NewMyHandler)

func RunHttpServer

func RunHttpServer(p HttpServerParams, server *GinServer)

RunHttpServer starts the HTTP server with registered handlers. This is invoked by Fx during application startup.

Types

type Config

type Config struct {
	httpmodule.Config `mapstructure:",squash"`
	Mode              string `mapstructure:"mode"` // debug, release, test
}

Config holds the Gin-specific HTTP server configuration. It embeds the common http.Config and adds Gin-specific options.

func NewConfig

func NewConfig(v *viper.Viper) *Config

NewConfig creates a new Gin HTTP configuration from Viper.

type GinHandler

type GinHandler interface {
	RegisterRoutes(engine *gin.Engine)
}

GinHandler is a Gin-specific implementation of http.Handler. Handlers that implement this interface can be registered with AsGinHandler.

type GinServer

type GinServer struct {
	// contains filtered or unexported fields
}

GinServer implements the http.Server interface using Gin.

func NewGinServer

func NewGinServer(config *Config, logger log.Logger) *GinServer

NewGinServer creates a new Gin server instance.

func (*GinServer) Addr

func (s *GinServer) Addr() string

Addr implements http.Server.Addr

func (*GinServer) Engine

func (s *GinServer) Engine() *gin.Engine

Engine returns the underlying Gin engine. This is useful for registering middleware or customizing the engine.

func (*GinServer) Start

func (s *GinServer) Start(ctx context.Context) error

Start implements http.Server.Start

func (*GinServer) Stop

func (s *GinServer) Stop(ctx context.Context) error

Stop implements http.Server.Stop

type HttpServerParams

type HttpServerParams struct {
	fx.In
	Lifecycle fx.Lifecycle
	Logger    log.Logger
	Config    *Config
	Handlers  []GinHandler `group:"http.handlers"`
}

HttpServerParams contains all dependencies needed to run the HTTP server.

Jump to

Keyboard shortcuts

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