routek

package module
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 13 Imported by: 0

README

routek - YAML-based HTTP Router

🛤️ Custom Konsultin YAML-based HTTP router for fasthttp with automatic handler binding.

Installation

go get github.com/konsultin/routek

Quick Start

# api-route.yaml
users:
  route:
    - get: /v1/users
      handler: List
    - get: /v1/users/{id}
      handler: GetByID
    - post: /v1/users
      handler: Create
import (
    "github.com/konsultin/routek"
    "github.com/valyala/fasthttp"
)

// Create handler struct
type UserHandler struct {
    service *UserService
}

func (h *UserHandler) List(ctx *fasthttp.RequestCtx) error {
    users, err := h.service.List()
    return err
}

// Initialize router
router, err := routek.NewRouter(routek.Config{
    RouteFile: "api-route.yaml",
    Handlers: map[string]any{
        "users": &UserHandler{service: userService},
    },
})

fasthttp.ListenAndServe(":8080", router.Handler)

Features

  • YAML Configuration - Define routes in external file
  • Automatic Handler Binding - Map handlers by method name
  • fasthttp Integration - Built on high-performance fasthttp
  • JSON Responder - Built-in response helper
  • Multiple HTTP Methods - GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS

License

MIT License - see LICENSE

Documentation

Index

Constants

View Source
const (
	// DefaultRouteFile is used when Config.RouteFile is empty.
	DefaultRouteFile = "internal/api-route.yaml"
)

Variables

This section is empty.

Functions

func NewRouter

func NewRouter(cfg Config) (*router.Router, error)

Types

type Code

type Code string

Code represents API response codes

const (
	CodeOK            Code = "OK"
	CodeCreated       Code = "CREATED"
	CodeBadRequest    Code = "BAD_REQUEST"
	CodeUnauthorized  Code = "UNAUTHORIZED"
	CodeForbidden     Code = "FORBIDDEN"
	CodeNotFound      Code = "NOT_FOUND"
	CodeConflict      Code = "CONFLICT"
	CodeInternalError Code = "INTERNAL_ERROR"
)

Common response codes

type Config

type Config struct {
	// RouteFile is the path to api-route.yaml. If empty, routek searches a few sensible defaults.
	RouteFile string
	Handlers  map[string]any
	Responder *Responder
}

type Responder

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

func NewResponder

func NewResponder(debug bool) *Responder

NewResponder creates a responder; debug=true will include error details in responses.

func (*Responder) Error

func (r *Responder) Error(ctx *fasthttp.RequestCtx, status int, code Code, message string, err error)

Error standardizes error responses.

func (*Responder) Success

func (r *Responder) Success(ctx *fasthttp.RequestCtx, status int, code Code, message string, data any)

Success sends a successful Response with the given status, code, message, and payload data.

type Response

type Response[T any] struct {
	Message   string `json:"message"`
	Code      Code   `json:"code"`
	Data      T      `json:"data"`
	Timestamp int64  `json:"timestamp"`
}

Response is the standard API response structure

Jump to

Keyboard shortcuts

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