thttp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2026 License: MIT Imports: 17 Imported by: 0

README

Qucik Start

Install

go get github.com/isayme/go-thttp@latest

Usage

package main

import (
	"log"

	"github.com/isayme/go-thttp"
)

func main() {
	app := thttp.New()

	// curl http://127.0.0.1:8080/version
	app.Get("/version", func(ctx thttp.Context) error {
		return ctx.JSON(200, map[string]string{
			"version": "v1.0.0",
		})
	})

	// curl http://127.0.0.1:8080/tasks/123
	app.Get("/tasks/:tid", func(ctx thttp.Context) error {
		return ctx.JSON(200, map[string]string{
			"tid": ctx.PathParam("tid"),
		})
	})

	// group route
	g := app.Group("/v1")

	// curl http://127.0.0.1:8080/v1/notes/123
	g.Get("/notes/{nid}", func(ctx thttp.Context) error {
		return ctx.JSON(200, map[string]string{
			"nid": ctx.PathParam("nid"),
		})
	})

	// curl http://127.0.0.1:8080/static/index.html
	// curl http://127.0.0.1:8080/static/img/favicon.ico
	app.Get("/static/*path", func(ctx thttp.Context) error {
		return ctx.JSON(200, map[string]string{
			"path": ctx.PathParam("path"),
		})
	})

	addr := ":8080"
	log.Printf("server start, listen on %s", addr)
	log.Fatal(app.Start(addr))
}

Route Pattern

Static

// match /users
app.Get("/users")

Param

// match /users/abc
// match /users/123
app.Get("/users/:id")
app.Get("/users/{id}")

regex is not supported.

Catch-All (wildcard)

// match /users/abc
// match /users/abc/def/ghi
app.Get("/users/*others")
app.Get("/users/{others...}")

// this is not supported
app.Get("/users/*")

Documentation

Index

Constants

View Source
const (
	HeaderContentType        = "Content-Type"
	HeaderXRequestID         = "X-Request-ID"
	HeaderLocation           = "Location"
	HeaderContentDisposition = "Content-Disposition"
)
View Source
const (
	ContextKey contextKey = iota
	PathParamsCtxKey
	CatchAllPathParamCtxKey
	PathRawParamsCtxKey
	HandlerFoundKey
	RequestIDKey
	HandlerKey
	LoggerCtxKey
)
View Source
const (
	MIMEApplicationJSON = "application/json"
)

Variables

This section is empty.

Functions

func SetHandlerInCtx

func SetHandlerInCtx(ctx Context, h HandlerFunc)

func WithErrorHandler

func WithErrorHandler(handler func(Context, error) error) optionFunc

func WithNotFoundHandler

func WithNotFoundHandler(handler HandlerFunc) optionFunc

func WithPrefix

func WithPrefix(prefix string) optionFunc

func WithRouterType

func WithRouterType(typ RouterType) optionFunc

Types

type App

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

func New

func New(options ...optionFunc) *App

func (*App) Any

