httpx

package module
v0.0.0-...-abfcc2c Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2017 License: MIT Imports: 19 Imported by: 0

README

a lightweight web framework

support http2 protocol

Web Configuable

type ServerConfig struct {
	Addr           string
	Port           int
	ReadTimeout    time.Duration // 读的最大Timeout时间
	WriteTimeout   time.Duration // 写的最大Timeout时间
	MaxHeaderBytes int           // 请求头的最大长度
	TLSConfig      *tls.Config   // 配置TLS
}

support Method

	const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	DELETE  = "DELETE"
	HEAD    = "HEAD"
	TRACE   = "TRACE"
	CONNECT = "CONNECT"
	)
Handler interface definition
	func(request *http.Request, pathFragments map[string]string, reply *Reply)

Example

package web

import (
	"errors"
	"net/http"
	"testing"
	"time"
)

func TestServer(t *testing.T) {
	server := NewServer(nil)
	server.Regedit("/a/{name}/123", GET, Service)
	server.Regedit("/a/123/{name}", GET, testService)
	server.AddFilter("/*", AccessLogFilter)
	server.Start()
	time.Sleep(time.Second * 20)
	server.Stop()
}

func Service(request *http.Request, param map[string]string, reply *Reply) {
	reply.With("123" + param["name"])
	panic(errors.New("test error"))
}

func testService(request *http.Request, param map[string]string, reply *Reply) {
	reply.With(param["name"])
}

Documentation

Index

Constants

View Source
const (
	//GET GET Method
	GET = RequestMethod(http.MethodGet)
	//HEAD HEAD Method
	HEAD = RequestMethod(http.MethodHead)
	//POST POST Method
	POST = RequestMethod(http.MethodPost)
	//PUT PUT Method
	PUT = RequestMethod(http.MethodPut)
	//PATCH PATCH Method
	PATCH = RequestMethod(http.MethodPatch)
	//DELETE DELETE Method
	DELETE = RequestMethod(http.MethodDelete)
	//CONNECT CONNECT Method
	CONNECT = RequestMethod(http.MethodConnect)
	//OPTIONS OPTIONS Method
	OPTIONS = RequestMethod(http.MethodOptions)
	//TRACE TRACE Method
	TRACE = RequestMethod(http.MethodTrace)
)

Variables

View Source
var (
	//DefaultCharset 默认的字符集
	DefaultCharset = "utf-8"

	//DefaultRenderJSON 默认的 Json 渲染器
	DefaultRenderJSON = RenderJSON{Charset: DefaultCharset}
	//DefaultRenderXML 默认的 Xml 渲染器
	DefaultRenderXML = RenderXML{Charset: DefaultCharset}
	//DefaultRenderText 默认的 Text 渲染器
	DefaultRenderText = RenderText{Charset: DefaultCharset}
)
View Source
var EmptyParam = RequestParam("")

EmptyParam 空参数

Functions

func AccessLogFilter

func AccessLogFilter(reply Reply, chain FilterChain)

AccessLogFilter 访问日志的 Filter, 在 consol 上输出每个被访问的 url 及响应时间

Types

type Config

type Config struct {
	ServerAddr        string        `json:"server_addr" yaml:"server_addr"`           //ServerAddr server地址
	ReadTimeout       time.Duration `json:"read_timeout" yaml:"read_timeout"`         // 读的最大Timeout时间
	WriteTimeout      time.Duration `json:"write_timeout" yaml:"write_timeout"`       // 写的最大Timeout时间
	MaxHeaderBytes    int           `json:"max_header_bytes" yaml:"max_header_bytes"` // 请求头的最大长度
	TLSConfig         *tls.Config   // 配置TLS
	TLSNextProto      map[string]func(*http.Server, *tls.Conn, http.Handler)
	ConnState         func(net.Conn, http.ConnState)
	HTTPErrorLogout   io.Writer
	DefaultRender     Render
	KeepAliveDuration time.Duration `json:"keep_alive_duration" yaml:"keep_alive_duration"`
	IdleTimeout       time.Duration `json:"idle_timeout" yaml:"idle_timeout"`
	RootContext       context.Context
}

Config http server Config

func (*Config) GetRootContext

func (config *Config) GetRootContext() context.Context

type ConvertError

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

ConvertError 请求参数转换错误

func (ConvertError) Error

func (ce ConvertError) Error() string

type Filter

type Filter func(reply Reply, chain FilterChain)

Filter 定义实现 Filter 需要实现的方法类型

type FilterChain

type FilterChain func(reply Reply)

FilterChain 定义的需要执行的方法子方法

type HTTPError

type HTTPError struct {
	Code    int         `json:"httpcode"`
	Message interface{} `json:"message"`
}

HTTPError 对 http 处理的过程中异常的封装

func NewHTTPErr

func NewHTTPErr(code int, message string) *HTTPError

NewHTTPErr 创建一个新的 Http 错误描述

func (*HTTPError) Error

func (httpError *HTTPError) Error() string

Error 实现的 error接口

type PathFragment

type PathFragment map[string]RequestParam

PathFragment 动态 Path 里面的变量片段

func (PathFragment) Get

func (pf PathFragment) Get(key string) (RequestParam, error)

Get get RequestParam form key

func (PathFragment) GetAsFloat32

