core

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: Apache-2.0 Imports: 16 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	RouterGroup

	// hook
	OnRun      []HookFuncWithErr
	OnShutdown []HookFunc
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(opts *Options) *Engine

NewEngine for PIANO

func (*Engine) Init added in v0.1.3

func (e *Engine) Init() error

Init PIANO engine

func (*Engine) IsRunning added in v0.1.5

func (e *Engine) IsRunning() bool

func (*Engine) Options added in v0.1.3

func (e *Engine) Options() *Options

Options return options field of current engine

func (*Engine) Run added in v0.1.3

func (e *Engine) Run() error

Run Start the PIANO Engine

func (*Engine) ServeHTTP

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

ServeHTTP core function, replace DefaultServeMux

func (*Engine) Shutdown added in v0.1.3

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

type HandlerFunc

type HandlerFunc func(ctx context.Context, pk *PianoKey)

HandlerFunc is the core type of PIANO

type HandlersChain

type HandlersChain []HandlerFunc

HandlersChain is the slice of HandlerFunc

type HookFunc added in v0.1.3

type HookFunc func(ctx context.Context)

type HookFuncWithErr added in v0.1.3

type HookFuncWithErr func(ctx context.Context) error

type IRoute

type IRoute interface {
	Handle(string, string, ...HandlerFunc)

	GET(string, ...HandlerFunc)
	POST(string, ...HandlerFunc)
	PUT(string, ...HandlerFunc)
	DELETE(string, ...HandlerFunc)
	PATCH(string, ...HandlerFunc)
	OPTIONS(string, ...HandlerFunc)
	HEAD(string, ...HandlerFunc)
}

type IRouter

type IRouter interface {
	IRoute
	Group(string, ...HandlerFunc) *RouterGroup
	Use(...HandlerFunc)
}

type M

type M map[string]any

M music is used to simplified code

type MethodForest

type MethodForest []*tree

MethodForest is alias for MethodTrees

type Option

type Option func(o *Options)

func WithHideBanner added in v0.1.5

func WithHideBanner() Option

func WithHostAddr

func WithHostAddr(addr string) Option

WithHostAddr used to define addr you prefer

func WithShutdownTimeout added in v0.1.3

func WithShutdownTimeout(timeout time.Duration) Option

type Options

type Options struct {
	Addr            string
	ShutdownTimeout time.Duration
	HideBanner      bool
}

func NewOptions

func NewOptions(opts ...Option) *Options

NewOptions for PIANO engine

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter

type Params

type Params []Param

func (*Params) Get

func (ps *Params) Get(key string) string

Get returns the value of the first Param which key matches the given key. If no matching Param is found, an empty string is returned.

func (*Params) Set

func (ps *Params) Set(key, value string)

Set will set key-value pair into Params

type PianoKey

type PianoKey struct {
	Request *http.Request
	Writer  http.ResponseWriter

	Params Params

	KVs M
	// contains filtered or unexported fields
}

PianoKey play the piano with PianoKeys

func NewContext

func NewContext(maxParams uint16) *PianoKey

NewContext will return a new context object which is piano key

func (*PianoKey) Break

func (pk *PianoKey) Break()

Break current handler

func (*PianoKey) BreakWithMessage

func (pk *PianoKey) BreakWithMessage(code int, msg string)

func (*PianoKey) BreakWithStatus

func (pk *PianoKey) BreakWithStatus(code int)

func (*PianoKey) DefaultFormValue

func (pk *PianoKey) DefaultFormValue(key, defaultValue string) string

func (*PianoKey) DefaultPostForm

func (pk *PianoKey) DefaultPostForm(key, defaultValue string) string

DefaultPostForm is PostForm with default value when no match

func (*PianoKey) DefaultQuery

func (pk *PianoKey) DefaultQuery(key, defaultValue string) string

DefaultQuery is Query with default value when no match

func (*PianoKey) FormValue

func (pk *PianoKey) FormValue(key string) string

func (*PianoKey) Get

func (pk *PianoKey) Get(key string) (value any, ok bool)

Get will return the value corresponding to the given key, it will return (nil, false) if key does not exist

func (*PianoKey) JSON

func (pk *PianoKey) JSON(code int, data any)

JSON is used to response data in JSON form

func (*PianoKey) MustGet

func (pk *PianoKey) MustGet(key string) any

MustGet will return the value corresponding to the given key, it will panic if key does not exist

func (*PianoKey) Next

func (pk *PianoKey) Next(ctx context.Context)

Next executes the handlers on the chain

func (*PianoKey) Param

func (pk *PianoKey) Param(key string) string

Param return the corresponding param in request URL

func (*PianoKey) PostForm

func (pk *PianoKey) PostForm(key string) string

PostForm is used to get HTTP POST form data

func (*PianoKey) Query

func (pk *PianoKey) Query(key string) string

Query is used to match HTTP GET query params

func (*PianoKey) Set

func (pk *PianoKey) Set(key string, value any)

Set will store the key and value into this PianoKey

func (*PianoKey) SetHandlers

func (pk *PianoKey) SetHandlers(handlers HandlersChain)

SetHandlers will set handlers field for PianoKey context

func (*PianoKey) SetHeader

func (pk *PianoKey) SetHeader(key, value string)

SetHeader is used to set HTTP response header

func (*PianoKey) SetStatusCode

func (pk *PianoKey) SetStatusCode(code int)

SetStatusCode is used to set HTTP response code

func (*PianoKey) String

func (pk *PianoKey) String(code int, format string, data ...any)

String is used to response data in string form

type RouterGroup

type RouterGroup struct {
	Handlers HandlersChain
	// contains filtered or unexported fields
}

func (*RouterGroup) DELETE

func (rg *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc)

DELETE will handler HTTP DELETE request

func (*RouterGroup) GET

func (rg *RouterGroup) GET(relativePath string, handlers ...HandlerFunc)

GET will handler HTTP GET request

func (*RouterGroup) Group added in v0.1.3

func (rg *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup

Group will new a route group

func (*RouterGroup) HEAD added in v0.1.3

func (rg *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc)

HEAD will handler HTTP HEAD request

func (*RouterGroup) Handle added in v0.1.3

func (rg *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc)

Handle is suggested to use for custom methods

func (*RouterGroup) OPTIONS added in v0.1.3

func (rg *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc)

OPTIONS will handler HTTP OPTIONS request

func (*RouterGroup) PATCH added in v0.1.3

func (rg *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc)

PATCH will handler HTTP PATCH request

func (*RouterGroup) POST

func (rg *RouterGroup) POST(relativePath string, handlers ...HandlerFunc)

POST will handler HTTP POST request

func (*RouterGroup) PUT

func (rg *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc)

PUT will handler HTTP PUT request

func (*RouterGroup) Use added in v0.1.3

func (rg *RouterGroup) Use(middleware ...HandlerFunc)

Use middlewares or other custom handlers

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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