sdecho

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 40 Imported by: 0

Documentation

Overview

Package sdecho echo 扩展工具

Index

Constants

View Source
const (
	ActionCall = "call"
	ActionShow = "show"
)

Variables

View Source
var (
	ErrUnauthorized        = sderr.Sentinel("unauthorized")
	ErrForbidden           = sderr.Sentinel("forbidden")
	ErrBadRequest          = sderr.Sentinel("bad request")
	ErrInternalServerError = sderr.Sentinel("internal server error")
	ErrDecodeToken         = sderr.Sentinel("decode token error")
	ErrTokenExpired        = sderr.Sentinel("token expired")
	ErrLogin               = sderr.Sentinel("login error")
)
View Source
var Public = Object{/* contains filtered or unexported fields */}

Functions

func AccessControlCheck

func AccessControlCheck(ctx context.Context, ec echo.Context, token Token, object Object, action string) error

func CrossOrigin

func CrossOrigin() echo.MiddlewareFunc

func Get

func Get[T any](ec echo.Context, k string) (T, bool)

func MustGet

func MustGet[T any](ec echo.Context, k string) T

func MustHtmlRenderer

func MustHtmlRenderer(fsys fs.FS, eager bool) echo.Renderer

func NewHtmlRenderer

func NewHtmlRenderer(fsys fs.FS, eager bool) (echo.Renderer, error)

func TokenEncode

func TokenEncode(_ context.Context, ec echo.Context, t Token) string

Types

type API

type API struct {
	Path        string
	Object      Object
	Bare        bool
	Func        any
	Middlewares []echo.MiddlewareFunc
}

func (API) ToEndpoint

func (api API) ToEndpoint() Endpoint

type AccessControl

type AccessControl struct {
	Check             AccessControlChecker
	DefaultObjectVars map[string]string
}

func (AccessControl) Apply

func (ac AccessControl) Apply(app *echo.Echo) error

type AccessControlChecker

type AccessControlChecker func(ctx context.Context, ec echo.Context, token Token, object Object, action string) (bool, error)

type AntdJsonRequest

type AntdJsonRequest struct {
	Params       sdjson.Object `json:"params,omitempty"`
	Sort         sdjson.Object `json:"sort,omitempty"`
	Filter       sdjson.Object `json:"filter,omitempty"`
	UpdateFields []string      `json:"fields,omitempty"`
	ShowFlags    []string      `json:"flags,omitempty"`
}

func (*AntdJsonRequest) Fields

func (req *AntdJsonRequest) Fields() []string

func (*AntdJsonRequest) Flags

func (req *AntdJsonRequest) Flags() []string

func (*AntdJsonRequest) Keyword

func (req *AntdJsonRequest) Keyword() string

func (*AntdJsonRequest) Page

func (req *AntdJsonRequest) Page() RequestPage

func (*AntdJsonRequest) PageDef

func (req *AntdJsonRequest) PageDef(defaultSize int) RequestPage

func (*AntdJsonRequest) SetFields

func (req *AntdJsonRequest) SetFields(fields []string)

func (*AntdJsonRequest) SetFlags

func (req *AntdJsonRequest) SetFlags(flags []string)

func (*AntdJsonRequest) SetPage

func (req *AntdJsonRequest) SetPage(page RequestPage)

type BaseRequest

type BaseRequest interface {
	Page() RequestPage
	SetPage(page RequestPage)
	Fields() []string
	SetFields(fields []string)
	Flags() []string
	SetFlags(flags []string)
}

type BrowserHistoryRouterStatic

type BrowserHistoryRouterStatic struct {
	PathPrefix       string
	Fsys             fs.FS
	Root             string
	TrimPathPrefixes []string
}

func (BrowserHistoryRouterStatic) Apply

func (d BrowserHistoryRouterStatic) Apply(app *echo.Echo) error

type CasbinRbac