func (pf PathFragment) GetAsFloat32(key string) (float32, error)

GetAsFloat32 get a float32 from key

func (PathFragment) GetAsFloat64

func (pf PathFragment) GetAsFloat64(key string) (float64, error)

GetAsFloat64 get a float64 from key

func (PathFragment) GetAsInt

func (pf PathFragment) GetAsInt(key string) (int, error)

GetAsInt get a int from key

func (PathFragment) GetAsInt32

func (pf PathFragment) GetAsInt32(key string) (int32, error)

GetAsInt32 get a int32 form key

func (PathFragment) GetAsInt64

func (pf PathFragment) GetAsInt64(key string) (int64, error)

GetAsInt64 get a int64 from from key

func (PathFragment) GetAsString

func (pf PathFragment) GetAsString(key string) (string, error)

GetAsString get a string form key

func (PathFragment) GetAsTime

func (pf PathFragment) GetAsTime(key string, layout string) (time.Time, error)

GetAsTime get a Time from key

type Render

type Render interface {
	ContentType() string
	Render(data interface{}) (io.ReadCloser, error)
}

Render 渲染器接口

type RenderJSON

type RenderJSON struct {
	Charset string
}

RenderJSON json格式的渲染器

func (RenderJSON) ContentType

func (render RenderJSON) ContentType() string

ContentType implement Render func

func (RenderJSON) Render

func (render RenderJSON) Render(data interface{}) (io.ReadCloser, error)

Write implement Render func

type RenderText

type RenderText struct {
	Charset string
}

RenderText text格式的渲染器

func (RenderText) ContentType

func (render RenderText) ContentType() string

ContentType implement Render func

func (RenderText) Render

func (render RenderText) Render(data interface{}) (io.ReadCloser, error)

Write implement Render func

type RenderXML

type RenderXML struct {
	Charset string
}

RenderXML xml格式的渲染器

func (RenderXML) ContentType

func (render RenderXML) ContentType() string

ContentType implement Render func

func (RenderXML) Render

func (render RenderXML) Render(data interface{}) (io.ReadCloser, error)

Write implement Render func

type Reply

type Reply interface {
	GetStatusCode() int
	SetStatusCode(statusCode int) Reply
	SetCookie(cookie http.Cookie) Reply
	SetHeader(key, value string) Reply
	AddHeader(key, value string) Reply
	DelHeader(key string) Reply
	GetHeader(key string) string
	Header() http.Header
	Redirect(code int, url string) Reply
	AddPathFragment(k, v string)

	With(data interface{}) Reply
	As(render Render) Reply

	GetRequest() *http.Request
	GetResponseWriter() http.ResponseWriter
	GetPathFragment() PathFragment
	AdapterHTTPHandler(adapter bool)
	//包装一层ResponseWriter,如 Gzip
	WarpResponseWriter(http.ResponseWriter)
	GetContext() context.Context
	SetContext(key string, value interface{})
}

Reply a wapper for http requese and response

type RequestErrorHandler

type RequestErrorHandler func(err *HTTPError, reply Reply)

RequestErrorHandler 请求异常的处理方法定义

type RequestHandler

type RequestHandler func(reply Reply)

RequestHandler 处理 Request的方法定义

type RequestMethod

type RequestMethod string

RequestMethod 请求的 Http 方法

type RequestParam

type RequestParam string

RequestParam 统一Request 请求参数类型

func (RequestParam) AsFloat32

func (rp RequestParam) AsFloat32() (float32, error)

AsFloat32 to float32

func (RequestParam) AsFloat64

func (rp RequestParam) AsFloat64() (float64, error)

AsFloat64 to float64

func (RequestParam) AsInt

func (rp RequestParam) AsInt() (int, error)

AsInt 转换为 Int 类型

func (RequestParam) AsInt32

func (rp RequestParam) AsInt32() (int32, error)

AsInt32 to int32

func (RequestParam) AsInt64

func (rp RequestParam) AsInt64() (int64, error)

AsInt64 to int64

func (RequestParam) AsString

func (rp RequestParam) AsString() string

AsString to string

func (RequestParam) AsTime

func (rp RequestParam) AsTime(layout string) (time.Time, error)

AsTime to time type

type Server

type Server interface {
	Start() <-chan error
	Stop()
	GetServerAddress() string
	RegisterHandlerFunc(path string, method RequestMethod, handlerFunc http.HandlerFunc) error
	RegisterHandler(path string, method RequestMethod, handler http.Handler) error
	Register(path string, method RequestMethod, requestHandler RequestHandler) error

	AddFirstFilter(uriPattern string, actionFilter Filter)
	AddLastFilter(uriPattern string, actionFilter Filter)
	AddFilterWithRegex(uriPattern string, actionFilter Filter)

	AddRequestErrorHandler(code int, handler RequestErrorHandler) error
}

Server the http server interface

func NewServer

func NewServer(serverConfig *Config) Server

NewServer 创建一个Http Server,参数可以为空,默认使用0.0.0.0:8888

type URIPatternMatcher

type URIPatternMatcher interface {
	// contains filtered or unexported methods
}

URIPatternMatcher a URI matcher interface

Directories

Path Synopsis
main
main
filters

Jump to

Keyboard shortcuts

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