action

package
v0.0.0-...-0894c66 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: Unlicense Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxRequestBodySize = 1024_00
)

Variables

This section is empty.

Functions

func Api

func Api[Input any, Output IHttpOutput](
	group *ActionGroup,
	serializer ISerializer,
	fnc func(ctx context.Context, input *Input) (Output, error),
	opts *ActionOptions,
)

func DoAdminCall

func DoAdminCall(cli *http.Client, checker IAdminChecker, req *http.Request) (*http.Response, error)

func JSONApi

func JSONApi[Input any, Output IHttpOutput](
	group *ActionGroup,
	fnc func(ctx context.Context, input *Input) (Output, error),
	opts *ActionOptions,
)

func MustPeekRequest

func MustPeekRequest(ctx context.Context) *http.Request

func NewJsonRequest

func NewJsonRequest[T any](ctx context.Context, method string, url string, val T) (*http.Request, error)

func NewSpeedLimitedRespWriter

func NewSpeedLimitedRespWriter(ctx context.Context, writer http.ResponseWriter, kbs int) http.ResponseWriter

func PeekRequest

func PeekRequest(ctx context.Context) (*http.Request, bool)

func ServeGzipedFS

func ServeGzipedFS(prefix string, fs http.FileSystem) http.HandlerFunc

ServeGzipedFS serves pre-gziped files.

Types

type ActionGroup

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

func NewGroup

func NewGroup(module_prefix string, optsfnc func(opts *ActionOptions) *ActionOptions) *ActionGroup

func (*ActionGroup) RawApi

func (group *ActionGroup) RawApi(fnc ApiAction, opts *ActionOptions)

func (*ActionGroup) ToHandler

func (group *ActionGroup) ToHandler(actiongetter func(req *http.Request) string) http.Handler

func (*ActionGroup) WithConfig

func (group *ActionGroup) WithConfig(cfg *Config) *ActionGroup

func (*ActionGroup) WithLogger

func (group *ActionGroup) WithLogger(logger *slog.Logger) *ActionGroup

func (*ActionGroup) WithRemoteIpProvider

func (group *ActionGroup) WithRemoteIpProvider(remoteipprovider IRemoteIPProvider) *ActionGroup

func (*ActionGroup) WithSessionProvider

func (group *ActionGroup) WithSessionProvider(sessionprovider ISessionProvider) *ActionGroup

type ActionOptions

type ActionOptions struct {
	SessionPolicy  SessionPolicyKind
	RateLimitTakeN int
	RequireAdmin   bool
	OnlyDebug      bool
}

type ApiAction

type ApiAction func(ctx context.Context, req *http.Request, respw http.ResponseWriter) error

type Config

type Config struct {
	Debug        bool
	AdminAuthDir string
	ErrorMap     func(err error) (IHttpError, bool)
}

type GzippedOutput

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

func NewGzippedOutput

func NewGzippedOutput(code int, content []byte, contentType string) *GzippedOutput

func (*GzippedOutput) BytesBody

func (obj *GzippedOutput) BytesBody() ([]byte, bool)

func (*GzippedOutput) Code

func (obj *GzippedOutput) Code() int

func (*GzippedOutput) Headers

func (obj *GzippedOutput) Headers() http.Header

type IAdminChecker

type IAdminChecker interface {
	Check(ctx context.Context, ip string, req *http.Request) error
	Do(ctx context.Context, cli *http.Client, req *http.Request) (*http.Response, error)
}

func NewFsAdminChecker

func NewFsAdminChecker(secret string, fsdir string, header string, filesize int64) IAdminChecker

NewFsAdminChecker Help dockerized services to verify that admin requests from the host. It mandates a read-only mounted fsdir, rejecting all requests if write permission is detected.

type IBind

type IBind interface {
	Bind(req *http.Request) error
}

type IHttpError

type IHttpError interface {
	error
	Code() int
}

func NewHttpError

func NewHttpError(code int, fmt string, args ...any) IHttpError

