kit

package module
v0.0.4 Latest Latest
Warning

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

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

README

go-kit

Essential infrastructure components for Go API development.

Installation

go get github.com/NSObjects/go-kit

Packages

Package Description
code Error code framework with HTTP status mapping
config Configuration management with hot-reload
log Structured logging with slog
resp Unified API response formatting
middleware Echo middleware (Error, Recovery, JWT, Casbin)
db Database connections (MySQL, PostgreSQL, SQLite, Redis, MongoDB)
health Component health checking
cache Redis cache abstraction
metrics Prometheus metrics
utils Common utilities
validator Custom validation extensions

Quick Start

package main

import (
    "github.com/NSObjects/go-kit/code"
    "github.com/NSObjects/go-kit/config"
    "github.com/NSObjects/go-kit/log"
    "github.com/NSObjects/go-kit/resp"
    "github.com/NSObjects/go-kit/middleware"
    "github.com/labstack/echo/v4"
)

func main() {
    e := echo.New()
    e.HTTPErrorHandler = middleware.ErrorHandler
    e.Use(middleware.Recovery())

    e.GET("/users", listUsers)
    e.Logger.Fatal(e.Start(":8080"))
}

func listUsers(c echo.Context) error {
    users := []User{{ID: 1, Name: "Alice"}}
    return resp.ListDataResponse(c, users, 1)
}

Database Configuration

Supports MySQL, PostgreSQL, and SQLite via GORM.

MySQL
[database]
driver = "mysql"
host = "localhost"
port = 3306
user = "root"
password = "secret"
database = "myapp"
charset = "utf8mb4"
timezone = "Local"
max_idle_conns = 10
max_open_conns = 100
max_lifetime = 300
conn_max_idle_time = 60
PostgreSQL
[database]
driver = "postgres"
host = "localhost"
port = 5432
user = "postgres"
password = "secret"
database = "myapp"
ssl_mode = "disable"
schema = "public"
timezone = "Asia/Shanghai"
max_idle_conns = 10
max_open_conns = 100
max_lifetime = 300
conn_max_idle_time = 60
SQLite
[database]
driver = "sqlite"
database = "./data.db"  # or ":memory:"
max_idle_conns = 1
max_open_conns = 1
Usage
cfg := config.Load[config.Config]("config.toml")
db, err := db.NewDatabase(cfg.Database, os.Stdout)

Version

v1.0.0

Documentation

Overview

Package kit provides essential infrastructure components for Go API development.

go-kit is a collection of reusable packages for building production-ready Go applications. It includes error handling, configuration management, logging, middleware, and database connectivity.

Core Packages

  • code: Error code framework with HTTP status mapping
  • config: Configuration loading with hot-reload support
  • log: Structured logging with multiple sinks
  • resp: Unified API response formatting
  • middleware: Echo middleware (JWT, Casbin, CORS, Recovery)
  • db: Database connection management (MySQL, Redis, MongoDB, Kafka)
  • health: Component health checking
  • cache: Redis cache abstraction
  • metrics: Prometheus metrics collection
  • utils: Common utilities
  • validator: Custom validation extensions
  • fx: Uber Fx module integrations

Quick Start

import (
    "github.com/NSObjects/go-kit/code"
    "github.com/NSObjects/go-kit/config"
    "github.com/NSObjects/go-kit/log"
)

func main() {
    cfg := config.Load("config.yaml")
    logger := log.New(cfg.Log)

    if err := doSomething(); err != nil {
        return code.WrapDatabaseError(err, "operation failed")
    }
}

Version

v1.0.0 - Stable release

Directories

Path Synopsis
Package cache provides Redis cache abstraction.
Package cache provides Redis cache abstraction.
Package code provides business error codes with HTTP status mapping.
Package code provides business error codes with HTTP status mapping.
Package config provides configuration management with hot-reload support.
Package config provides configuration management with hot-reload support.
Package db provides database connection management.
Package db provides database connection management.
Package errors provides error handling primitives for Go 1.21+.
Package errors provides error handling primitives for Go 1.21+.
Package health provides component health checking.
Package health provides component health checking.
Package log provides structured logging with multiple sink support.
Package log provides structured logging with multiple sink support.
Package metrics provides Prometheus metrics collection.
Package metrics provides Prometheus metrics collection.
Package middleware provides Echo middleware implementations.
Package middleware provides Echo middleware implementations.
Package resp provides unified API response formatting.
Package resp provides unified API response formatting.
Package utils provides common utility functions.
Package utils provides common utility functions.
Package validator provides custom validation extensions.
Package validator provides custom validation extensions.

Jump to

Keyboard shortcuts

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