type CasbinRbac struct {
	Rbac              sdcasbin.Rbac
	CheckToken        func(echo.Context, Token) (bool, error)
	DefaultObjectVars map[string]string
}

func (CasbinRbac) Apply

func (ac CasbinRbac) Apply(app *echo.Echo) error

type Context

type Context struct {
	echo.Context
}

func C

func C(c echo.Context) Context

func (Context) ArgBool

func (c Context) ArgBool(name string, def bool) bool

func (Context) ArgBoolFirst

func (c Context) ArgBoolFirst(names []string, def bool) bool

func (Context) ArgFloat64

func (c Context) ArgFloat64(name string, def float64) float64

func (Context) ArgFloat64First

func (c Context) ArgFloat64First(names []string, def float64) float64

func (Context) ArgInt

func (c Context) ArgInt(name string, def int) int

func (Context) ArgInt64

func (c Context) ArgInt64(name string, def int64) int64

func (Context) ArgInt64First

func (c Context) ArgInt64First(names []string, def int64) int64

func (Context) ArgIntFirst

func (c Context) ArgIntFirst(names []string, def int) int

func (Context) ArgString

func (c Context) ArgString(name, def string) string

func (Context) ArgStringFirst

func (c Context) ArgStringFirst(names []string, def string) string

func (Context) ArgTime

func (c Context) ArgTime(name string, def time.Time) time.Time

func (Context) ArgTimeFirst

func (c Context) ArgTimeFirst(names []string, def time.Time) time.Time

func (Context) CookieInt

func (c Context) CookieInt(name string, def int) int

func (Context) CookieInt64

func (c Context) CookieInt64(name string, def int64) int64

func (Context) CookieJson

func (c Context) CookieJson(name string, v any) error

func (Context) CookieJsonObject

func (c Context) CookieJsonObject(name string, def sdjson.Object) sdjson.Object

func (Context) CookieString

func (c Context) CookieString(name, def string) string

func (Context) DeleteCookie

func (c Context) DeleteCookie(name string, path string)

func (Context) HandleMultipartFormFile

func (c Context) HandleMultipartFormFile(files []string, handler func(file FileHeader) error) error

func (Context) RequestBodyAs

func (c Context) RequestBodyAs(v any) error

func (Context) RequestBodyAsJsonArray

func (c Context) RequestBodyAsJsonArray() (sdjson.Array, error)

func (Context) RequestBodyAsJsonObject

func (c Context) RequestBodyAsJsonObject() (sdjson.Object, error)

func (Context) RequestBodyAsJsonValue

func (c Context) RequestBodyAsJsonValue() (sdjson.Value, error)

func (Context) RequestBodyBytes

func (c Context) RequestBodyBytes() ([]byte, error)

func (Context) RequestBodyString

func (c Context) RequestBodyString() (string, error)

func (Context) SetCookieInt

func (c Context) SetCookieInt(name string, val int, path string, maxAge int)

func (Context) SetCookieInt64

func (c Context) SetCookieInt64(name string, val int64, path string, maxAge int)

func (Context) SetCookieJson

func (c Context) SetCookieJson(name string, v any, path string, maxAge int) error

func (Context) SetCookieString

func (c Context) SetCookieString(name, val string, path string, maxAge int)

type CrudAPI

type CrudAPI[T Record[ID], ID RecordID, REQ BaseRequest] struct {
	Path        string
	Create      func(echo.Context, T, REQ) (T, error)
	Update      func(echo.Context, T, REQ) (T, error)
	Delete      func(echo.Context, ID, REQ) error
	Get         func(echo.Context, ID, REQ) (T, error)
	List        func(echo.Context, REQ) ([]T, error)
	Find        func(echo.Context, REQ) (*FindResult[T], error)
	Object      Object
	ObjectR     Object
	ObjectW     Object
	Middlewares []echo.MiddlewareFunc
}

func (CrudAPI[T, ID, REQ]) ToEndpoints

