ki

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 12 Imported by: 0

README

ki

Thin wrapper around the Go standard Mux.

Installation

go get -u github.com/throskam/ki

Features

  • Compatible with the Go standard Mux
  • Method based routing
  • Middlewares (see middlewares)
  • Sub-routers
  • Groups
  • Named routes

Usage

package main

import (
	"net/http"

	"github.com/throskam/ki"
	"github.com/throskam/ki/middlewares"
)

func main() {
	router := ki.NewRouter()

	router.Use(middlewares.RequestID())
	router.Use(middlewares.RealIP())
	router.Use(middlewares.RequestLogger())
	router.Use(middlewares.Recoverer())

	router.Get("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("Hello, World!"))
	})

	_ = http.ListenAndServe(":8080", router)
}


see examples for examples.

License

MIT

Documentation

Overview

Package ki is a thin wrapper around the standard Go mux.

Index

Constants

This section is empty.

Variables

View Source
var Logger *slog.Logger

Logger is the global logger.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) string

GetRequestID returns the request ID from the context. Use with RequestID middleware.

func MustGetLanguage

func MustGetLanguage(ctx context.Context) language.Tag

MustGetLanguage returns the language from the context. If the language is not set, it panics. Use with Language middleware.

func MustGetLogger

func MustGetLogger(ctx context.Context) *slog.Logger

MustGetLogger returns the logger from the context. If the logger is not set, it panics. Use with Logger middleware.

func SetLanguage

func SetLanguage(ctx context.Context, lang language.Tag) context.Context

SetLanguage sets the language in the context.

func SetLogger

func SetLogger(ctx context.Context, logger *slog.Logger) context.Context

SetLogger sets the logger in the context.

func SetLoggerLevelByText

func SetLoggerLevelByText(s string)

SetLoggerLevelByText sets the logger level to the given text level. The text level must be one of the following: debug, info, warn, error.

func SetRegistry

func SetRegistry(ctx context.Context, registry *Registry) context.Context

SetRegistry sets the registry in the context.

func SetRequestID

func SetRequestID(ctx context.Context, requestID string) context.Context

SetRequestID sets the request ID in the context.

Types

type BufferedResponseWriter

type BufferedResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

BufferedResponseWriter is a response writer that buffers the response.

func NewBufferedResponseWriter

func NewBufferedResponseWriter(w http.ResponseWriter) *BufferedResponseWriter

NewBufferedResponseWriter returns a new BufferedResponseWriter.

func (*BufferedResponseWriter) Flush

func (w *BufferedResponseWriter) Flush() (int, error)

Flush writes the buffered response to the underlying response writer.

func (*BufferedResponseWriter) Header

func (w *BufferedResponseWriter) Header() http.Header

Header returns the header of the response.

func (*BufferedResponseWriter) Size

func (w *BufferedResponseWriter) Size() int

Size returns the size of the buffered response.

func (*BufferedResponseWriter) StatusCode

func (w *BufferedResponseWriter) StatusCode() int

StatusCode returns the status code of the response.

func (*BufferedResponseWriter) Write

func (w *BufferedResponseWriter) Write(b []byte) (int, error)

Write writes the given bytes to the buffer if the response has not been flushed or the response has already otherwise.

func (*BufferedResponseWriter) WriteHeader

func (w *BufferedResponseWriter) WriteHeader(statusCode int)

WriteHeader sets the status code of the response.

type Location

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

Location represents a resolved route.

func GetLocation

func GetLocation(ctx context.Context, key string) Location

GetLocation returns the location for the given key from the registry in the context. Use with Locator middleware.

func NewLocation

func NewLocation(method, pattern string) Location

NewLocation returns a new Location.

func (Location) Method

func (l Location) Method() string

Method returns the method of the route.

func (Location) Pattern

func (l Location) Pattern() string

Pattern returns the pattern of the route.

func (Location) URL

func (l Location) URL() *url.URL

URL returns the parameterized URL. It panics if the URL is invalid.

func (Location) WithPathParams

func (l Location) WithPathParams(params ...string) Location

WithPathParams returns a new Location with the path parameters.

func (Location) WithPrefix

func (l Location) WithPrefix(prefix string) Location

WithPrefix returns a new Location with the prefix.

func (Location) WithQuery

func (l Location) WithQuery(query url.Values) Location

WithQuery returns a new Location with the query.

func (Location) WithQueryParam

func (l Location) WithQueryParam(key, value string) Location

WithQueryParam returns a new Location with the query parameter.

type Mux

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

Mux is a router that uses a ServeMux.

func NewMux

func NewMux() *Mux

NewMux returns a new Mux.

func NewRouter

func NewRouter() *Mux

NewRouter returns a new Router.

func (*Mux) Delete

