zhttp

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package zhttp is the HTTP layer the handlers are written against. The server runs on gin by default, `-tags fiber` switches it to fiber; both run the same handler chains with gin's middleware semantics.

Index

Constants

View Source
const Backend = "gin"

Backend names the server the binary was built with.

Variables

View Source
var TLSNextProtos = []string{"h2", "http/1.1"}

TLSNextProtos are the ALPN protocols the server speaks.

Functions

This section is empty.

Types

type Context

type Context interface {
	// Method is the request method.
	Method() string
	// Path is the decoded URL path.
	Path() string
	// RequestURI is the unmodified request target, path and query.
	RequestURI() string
	// FullPath is the matched route pattern, empty when no route matched.
	FullPath() string
	// Param returns a route parameter.
	Param(key string) string
	// SetParam overrides a route parameter for the rest of the chain.
	SetParam(key, value string)
	// Query returns a query string value.
	Query(key string) string
	// DefaultQuery returns a query string value or def when it is absent.
	DefaultQuery(key, def string) string
	// GetQuery returns a query string value and whether it is present.
	GetQuery(key string) (string, bool)
	// GetHeader returns a request header.
	GetHeader(key string) string
	// BasicAuth returns the credentials of the Authorization header.
	BasicAuth() (username, password string, ok bool)
	// Body returns the request body, never nil.
	Body() io.ReadCloser
	// SetBody replaces the request body for the rest of the chain.
	SetBody(body []byte)
	// Context returns the request context.
	Context() context.Context
	// SetContext replaces the request context for the rest of the chain.
	SetContext(ctx context.Context)
	// Set stores a value for the rest of the chain.
	Set(key string, value interface{})
	// Get returns a value stored with Set.
	Get(key string) (interface{}, bool)

	// Header sets a response header.
	Header(key, value string)
	// Status sets the response status code.
	Status(code int)
	// JSON writes obj as JSON.
	JSON(code int, obj interface{})
	// IndentedJSON writes obj as indented JSON.
	IndentedJSON(code int, obj interface{})
	// Data writes raw bytes.
	Data(code int, contentType string, data []byte)
	// Redirect sends the client to location.
	Redirect(code int, location string)
	// ResponseStatus is the status code written so far.
	ResponseStatus() int
	// ResponseSize is the number of body bytes written so far.
	ResponseSize() int
	// ServeHTTP lets a net/http handler answer the request.
	ServeHTTP(h http.Handler)

	// Next runs the remaining handlers of the chain.
	Next()
	// NextBuffered runs the remaining handlers holding their response back.
	// Calling flush sends it, otherwise it is dropped and the caller writes
	// its own response.
	NextBuffered() (flush func() error)
	// Abort stops the chain after the current handler.
	Abort()
	// IsAborted reports whether Abort was called.
	IsAborted() bool
	// AbortWithStatus aborts and writes the status code.
	AbortWithStatus(code int)
	// AbortWithStatusJSON aborts and writes obj as JSON.
	AbortWithStatusJSON(code int, obj interface{})
}

Context is one request travelling through a handler chain.

A handler that returns without calling Next or Abort lets the chain continue, Next runs the rest of the chain before returning, Abort stops it.

type Engine

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

Engine is the router. Like gin, a route captures the middlewares added with Use before it, while the NoRoute chain sees all of them.

func New

func New() *Engine

New returns an engine without routes.

func (*Engine) Close

func (e *Engine) Close() error

Close stops the server without waiting for active connections.

func (*Engine) DELETE

func (e *Engine) DELETE(path string, handlers ...HandlerFunc)

func (*Engine) GET

func (e *Engine) GET(path string, handlers ...HandlerFunc)

func (*Engine) HEAD

func (e *Engine) HEAD(path string, handlers ...HandlerFunc)

func (*Engine) Handle

func (e *Engine) Handle(method, path string, handlers ...HandlerFunc)

Handle registers a route.

func (*Engine) NoRoute

func (e *Engine) NoRoute(handlers ...HandlerFunc)

NoRoute sets the handlers for requests no route matched.

func (*Engine) POST

func (e *Engine) POST(path string, handlers ...HandlerFunc)

func (*Engine) PUT

func (e *Engine) PUT(path string, handlers ...HandlerFunc)