func (api CrudAPI[T, ID, REQ]) ToEndpoints() []Endpoint

type Echo

type Echo struct {
	*echo.Echo
}

func E

func E(e *echo.Echo) Echo

func New

func New(opts *Options) Echo

func (Echo) Install

func (e Echo) Install(features ...Feature) error

type Endpoint

type Endpoint struct {
	Methods     []string
	Path        string
	Object      Object
	Bare        bool // 跳过decode_token和access_control流程,直接调用func
	Func        any
	Middlewares []echo.MiddlewareFunc
	// contains filtered or unexported fields
}

func (*Endpoint) ToHandler

func (endpoint *Endpoint) ToHandler() (echo.HandlerFunc, error)

type Feature

type Feature interface {
	Apply(app *echo.Echo) error
}

type FeatureFunc

type FeatureFunc func(*echo.Echo) error

func (FeatureFunc) Apply

func (f FeatureFunc) Apply(app *echo.Echo) error

type FileHeader

type FileHeader struct {
	File string
	*multipart.FileHeader
}

func (FileHeader) ReadData

func (fh FileHeader) ReadData(sizeLimit int64) ([]byte, error)

type FindAPI

type FindAPI[T Record[ID], ID RecordID, REQ BaseRequest] struct {
	Path        string
	Object      Object
	Bare        bool
	Func        func(echo.Context, REQ) (*FindResult[T], error)
	Middlewares []echo.MiddlewareFunc
}

func (FindAPI[T, ID, REQ]) ToEndpoint

func (api FindAPI[T, ID, REQ]) ToEndpoint() Endpoint

type FindResult

type FindResult[T any] struct {
	Data      []T
	Request   any
	NumRows   int
	PageSize  int
	PageNum   int
	PageTotal int
}

func (*FindResult[T]) ToResult added in v0.1.3

func (fr *FindResult[T]) ToResult(err error) *Result

type Inject

type Inject map[string]any

func (Inject) Apply

func (inject Inject) Apply(app *echo.Echo) error

type ListAPI

type ListAPI[T Record[ID], ID RecordID, REQ BaseRequest] struct {
	Path        string
	Object      Object
	Bare        bool
	Func        func(echo.Context, REQ) ([]T, error)
	Middlewares []echo.MiddlewareFunc
}

func (ListAPI[T, ID, REQ]) ToEndpoint

func (api ListAPI[T, ID, REQ]) ToEndpoint() Endpoint
type Menu struct {
	Id    string      `json:"id"`
	Items []*MenuItem `json:"items,omitempty"`
}
func MenuReify(ctx context.Context, ec echo.Context, menuId string, tags []string) *Menu
func (menu *Menu) Reify(ctx context.Context, ec echo.Context, token Token, tags []string) *Menu
type MenuItem struct {
	Name     string      `json:"name"`
	Path     string      `json:"path,omitempty"`
	Href     string      `json:"href,omitempty"`
	Icon     string      `json:"icon,omitempty"`
	Children []*MenuItem `json:"children,omitempty"`
	Object   Object      `json:"-"`
	Tags     []string    `json:"-"`
}
func (item *MenuItem) Reify(ctx context.Context, ec echo.Context, token Token, tags []string) *MenuItem
type Menus []*Menu
func (menus Menus) Apply(app *echo.Echo) error

type NoRedirectStatic

type NoRedirectStatic struct {
	PathPrefix       string
	Fsys             fs.FS
	Root             string
	TrimPathPrefixes []string
}

func (NoRedirectStatic) Apply

func (d NoRedirectStatic) Apply(app *echo.Echo) error

type Object

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

func O

func O(id string, tags ...string) Object

func (Object) Expand

func (o Object) Expand(mapper any, others ...any) Object

func (Object) HasTags

func (o Object) HasTags() bool

func (Object) Id

func (o Object) Id() string

func (Object) IsEmpty

func (o Object) IsEmpty() bool

