rest

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: 0BSD Imports: 24 Imported by: 0

Documentation

Overview

router.go Copyright (C) 2024 veypi <i@veypi.com> 2024-08-07 13:45 Distributed under terms of the MIT license.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCrash      = errors.New("crash")
	ErrNotFound   = errors.New("not found")
	ErrParse      = errors.New("parse request data failed")
	ErrArgMissing = fmt.Errorf("%w: missing arg", ErrParse)
	ErrArgInvalid = fmt.Errorf("%w: invalid argument", ErrParse)
)

Functions

This section is empty.

Types

type ApiHandler added in v0.6.0

type ApiHandler interface {
	fc0 | fc1 | fc2 | fc3 | fc4 | fc5 | fc6
}

type Application

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

func New

func New(c *RestConf) (*Application, error)

func (*Application) Domain added in v0.6.0

func (app *Application) Domain(d string) Router

func (*Application) Router

func (app *Application) Router() Router

func (*Application) Run

func (app *Application) Run() error

func (*Application) ServeHTTP added in v0.6.0

func (app *Application) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Application) SetMux added in v0.6.0

func (app *Application) SetMux(m func(w http.ResponseWriter, r *http.Request) func(http.ResponseWriter, *http.Request))

type ErrHandle added in v0.6.0

type ErrHandle = func(x *X, err error)

type Params added in v0.6.0

type Params [][2]string

func (*Params) Get added in v0.6.0

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

func (*Params) GetInt added in v0.6.0

func (ps *Params) GetInt(k string) int

func (*Params) GetStr added in v0.6.0

func (ps *Params) GetStr(k string) string

type RestConf

type RestConf struct {
	Host string `json:"host"`
	Port int    `json:"port"`
	// log file path
	LoggerPath     string `json:"logger_path,omitempty"`
	LoggerLevel    string `json:"logger_level,omitempty"`
	PrettyLog      bool   `json:"pretty_log,omitempty"`
	TimeFormat     string `json:"time_format,omitempty"`
	PostMaxMemory  uint
	TlsCfg         *tls.Config
	MaxConnections int
}

func (*RestConf) IsValid

func (c *RestConf) IsValid() error

func (*RestConf) Url

func (c *RestConf) Url() string

type Router added in v0.6.0

type Router interface {
	String() string
	Print()
	ServeHTTP(http.ResponseWriter, *http.Request)
	SubRouter(prefix string) Router

	Set(url string, method string, handlers ...any) Router
	Get(url string, handlers ...any) Router
	Any(url string, handlers ...any) Router
	Post(url string, handlers ...any) Router
	Head(url string, handlers ...any) Router
	Put(url string, handlers ...any) Router
	Patch(url string, handlers ...any) Router
	Delete(url string, handlers ...any) Router

	Use(middleware ...any)
	SetErrFunc(fc ErrHandle)
	Static(prefix string, directory string)
	EmbedFile(prefix string, f []byte)
	EmbedDir(prefix string, fs embed.FS, fsPrefix string, file404 string)
}

func NewRouter added in v0.6.0

func NewRouter() Router

type X

type X struct {
	Request *http.Request

	Params Params
	// contains filtered or unexported fields
}

func (*X) GetRemoteIp added in v0.6.0

func (x *X) GetRemoteIp() string

func (*X) Header added in v0.6.0

func (x *X) Header() http.Header

func (*X) JSON added in v0.6.0

func (x *X) JSON(data any) error

func (*X) Next added in v0.6.0

func (x *X) Next(args ...any) (err error)

func (*X) Parse added in v0.6.0

func (x *X) Parse(obj any) error

从不同来源解析目标结构体一级字段 tag标签 parse:"path/header/query/form/json" 可以追加为 path@alias_name tag标签 default:"" 字段为指针类型时为可选参数,defalt标签不生效 字段为非指针类型时是必选参数,default标签生效,未设置该值且未发现参数时报参数缺失错误 json 字段由json解码控制,没有default机制

func (*X) SetParam added in v0.6.0

func (x *X) SetParam(k string, v string)

func (*X) Stop added in v0.6.0

func (x *X) Stop()

func (*X) Write added in v0.6.0

func (x *X) Write(p []byte) (n int, err error)

func (*X) WriteHeader added in v0.6.0

func (x *X) WriteHeader(statusCode int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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