fhblade

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

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 28 Imported by: 3

README

golang简易web api框架

使用方法: go get github.com/zatxm/fhblade

Documentation

Index

Constants

View Source
const (
	BodyBytesKey = "hblade_bodybyteskey"
)
View Source
const LogKey = "hblade_log_key"

Variables

View Source
var Json = jsoniter.ConfigCompatibleWithStandardLibrary
View Source
var Log *zap.Logger = initLog()

Functions

func LogReleaseCtr

func LogReleaseCtr(c *Context) *zap.Logger

func LogWithCtr

func LogWithCtr(c *Context) *zap.Logger

Types

type Blade

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

func New

func New() *Blade

New creates a new blade.

func (*Blade) Any

func (b *Blade) Any(path string, handler Handler)

Any registers your function to be called with any http method.

func (*Blade) BindMiddleware

func (b *Blade) BindMiddleware()

Binding middleware

func (*Blade) Delete

func (b *Blade) Delete(path string, handler Handler)

Delete registers your function to be called when the given DELETE path has been requested.

func (*Blade) EnableLogRequest

func (b *Blade) EnableLogRequest()

Whether to record request logs

func (*Blade) Get

func (b *Blade) Get(path string, handler Handler)

Get registers your function to be called when the given GET path has been requested.

func (*Blade) Gzip

func (b *Blade) Gzip() bool

Get Gzip

func (*Blade) Head

func (b *Blade) Head(path string, handler Handler)

Head registers your function to be called when the given HEAD path has been requested.

func (*Blade) NoFunc

func (b *Blade) NoFunc(f func(*Context))

func (*Blade) OnEnd

func (b *Blade) OnEnd(callback func())

OnEnd registers a callback to be executed on server shutdown.

func (*Blade) OnError

func (b *Blade) OnError(callback func(*Context, error))

OnError registers a callback to be executed on server errors.

func (*Blade) OnStart

func (b *Blade) OnStart(callback func())

OnStart registers a callback to be executed on server start.

func (*Blade) Options

func (b *Blade) Options(path string, handler Handler)

Options registers your function to be called when the given OPTIONS path has been requested.

func (*Blade) Patch

func (b *Blade) Patch(path string, handler Handler)

Patch registers your function to be called when the given PATCH path has been requested.

func (*Blade) Post

func (b *Blade) Post(path string, handler Handler)

Post registers your function to be called when the given POST path has been requested.

func (*Blade) Put

func (b *Blade) Put(path string, handler Handler)

Put registers your function to be called when the given PUT path has been requested.

func (*Blade) Router

func (b *Blade) Router() *Router

Router returns the router used by the blade.

func (*Blade) Run

func (b *Blade) Run(addr ...string) error

Run starts your application with http.

func (*Blade) RunServer

func (b *Blade) RunServer(server *http.Server, l net.Listener) error

Run starts your application by given server and listener.

func (*Blade) RunTLS

func (b *Blade) RunTLS(addr, certFile, keyFile string) error

Run starts your application with https.

func (*Blade) RunUnix

func (b *Blade) RunUnix(file string) error

Run starts your application with Unix.

func (*Blade) ServeHTTP

func (b *Blade) ServeHTTP(response http.ResponseWriter, request *http.Request)

ServeHTTP responds to the given request.

func (*Blade) Server

func (b *Blade) Server() *http.Server

Server is used to load stored http server.

func (*Blade) SetGzip

func (b *Blade) SetGzip(gzip bool)

Set whether to enable Gzip

func (*Blade) SetSignAutoRun

func (b *Blade) SetSignAutoRun(auto bool)

func (*Blade) Shutdown

func (b *Blade) Shutdown() error

Shutdown will gracefully shut down the server.

func (*Blade) Static

func (b *Blade) Static(path, bind string)

Can bind static directory h.Static("/static", "static/")

func (*Blade) Use

func (b *Blade) Use(middlewares ...Middleware)

Use adds middleware to your middleware chain.

type Context

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

Context represents a request & response context.

func (*Context) B

func (c *Context) B() *Blade

返回blade

func (*Context) Bind

func (c *Context) Bind(obj interface{}) error

Bind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:

"application/json" --> JSON binding
"application/xml"  --> XML binding

otherwise --> returns an error. It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.

func (*Context) Bytes

func (c *Context) Bytes(body []byte) error

返回字节处理

func (*Context) CSS

func (c *Context) CSS(text string) error

CSS sends a style sheet.

func (*Context) ClientIP

func (c *Context) ClientIP() string

ClientIP tries to determine the real IP address of the client.

func (*Context) Close

func (c *Context) Close()

Close frees up resources and is automatically called in the ServeHTTP part of the web server.

func (*Context) Cookie

func (c *Context) Cookie(name string) (string, error)

Get name cookie value

func (*Context) Error

func (c *Context) Error(statusCode int, errorList ...interface{}) error

Error should be used for sending error messages to the client.

func (*Context) File

func (c *Context) File(file string) error

File sends the contents of a local file and determines its mime type by extension.

func (*Context) Get

func (c *Context) Get(param string) string

Get retrieves an URL parameter.

func (*Context) GetInt

func (c *Context) GetInt(param string) (int, error)

GetInt retrieves an URL parameter as an integer.

func (*Context) GetKey

func (c *Context) GetKey(key string) (value interface{}, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)

func (*Context) GetKeyBodyJsonAny

func (c *Context) GetKeyBodyJsonAny(key string) (sm interface{})

func (*Context) GetKeyBool

func (c *Context) GetKeyBool(key string) (b bool)

func (*Context) GetKeyByte