func (Object) IsExpanded

func (o Object) IsExpanded() bool

func (Object) IsPublic

func (o Object) IsPublic() bool

func (Object) MatchTag

func (o Object) MatchTag(tag string) bool

func (Object) MatchTagsAny

func (o Object) MatchTagsAny(tags []string) bool

func (Object) String

func (o Object) String() string

func (Object) TagCount

func (o Object) TagCount() int

func (Object) Tags

func (o Object) Tags() []string

type Options

type Options struct {
	DebugMode    bool
	LogSkipper   middleware.Skipper
	ErrorHandler echo.HTTPErrorHandler
}

type Record

type Record[ID RecordID] interface {
	RecordID() ID
}

type RecordID

type RecordID interface{ ~string | ~int | ~int64 }

type RequestPage

type RequestPage struct {
	Page     int
	PageSize int
}

func (RequestPage) Tuple

func (page RequestPage) Tuple() (int, int)

type Result

type Result struct {

	// http
	HttpStatus  int
	ContentType string
	Headers     map[string]string

	// data
	Facade any

	// API & HTML
	Code   any
	Fields map[string]any
	Data   any
	Error  error
	View   string
	// contains filtered or unexported fields
}

func ResultErr

func ResultErr(err any, msgs ...string) *Result

func ResultOf

func ResultOf(data any, err any, errMsgs ...string) *Result

func ResultOk

func ResultOk(data any) *Result

func ResultPage

func ResultPage(data any, view string) *Result

func ResultRaw

func ResultRaw(httpStatus int, contentType string, data []byte) *Result

func (*Result) WithCode

func (r *Result) WithCode(code any) *Result

func (*Result) WithContentType

func (r *Result) WithContentType(contentType string) *Result

func (*Result) WithFacade

func (r *Result) WithFacade(facade any) *Result

func (*Result) WithField

func (r *Result) WithField(k string, v any) *Result

func (*Result) WithFields

func (r *Result) WithFields(fields map[string]any) *Result

func (*Result) WithHeader

func (r *Result) WithHeader(k, v string) *Result

func (*Result) WithHeaders

func (r *Result) WithHeaders(headers map[string]string) *Result

func (*Result) WithHttpStatus

func (r *Result) WithHttpStatus(status int) *Result

func (*Result) Write

func (r *Result) Write(ec echo.Context, opts *ResultOptions) error

type ResultOptions

type ResultOptions struct {
	CodeOk           any
	CodeBadRequest   any
	CodeTokenExpired any
	CodeUnauthorized any
	CodeForbidden    any
	CodeLogin        any
	CodeNotFound     any
	CodeUnknown      any
}

type Routes

type Routes struct {
	BasePath  string
	Endpoints []any
	DumpRoute func(method, path string)
	*ResultOptions
}

func (Routes) Apply

func (routes Routes) Apply(app *echo.Echo) error

func (Routes) ExpandEndpoints

func (routes Routes) ExpandEndpoints() ([]*Endpoint, error)

type Token

type Token struct {
	UID  string `json:"uid,omitempty"`
	From string `json:"form,omitempty"`
	At   int64  `json:"at,omitempty"`
}

func TokenDecode

func TokenDecode(_ context.Context, ec echo.Context) (Token, error)

func TokenGet

func TokenGet(ctx context.Context, ec echo.Context) Token

type Tokens

type Tokens struct {
	Secrets    []string
	GetEncoded func(echo.Context) string
	IsExpired  func(echo.Context, Token) bool
}

func (Tokens) Apply

func (tt Tokens) Apply(app *echo.Echo) error

type WebPage

type WebPage struct {
	Method      string
	Path        string
	Object      Object
	Bare        bool
	Func        any
	Middlewares []echo.MiddlewareFunc
}

func (WebPage) ToEndpoint

func (p WebPage) ToEndpoint() Endpoint

Jump to

Keyboard shortcuts

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