Help us shape the future of Go! Take the 2026 Go Developer Survey

httpserver

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2021 License: GPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const HealthUrl = "/health"

Variables

This section is empty.

Functions

This section is empty.

Types

type Ctx

type Ctx struct {
	IHttpResponse
	// contains filtered or unexported fields
}

func NewCtx

func NewCtx(writer http.ResponseWriter, request *http.Request) *Ctx

func (*Ctx) Header

func (ctx *Ctx) Header() http.Header

func (*Ctx) Param

func (ctx *Ctx) Param(name string) string

func (*Ctx) Request

func (ctx *Ctx) Request() *http.Request

func (*Ctx) SetResponseHandler

func (ctx *Ctx) SetResponseHandler(response IHttpResponse)

func (*Ctx) Write

func (ctx *Ctx) Write(resp []byte) (int, error)

func (*Ctx) WriteHeader

func (ctx *Ctx) WriteHeader(code int)

func (*Ctx) WriteStr

func (ctx *Ctx) WriteStr(resp string) (int, error)

func (*Ctx) Writer

func (ctx *Ctx) Writer() http.ResponseWriter

type HttpForwardHandler

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

转发http的handler

func NewHttpForwardHandler

func NewHttpForwardHandler(forward string, timeout time.Duration) *HttpForwardHandler

func (*HttpForwardHandler) AddForwardRule

func (h *HttpForwardHandler) AddForwardRule(rule map[string]string)

func (*HttpForwardHandler) ServeHTTP

func (h *HttpForwardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type HttpResponse

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

func (*HttpResponse) Code

func (resp *HttpResponse) Code(code int64)

func (*HttpResponse) Data

func (resp *HttpResponse) Data(data interface{})

func (*HttpResponse) Msg

func (resp *HttpResponse) Msg(msg string)

func (*HttpResponse) Response

func (resp *HttpResponse) Response()

type HttpResponseData

type HttpResponseData struct {
	Code      int64       `json:"code"`
	Data      interface{} `json:"data"`
	Msg       string      `json:"msg"`
	RequestId string      `json:"requestId"`
}

func (*HttpResponseData) Encode

func (d *HttpResponseData) Encode() []byte

type HttpRouteFunc

type HttpRouteFunc = func(ctx *Ctx)

type HttpServer

type HttpServer struct {
	log.InvokeLog
	// contains filtered or unexported fields
}

func NewHttpServer

func NewHttpServer(name string, addr string) (*HttpServer, error)

func (*HttpServer) DefaultRouteHealth

func (s *HttpServer) DefaultRouteHealth() *HttpServer

func (*HttpServer) Name

func (s *HttpServer) Name() string

func (*HttpServer) RouteDelete

func (s *HttpServer) RouteDelete(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer

func (*HttpServer) RouteFiles

func (s *HttpServer) RouteFiles(path string, f http.FileSystem) *HttpServer

func (*HttpServer) RouteGet

func (s *HttpServer) RouteGet(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer

func (*HttpServer) RouteHandler

func (s *HttpServer) RouteHandler(method, path string, f http.Handler, middleware []Middleware) *HttpServer

func (*HttpServer) RouteOptions

func (s *HttpServer) RouteOptions(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer

func (*HttpServer) RoutePost

func (s *HttpServer) RoutePost(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer

func (*HttpServer) RoutePut

func (s *HttpServer) RoutePut(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer

func (*HttpServer) Router

func (s *HttpServer) Router() *httprouter.Router

func (*HttpServer) Run

func (s *HttpServer) Run() error

func (*HttpServer) Stop

func (s *HttpServer) Stop() error

type IHttpResponse

type IHttpResponse interface {
	Code(code int64)
	Msg(msg string)
	Data(data interface{})
	Response()
}

func NewHttpResponse

func NewHttpResponse(writer http.ResponseWriter) IHttpResponse

type Middleware

type Middleware func(ctx *Ctx, next func(ctx *Ctx))
var DefaultPanicMiddleware Middleware = func(ctx *Ctx, next func(*Ctx)) {
	defer func() {
		if err := recover(); err != nil {
			debug.PrintStack()

			ctx.WriteHeader(500)
			ctx.WriteStr("system busy")
			return
		}
	}()
	next(ctx)
}

panic处理

var DefaultResponseMiddleware Middleware = func(ctx *Ctx, next func(*Ctx)) {
	ctx.SetResponseHandler(NewHttpResponse(ctx.Writer()))
	next(ctx)
}

response

type Router

type Router struct {
	Path         string
	Method       string
	RouteHandler HttpRouteFunc
	Middleware   []Middleware

	HttpHandler http.Handler
}

Jump to

Keyboard shortcuts

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