func (c *Context) GetKeyByte(key string) (s []byte)

func (*Context) GetKeyDuration

func (c *Context) GetKeyDuration(key string) (d time.Duration)

func (*Context) GetKeyFloat64

func (c *Context) GetKeyFloat64(key string) (f64 float64)

func (*Context) GetKeyInt

func (c *Context) GetKeyInt(key string) (i int)

func (*Context) GetKeyInt64

func (c *Context) GetKeyInt64(key string) (i64 int64)

func (*Context) GetKeyString

func (c *Context) GetKeyString(key string) (s string)

func (*Context) GetKeyStringMap

func (c *Context) GetKeyStringMap(key string) (sm map[string]interface{})

func (*Context) GetKeyStringMapString

func (c *Context) GetKeyStringMapString(key string) (sms map[string]string)

func (*Context) GetKeyStringSlice

func (c *Context) GetKeyStringSlice(key string) (ss []string)

func (*Context) GetKeyTime

func (c *Context) GetKeyTime(key string) (t time.Time)

func (*Context) HTML

func (c *Context) HTML(html string) error

HTML sends a HTML string.

func (*Context) IP

func (c *Context) IP() string

Get IP by RemoteAddr

func (*Context) JSON

func (c *Context) JSON(value interface{}) error

JSON encodes the object to a JSON string and responds.

func (*Context) JSONAndStatus

func (c *Context) JSONAndStatus(status int, value interface{}) error

func (*Context) JavaScript

func (c *Context) JavaScript(code string) error

JavaScript sends a script.

func (*Context) MustBindWith

func (c *Context) MustBindWith(obj interface{}, b binding.Binding) error

MustBindWith binds the passed struct pointer using the specified binding engine. It will abort the request with HTTP 400 if any error occurs. See the binding package.

func (*Context) Path

func (c *Context) Path() string

获取相对请求路径,如/ws/gutu

func (*Context) Query

func (c *Context) Query(param string) string

从URL获取参数值

func (*Context) ReadAll

func (c *Context) ReadAll(reader io.Reader) error

ReadAll returns the contents of the reader. This will create an in-memory copy and calculate the E-Tag before sending the data. Compression will be applied if necessary.

func (*Context) ReadSeeker

func (c *Context) ReadSeeker(reader io.ReadSeeker) error

发送io.ReadSeeker内容,不会压缩 如阅读器包含大量数据时用此功能

func (*Context) Reader

func (c *Context) Reader(reader io.Reader) error

发送io.Reader内容,不会压缩 如阅读器包含大量数据时用此功能

func (*Context) Redirect

func (c *Context) Redirect(status int, u string) error

Redirect redirects to the given URL.

func (*Context) Request

func (c *Context) Request() Request

Request returns the HTTP request.

func (*Context) Response

func (c *Context) Response() Response

Response returns the HTTP response.

func (*Context) SetCookie

func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)

SetCookie adds a Set-Cookie header to the ResponseWriter's headers.

func (*Context) SetKey

func (c *Context) SetKey(key string, value interface{})

Set is used to store a new key/value pair exclusively for this context. It also lazy initializes c.Keys if it was not used previously.

func (*Context) SetPath

func (c *Context) SetPath(path string)

设置相对请求路径,如/ws/gutu

func (*Context) SetSameSite

func (c *Context) SetSameSite(samesite http.SameSite)

SetSameSite with cookie

func (*Context) SetStatus

func (c *Context) SetStatus(status int)

SetStatus sets the HTTP status.

func (*Context) ShouldBind

func (c *Context) ShouldBind(obj interface{}) error

func (*Context) ShouldBindJSON

func (c *Context) ShouldBindJSON(obj interface{}) error

ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, binding.JSON).

func (*Context) ShouldBindQuery

func (c *Context) ShouldBindQuery(obj interface{}) error

ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, binding.Query).

func (*Context) ShouldBindWith

func (c *Context) ShouldBindWith(obj interface{}, b binding.Binding) error

ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.

func (*Context) Status

func (c *Context) Status() int

Status returns the HTTP status.

func (*Context) String

func (c *Context) String(body string) error

String responds either with raw text or gzipped if the text length is greater than the gzip threshold.

func (*Context) Text

func (c *Context) Text(text string) error

Text sends a plain text string.

type H

type H map[string]interface{}

type Handler

type Handler func(*Context) error

func (Handler) Bind

func (handler Handler) Bind(middleware ...Middleware) Handler

type Middleware

type Middleware func(Handler) Handler

Middleware is a function that accepts a handler and transforms it into a different handler.

func Recovery

func Recovery() Middleware

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.

type Request

type Request interface {
	RawData() ([]byte, error)
	RawDataSetBody() ([]byte, error)
	Context() stdContext.Context
	Header(string) string
	Host() string
	Method() string
	Path() string
	Protocol() string
	Scheme() string
	RawQuery() string
	ContentType() string
	Req() *http.Request
}

type Response

type Response interface {
	Header(string) string
	Rw() http.ResponseWriter
	SetHeader(string, string)
	SetRw(http.ResponseWriter)
	Pusher() http.Pusher
}

type Router

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

Router is a high-performance router.

func (*Router) Add

func (router *Router) Add(method string, path string, handler Handler)

Add registers a new handler for the given method and path.

func (*Router) Find

func (router *Router) Find(method string, path string) Handler

Find returns the handler for the given route. This is only useful for testing purposes. Use Lookup instead.

func (*Router) Lookup

func (router *Router) Lookup(method string, path string, c *Context)

Lookup finds the handler and parameters for the given route and assigns them to the given context.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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