router

package
v1.67.2 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2026 License: MIT Imports: 5 Imported by: 1

README

Package router

Пакет router предоставляет обертку над httprouter с поддержкой метрик и удобной регистрацией обработчиков. Интегрируется с системой мониторинга для отслеживания эндпоинтов.

Types

Router

Структура для управления HTTP-маршрутами. Обеспечивает регистрацию обработчиков, сбор метрик и работу с параметрами URL.

Methods:

New() *Router

Конструктор роутера.

(r *Router) GET(path string, handler http.Handler) *Router

Зарегистрировать обработчик для GET-запросов. Аналогичные методы: POST(), PUT(), DELETE().

(r *Router) Handler(method string, path string, handler http.Handler) *Router

Зарегистрировать обработчик для произвольного HTTP-метода.

(r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

Обработать HTTP-запрос.

(r *Router) InternalRouter() *httprouter.Router

Возвращает внутренний экземпляр httprouter.Router для кастомных настроек.

Functions

ParamsFromRequest(http *http.Request) Params

Извлечь URL-параметры из запроса.

ParamsFromContext(ctx context.Context) Params

Извлечь URL-параметры из контекста.

Usage

Default usage flow
package main

import (
	"log"
	"net/http"

	"github.com/txix-open/isp-kit/http/router"
)

func main() {
	r := router.New()
	r.GET("/users/:id", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := router.ParamsFromRequest(r)
		userId := params.ByName("id") /* get id param */
		log.Println(userId)
		w.WriteHeader(http.StatusOK)
	}))
	_ = http.ListenAndServe(":8080", r)
}

Documentation

Overview

Package router provides HTTP request routing with metrics integration. It wraps the julienschmidt/httprouter library and adds automatic endpoint metrics collection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Params

type Params = httprouter.Params

Params is an alias for httprouter.Params, representing URL path parameters.

func ParamsFromContext

func ParamsFromContext(ctx context.Context) Params

ParamsFromContext extracts URL path parameters from a context. It returns empty params if none are present in the context.

func ParamsFromRequest

func ParamsFromRequest(http *http.Request) Params

ParamsFromRequest extracts URL path parameters from an http.Request. It retrieves the parameters from the request context.

type Router

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

Router wraps httprouter.Router with automatic metrics collection for each endpoint. It supports method-based routing (GET, POST, PUT, DELETE) and fluent API for route configuration.

func New

func New() *Router

New creates a new Router instance with a fresh httprouter backend.

func (*Router) DELETE

func (r *Router) DELETE(path string, handler http.Handler) *Router

DELETE registers an HTTP DELETE handler for the specified path. Returns the Router for fluent chaining.

func (*Router) GET

func (r *Router) GET(path string, handler http.Handler) *Router

GET registers an HTTP GET handler for the specified path. Returns the Router for fluent chaining.

func (*Router) Handler

func (r *Router) Handler(method string, path string, handler http.Handler) *Router

Handler registers a handler for the specified HTTP method and path. It automatically wraps the handler with metrics collection. Returns the Router for fluent chaining.

func (*Router) InternalRouter

func (r *Router) InternalRouter() *httprouter.Router

InternalRouter returns the underlying httprouter instance for advanced configuration.

func (*Router) POST

func (r *Router) POST(path string, handler http.Handler) *Router

POST registers an HTTP POST handler for the specified path. Returns the Router for fluent chaining.

func (*Router) PUT

func (r *Router) PUT(path string, handler http.Handler) *Router

PUT registers an HTTP PUT handler for the specified path. Returns the Router for fluent chaining.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP implements the http.Handler interface and delegates to the underlying httprouter.

Jump to

Keyboard shortcuts

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