type IHttpOutput

type IHttpOutput interface {
	Code() int
	Headers() http.Header
	BytesBody() ([]byte, bool)
}

type IRemoteIPProvider

type IRemoteIPProvider interface {
	Get(req *http.Request) string
}

func NewHeadersRemoteIPProvider

func NewHeadersRemoteIPProvider(headers ...string) IRemoteIPProvider

type ISerializer

type ISerializer interface {
	Deserialize(req *http.Request, dst any) error
	Header() http.Header
	Serialize(ctx context.Context, w io.Writer, src any) error
}

ISerializer impls not need to close req.Body in Deserialize method

var JSONSerializer ISerializer = _JsonSerializer{}

type ISession

type ISession interface {
	Take()
	TakeN(times int)
}

func PeekSession

func PeekSession(ctx context.Context) (ISession, bool)

type ISessionProvider

type ISessionProvider interface {
	Get(req *http.Request) (ISession, bool)
	Ensure(req *http.Request, respw http.ResponseWriter) ISession
}

type JsonBytesOutput

type JsonBytesOutput struct {
	Txt []byte
}

func NewJsonBytesOutput

func NewJsonBytesOutput[T any](val T) *JsonBytesOutput

func (*JsonBytesOutput) BytesBody

func (j *JsonBytesOutput) BytesBody() ([]byte, bool)

func (*JsonBytesOutput) Code

func (j *JsonBytesOutput) Code() int

func (*JsonBytesOutput) Headers

func (j *JsonBytesOutput) Headers() http.Header

func (*JsonBytesOutput) MarshalJSON

func (j *JsonBytesOutput) MarshalJSON() ([]byte, error)

type Output

type Output[T any] struct {
	// contains filtered or unexported fields
}

func NewOutput

func NewOutput[T any](val T) *Output[T]

func (*Output[T]) BytesBody

func (c *Output[T]) BytesBody() ([]byte, bool)

func (*Output[T]) Code

func (c *Output[T]) Code() int

func (*Output[T]) Headers

func (c *Output[T]) Headers() http.Header

func (*Output[T]) MarshalJSON

func (c *Output[T]) MarshalJSON() ([]byte, error)

func (*Output[T]) WithCode

func (c *Output[T]) WithCode(code int) *Output[T]

func (*Output[T]) WithHeader

func (c *Output[T]) WithHeader(fnc func(http.Header)) *Output[T]

type PlainTextOutput

type PlainTextOutput struct {
	Txt string
}

func NewPlainTextOutput

func NewPlainTextOutput(txt string) *PlainTextOutput

func (*PlainTextOutput) BytesBody

func (p *PlainTextOutput) BytesBody() ([]byte, bool)

func (*PlainTextOutput) Code

func (p *PlainTextOutput) Code() int

func (*PlainTextOutput) Headers

func (p *PlainTextOutput) Headers() http.Header

func (*PlainTextOutput) MarshalJSON

func (p *PlainTextOutput) MarshalJSON() ([]byte, error)

type SessionPolicyKind

type SessionPolicyKind int
const (
	SessionPolicyKindAuto SessionPolicyKind = iota
	SessionPolicyKindRequire
	SessionPolicyKindNone
)

type SpeedLimitedRespWriter

type SpeedLimitedRespWriter struct {
	Writer http.ResponseWriter
	*rate.Limiter
	// contains filtered or unexported fields
}

func (*SpeedLimitedRespWriter) Header

func (s *SpeedLimitedRespWriter) Header() http.Header

func (*SpeedLimitedRespWriter) Write

func (s *SpeedLimitedRespWriter) Write(p []byte) (int, error)

func (*SpeedLimitedRespWriter) WriteHeader

func (s *SpeedLimitedRespWriter) WriteHeader(statusCode int)

type StringMap

type StringMap struct {
	Map map[string]string
}

func (*StringMap) UnmarshalJSON

func (s *StringMap) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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