func (app *App) Any(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Delete

func (app *App) Delete(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) ErrorHandler

func (app *App) ErrorHandler(handler func(Context, error) error)

func (*App) Get

func (app *App) Get(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Group

func (app *App) Group(prefix string, middleware ...MiddlewareFunc) *Group

func (*App) Handle

func (app *App) Handle(method, pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Head

func (app *App) Head(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Logger

func (app *App) Logger() *slog.Logger

func (*App) NotFound

func (app *App) NotFound(handler HandlerFunc)

func (*App) Options

func (app *App) Options(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Patch

func (app *App) Patch(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Post

func (app *App) Post(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) Put

func (app *App) Put(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*App) ServeHTTP

func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*App) Start

func (app *App) Start(address string) error

func (*App) Static

func (app *App) Static(pattern string, root string)

func (*App) Use

func (app *App) Use(middleware ...MiddlewareFunc)

type ChiMux

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

func (*ChiMux) FormatSegment

func (router *ChiMux) FormatSegment(seg Segment) string

func (*ChiMux) Handle

func (router *ChiMux) Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

func (*ChiMux) Match

func (*ChiMux) Use

func (router *ChiMux) Use(middlewares ...MiddlewareFunc)

type Context

type Context interface {
	Context() context.Context

	Request() *http.Request
	SetRequest(r *http.Request)

	Response() http.ResponseWriter
	SetResponse(r http.ResponseWriter)

	Get(key interface{}) interface{}
	Set(key, value interface{})

	Method() string
	SetPathParam(fn PathParams)
	PathParam(name string) string
	QueryParam(name string) string
	QueryParams() url.Values
	QueryString() string
	FormParam(name string) string
	FormFile(name string) (*multipart.FileHeader, error)
	MultipartForm() (*multipart.Form, error)
	Cookie(name string) (*http.Cookie, error)
	Cookies() []*http.Cookie
	SetCookie(cookie *http.Cookie)
	Header(key string) string
	SetHeader(key string, value string)
	AddHeader(key string, value string)
	DelHeader(key string)

	Blob(code int, contentType string, b []byte) error
	JSON(code int, i interface{}) error
	String(code int, s string) error
	Stream(code int, contentType string, r io.Reader) error
	Redirect(code int, url string) error

	Logger() *slog.Logger
	Reset(r *http.Request, w http.ResponseWriter, logger *slog.Logger)
}

func MustGetContextFromRequest

func MustGetContextFromRequest(r *http.Request) Context

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request) Context

type EchoMux

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

func (*EchoMux) FormatSegment

func (router *EchoMux) FormatSegment(seg Segment) string

func (*EchoMux) Handle

func (router *EchoMux) Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

func (*EchoMux) Match

func (*EchoMux) Use

func (router *EchoMux) Use(middlewares ...MiddlewareFunc)

type ErrorHandlerFunc

type ErrorHandlerFunc func(ctx Context, err error) error

type GinMux

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

func (*GinMux) FormatSegment

func (router *GinMux) FormatSegment(seg Segment) string

func (*GinMux) Handle

func (router *GinMux) Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

func (*GinMux) Match

func (*GinMux) Use

func (router *GinMux) Use(middlewares ...MiddlewareFunc)

type GorillaMux

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

func (*GorillaMux) FormatSegment

func (router *GorillaMux) FormatSegment(seg Segment) string

func (*GorillaMux) Handle

func (router *GorillaMux) Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

func (*GorillaMux) Match

func (*GorillaMux) Use

func (router *GorillaMux) Use(middlewares ...MiddlewareFunc)

type Group

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

func (*Group) Any

func (g *Group) Any(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Delete

func (g *Group) Delete(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Get

func (g *Group) Get(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Group

func (g *Group) Group(prefix string, middleware ...MiddlewareFunc) *Group

func (*Group) Handle

func (g *Group) Handle(method, pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Head

func (g *Group) Head(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Options

func (g *Group) Options(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Patch

func (g *Group) Patch(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Post

func (g *Group) Post(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Put

func (g *Group) Put(pattern string, handler HandlerFunc, middleware ...MiddlewareFunc)

func (*Group) Use

func (g *Group) Use(middleware ...MiddlewareFunc)

type HandlerFunc

type HandlerFunc func(ctx Context) error

func MustGetHandlerFromCtx

func MustGetHandlerFromCtx(ctx Context) HandlerFunc

func WrapHandler

func WrapHandler(h http.Handler) HandlerFunc

WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.

func WrapHandlerFunc

func WrapHandlerFunc(h http.HandlerFunc) HandlerFunc

type HttpServeMux

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

func (*HttpServeMux) FormatSegment

func (router *HttpServeMux) FormatSegment(seg Segment) string

func (*HttpServeMux) Handle

func (router *HttpServeMux) Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

func (*HttpServeMux) Match

func (*HttpServeMux) Use

func (router *HttpServeMux) Use(middlewares ...MiddlewareFunc)

type HttprouterMux

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

func (*HttprouterMux) FormatSegment

func (router *HttprouterMux) FormatSegment(seg Segment) string

func (*HttprouterMux) Handle

func (router *HttprouterMux) Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

func (*HttprouterMux) Match

func (*HttprouterMux) Use

func (router *HttprouterMux) Use(middlewares ...MiddlewareFunc)

type HttprouterMuxPathParams

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

func (*HttprouterMuxPathParams) Get

func (pp *HttprouterMuxPathParams) Get(name string) string

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

func WrapMiddleware

func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc

type PathParams

type PathParams interface {
	Get(name string) string
}

func NewHttprouterMuxPathParams

func NewHttprouterMuxPathParams(ctx Context) PathParams

type PathParamsFunc

type PathParamsFunc func(ctx Context) PathParams

type Router

type Router interface {
	Use(middleware ...MiddlewareFunc)

	Handle(method, pattern string, h HandlerFunc, middleware ...MiddlewareFunc)

	Match(w http.ResponseWriter, r *http.Request) (HandlerFunc, PathParamsFunc, bool)

	FormatSegment(seg Segment) string
}

type RouterType

type RouterType string
const (
	RouterTypeStd        RouterType = "net/http"
	RouterTypeHttprouter RouterType = "julienschmidt/httprouter"
	RouterTypeGorillaMux RouterType = "gorilla/mux"
	RouterTypeGin        RouterType = "gin-gonic/gin"
	RouterTypeChi        RouterType = "go-chi/chi"
	RouterTypeEcho       RouterType = "labstack/echo"
)

type Segment

type Segment struct {
	Type SegmentType
	Raw  string
	Name string
}

func ParsePath

func ParsePath(pattern string) []Segment

ParsePath parse pattern

type SegmentType

type SegmentType int
const (
	Static   SegmentType = iota // static
	Param                       // param
	CatchAll                    // wildcard
)

type Skipper

type Skipper func(ctx Context) bool

Directories

Path Synopsis
errorhandler command
graceful command
recovery command
static command

Jump to

Keyboard shortcuts

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