func (m *Mux) Delete(pattern string, handler http.HandlerFunc, options ...RouteOption) Location

Delete adds a route for the DELETE verb.

func (*Mux) Get

func (m *Mux) Get(pattern string, handler http.HandlerFunc, options ...RouteOption) Location

Get adds a route for the GET verb.

func (*Mux) Group

func (m *Mux) Group(fn func(Router)) Router

Group creates a new router without any prefix.

func (*Mux) Method

func (m *Mux) Method(method, pattern string, handler http.HandlerFunc, options ...RouteOption) Location

Method adds a route for the given verb.

func (*Mux) Mount

func (m *Mux) Mount(prefix string, handler http.Handler)

Mount mounts the given handler at the given prefix.

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handler http.HandlerFunc, options ...RouteOption) Location

Patch adds a route for the PATCH verb.

func (*Mux) Post

func (m *Mux) Post(pattern string, handler http.HandlerFunc, options ...RouteOption) Location

Post adds a route for the POST verb.

func (*Mux) Put

func (m *Mux) Put(pattern string, handler http.HandlerFunc, options ...RouteOption) Location

Put adds a route for the PUT verb.

func (*Mux) Registry

func (m *Mux) Registry() *Registry

Registry returns the registry of the router.

func (*Mux) Route

func (m *Mux) Route(prefix string, fn func(Router)) Router

Route creates a new router with the given prefix.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (*Mux) Use

func (m *Mux) Use(middlewares ...func(http.Handler) http.Handler)

Use adds the given middlewares to the router.

type Registry

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

Registry is a registry of routes.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a new Registry.

func (*Registry) Add

func (r *Registry) Add(key, method, pattern string)

Add adds a route to the registry. It panics if the route already exists.

func (*Registry) Child

func (r *Registry) Child(prefix string) *Registry

Child returns a new child registry with the given prefix.

func (*Registry) Get

func (r *Registry) Get(key string) Location

Get returns the location for the given key. It panics if the location does not exist.

func (*Registry) Has

func (r *Registry) Has(key string) bool

Has returns true if the registry has a route with the given key or any of its child registries.

func (*Registry) Remove

func (r *Registry) Remove(key string)

Remove removes a route from the registry.

type Route

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

Route represents a route.

func NewRoute

func NewRoute(method, path string, handler http.Handler, options ...RouteOption) Route

NewRoute returns a new Route.

func (*Route) Handler

func (r *Route) Handler() http.Handler

Handler builds the handler for the route including the middlewares.

func (*Route) Location

func (r *Route) Location() Location

Location returns a new Location for the route.

func (*Route) Method

func (r *Route) Method() string

Method returns the method of the route.

func (*Route) Name

func (r *Route) Name() string

Name returns the name of the route.

func (*Route) Path

func (r *Route) Path() string

Path returns the path of the route.

func (*Route) Pattern

func (r *Route) Pattern() string

Pattern returns the pattern of the route.

type RouteOption

type RouteOption func(*Route)

RouteOption is a function that configures a Route.

func WithMiddleware

func WithMiddleware(middlewares ...func(http.Handler) http.Handler) RouteOption

WithMiddleware returns a new RouteOption that sets the middlewares for the route.

func WithName

func WithName(name string) RouteOption

WithName returns a new RouteOption that sets the name of the route.

type Router

type Router interface {
	ServeHTTP(w http.ResponseWriter, req *http.Request)

	// Mount mounts the given handler at the given prefix.
	Mount(prefix string, handler http.Handler)

	// Route creates a new router with the given prefix.
	Route(prefix string, fn func(Router)) Router

	// Group creates a new router without any prefix.
	// It is useful for adding middlewares to a group of routes.
	Group(fn func(Router)) Router

	// Use adds the given middlewares to the router.
	Use(middlewares ...func(http.Handler) http.Handler)

	// Method adds a route for the given verb.
	Method(method, pattern string, handler http.HandlerFunc, options ...RouteOption) Location

	// HTTP routing methods.
	Get(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
	Post(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
	Put(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
	Patch(pattern string, handler http.HandlerFunc, options ...RouteOption) Location
	Delete(pattern string, handler http.HandlerFunc, options ...RouteOption) Location

	// Registry returns the registry of the router.
	Registry() *Registry
}

Router is a router. It provides a way to mount handlers, define routes, and use middlewares.

type Stack

type Stack []func(http.Handler) http.Handler

Stack is a stack of middlewares.

func (*Stack) Chain

func (s *Stack) Chain(handler http.Handler) http.Handler

Chain returns the chained handler.

Directories

Path Synopsis
examples
basic command
named_route command
routing command
Package middlewares provides middlewares for the ki router.
Package middlewares provides middlewares for the ki router.

Jump to

Keyboard shortcuts

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