func (*Engine) Serve

func (e *Engine) Serve(ln net.Listener) error

Serve accepts connections on ln until Shutdown or Close, which make it return nil.

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes the engine a net/http handler, used by tests.

func (*Engine) Shutdown

func (e *Engine) Shutdown(ctx context.Context) error

Shutdown stops accepting connections and waits for the active ones.

func (*Engine) Use

func (e *Engine) Use(handlers ...HandlerFunc)

Use appends global middlewares.

type H

type H map[string]interface{}

H is a shortcut for a JSON object.

type HTTPContext

type HTTPContext struct {
	Request *http.Request
	// contains filtered or unexported fields
}

HTTPContext is a Context over net/http. The gin backend serves requests with it and handler tests build one with NewHTTPContext.

func NewHTTPContext

func NewHTTPContext(w http.ResponseWriter, r *http.Request) *HTTPContext

NewHTTPContext returns a context without handlers for w and r.

func (*HTTPContext) Abort

func (s *HTTPContext) Abort()

func (*HTTPContext) AbortWithStatus

func (c *HTTPContext) AbortWithStatus(code int)

func (*HTTPContext) AbortWithStatusJSON

func (c *HTTPContext) AbortWithStatusJSON(code int, obj interface{})

func (*HTTPContext) BasicAuth

func (c *HTTPContext) BasicAuth() (string, string, bool)

func (*HTTPContext) Body

func (c *HTTPContext) Body() io.ReadCloser

func (*HTTPContext) Context

func (c *HTTPContext) Context() context.Context

func (*HTTPContext) Data

func (c *HTTPContext) Data(code int, contentType string, data []byte)

func (*HTTPContext) DefaultQuery

func (c *HTTPContext) DefaultQuery(key, def string) string

func (*HTTPContext) FullPath

func (s *HTTPContext) FullPath() string

func (*HTTPContext) Get

func (s *HTTPContext) Get(key string) (interface{}, bool)

func (*HTTPContext) GetHeader

func (c *HTTPContext) GetHeader(key string) string

func (*HTTPContext) GetQuery

func (c *HTTPContext) GetQuery(key string) (string, bool)

func (*HTTPContext) Header

func (c *HTTPContext) Header(key, value string)

func (*HTTPContext) IndentedJSON

func (c *HTTPContext) IndentedJSON(code int, obj interface{})

func (*HTTPContext) IsAborted

func (s *HTTPContext) IsAborted() bool

func (*HTTPContext) JSON

func (c *HTTPContext) JSON(code int, obj interface{})

func (*HTTPContext) Method

func (c *HTTPContext) Method() string

func (*HTTPContext) Next

func (c *HTTPContext) Next()

func (*HTTPContext) NextBuffered

func (c *HTTPContext) NextBuffered() func() error

func (*HTTPContext) Param

func (s *HTTPContext) Param(key string) string

func (*HTTPContext) Path

func (c *HTTPContext) Path() string

func (*HTTPContext) Query

func (c *HTTPContext) Query(key string) string

func (*HTTPContext) Redirect

func (c *HTTPContext) Redirect(code int, location string)

func (*HTTPContext) RequestURI

func (c *HTTPContext) RequestURI() string

func (*HTTPContext) ResponseSize

func (c *HTTPContext) ResponseSize() int

func (*HTTPContext) ResponseStatus

func (c *HTTPContext) ResponseStatus() int

func (*HTTPContext) ServeHTTP

func (c *HTTPContext) ServeHTTP(h http.Handler)

func (*HTTPContext) Set

func (s *HTTPContext) Set(key string, value interface{})

func (*HTTPContext) SetBody

func (c *HTTPContext) SetBody(body []byte)

func (*HTTPContext) SetContext added in v1.3.0

func (c *HTTPContext) SetContext(ctx context.Context)

func (*HTTPContext) SetParam

func (s *HTTPContext) SetParam(key, value string)

func (*HTTPContext) Status

func (c *HTTPContext) Status(code int)

type HandlerFunc

type HandlerFunc func(Context)

HandlerFunc handles a request or acts as a middleware.

type Param

type Param struct {
	Key   string
	Value string
}

Param is a route parameter.

Jump to

Keyboard shortcuts

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