web

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderXForwardedFor = "X-Forwarded-For"
	HeaderXRealIP       = "X-Real-Ip"

	HeaderXForwardedProto    = "X-Forwarded-Proto"
	HeaderXForwardedProtocol = "X-Forwarded-Protocol"
	HeaderXForwardedSsl      = "X-Forwarded-Ssl"
	HeaderXUrlScheme         = "X-Url-Scheme"

	HeaderContentType   = "Content-Type"
	HeaderAuthorization = "Authorization"

	ContentTypeJSON  = "application/json; charset=utf-8"
	ContentTypeHTML  = "text/html; charset=utf-8"
	ContentTypePlain = "text/plain; charset=utf-8"
)
View Source
const RouteTag = `group:"routes"`

Variables

View Source
var (
	Recoverer = middleware.Recoverer
	Logger    = middleware.Logger
	NewMux    = chi.NewMux
)
View Source
var (
	StatusCtxKey = &ctxKey{"Status"}
	DBCtxKey     = &ctxKey{"DB"}
	JwtCtxKey    = &ctxKey{"Jwt"}
	CtxKey       = &ctxKey{"Ctx"}
	WriterKey    = &ctxKey{"Writer"}
	UIDKey       = &ctxKey{"UID"}
	TokenKey     = &ctxKey{"Token"}
)
View Source
var ErrTokenMalformed = jwt.ErrTokenMalformed

Functions

func Authenticator

func Authenticator(next http.Handler) http.Handler

func PrintRoute

func PrintRoute(r Routes)

func Provide

func Provide(ctors ...any) fx.Option

func ResolveHandler

func ResolveHandler(in any) (handler http.Handler)

func ResolvePattern

func ResolvePattern(in string) (method, pattern string)

func Run

func Run(params Params)

func Serve

func Serve(handler http.Handler)

func ServeHTTP

func ServeHTTP(handler http.Handler)

func ServeHTTPS

func ServeHTTPS(mux http.Handler)

func SetTemplateFs

func SetTemplateFs(fsys fs.FS)

func Struct

func Struct(r Router, s any)

func Supply

func Supply(values ...Route) fx.Option

func Validator

func Validator(next http.Handler) http.Handler

Types

type Context

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

func GetContext

func GetContext(r *http.Request) Context

func (Context) Blob

func (c Context) Blob(v []byte, contentType string)

func (Context) Claims

func (c Context) Claims() M

func (Context) ClaimsValue

func (c Context) ClaimsValue(key string) any

func (Context) Cookie

func (c Context) Cookie(name string) string

Cookie gets the value of a cookie with name name.

func (Context) Error

func (c Context) Error(err error)

func (Context) File

func (c Context) File(filename string, attachName string)

func (Context) FileBlob

func (c Context) FileBlob(data []byte, contentType string, attachName string)

func (Context) FlatMap

func (c Context) FlatMap(keyValue ...any)

func (Context) FormFile

func (c Context) FormFile(fields ...string) (h *multipart.FileHeader)

获取第一个文件

func (Context) FormFiles

func (c Context) FormFiles(fields ...string) (hs []*multipart.FileHeader)

获取所有文件

func (Context) GetDB

func (c Context) GetDB() *gorm.DB

func (Context) GetJSON

func (c Context) GetJSON(url string) (any, error)

获取远程链接的JSON数据

func (Context) GetStatus

func (c Context) GetStatus() int

func (Context) GetUID

func (c Context) GetUID() (id int)

func (Context) HTML

func (c Context) HTML(data string)

func (Context) Header

func (c Context) Header() http.Header

func (Context) HeaderGet

func (c Context) HeaderGet(name string) string

func (Context) HeaderSet

func (c Context) HeaderSet(name, value string)

func (Context) Host

func (c Context) Host() (host string)

Host 返回请求的不带端口号的主机名

从请求返回过来的,所以忽略格式错误的状态

func (Context) IsTLS

func (c Context) IsTLS() bool

func (Context) JSON

func (c Context) JSON(v any, pretty ...bool)

func (Context) JSONBlob

func (c Context) JSONBlob(jsonBytes []byte)

func (Context) Jwt

func (c Context) Jwt() *Jwt

func (Context) Msg

func (c Context) Msg(errmsg string, errno ...int)

func (Context) NoContent

func (c Context) NoContent()

输出方法

func (Context) NotFound

func (c Context) NotFound()

func (Context) Origin

func (c Context) Origin() string

func (Context) Path

func (c Context) Path() string

func (Context) PathDir

func (c Context) PathDir() string

func (Context) PlainText

func (c Context) PlainText(data string)

func (Context) Port

func (c Context) Port() (port int)

Port 返回请求的端口号

func (Context) Query

func (c Context) Query(name string) string

func (Context) Querys

func (c Context) Querys(name string) []string

func (Context) ReadBody

func (c Context) ReadBody() (body []byte, err error)

func (Context) ReadJSON

func (c Context) ReadJSON(v any) error

func (Context) RealIp

func (c Context) RealIp() string

RealIp returns the client's network address based on `X-Forwarded-For` or `X-Real-IP` request header.

func (Context) Redirect

func (c Context) Redirect(url string)

func (Context) Request

func (c Context) Request() *http.Request

func (Context) RequestHeader

func (c Context) RequestHeader() http.Header

func (Context) RequestWrite

func (c Context) RequestWrite(w io.Writer) error

func (Context) Response

func (c Context) Response() w

func (Context) SaveUpload

func (c Context) SaveUpload(dstPath string, fields ...string) (h *multipart.FileHeader, hash string, err error)

保存上传文件,可以指定上传文件的字段名,如果没有指定,则保存第一个文件

func (Context) Scheme

func (c Context) Scheme() string

Scheme returns the HTTP protocol scheme, `http` or `https`.

func (Context) SetContentType

func (c Context) SetContentType(contentType string)

func (Context) Status

func (c Context) Status(status int)

func (Context) Template

func (c Context) Template(name string, model any)

func (Context) Write

func (c Context) Write(b []byte) (int, error)

func (Context) WriteHeader

func (c Context) WriteHeader(s int)

type ContextMw

type ContextMw = func(next HandlerContext) HandlerContext

type HandlerContext

type HandlerContext = func(Context)

type Jwt

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

func NewJwt

func NewJwt() (s *Jwt, err error)

func (*Jwt) Create

func (s *Jwt) Create(claims M) (bearer string, err error)

func (*Jwt) MustCreate

func (s *Jwt) MustCreate(claims M) (bearer string)

func (*Jwt) PublicKey

func (s *Jwt) PublicKey(*jwt.Token) (any, error)

func (*Jwt) Validate

func (s *Jwt) Validate(bearer string) (claims M, err error)

type M

type M map[string]any

type Middleware

type Middleware = func(next http.Handler) http.Handler

type Mux

type Mux = chi.Mux

type Params

type Params struct {
	fx.In
	DB     *gorm.DB
	Jwt    *Jwt
	Routes []Route `group:"routes"`
}

type Route

type Route interface{}

type Router

type Router = chi.Router

type Routes

type Routes = chi.Routes

type Simple

type Simple struct {
	Pattern   string
	Handler   any
	Mount     bool
	Protected bool
}

type TemplateContext

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

Context is the Context with which HTTP templates are executed.

func GetTemplateContext

func GetTemplateContext(r *http.Request, f http.FileSystem) TemplateContext

func (TemplateContext) Execute

func (c TemplateContext) Execute(templateFilename string, buf *bytes.Buffer, model any) (err error)

func (*TemplateContext) NewTemplate

func (c *TemplateContext) NewTemplate(tplName string) *template.Template

NewTemplate returns a new template intended to be evaluated with this context, as it is initialized with configuration from this context.

Jump to

Keyboard shortcuts

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