mochi

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: MIT Imports: 16 Imported by: 14

Documentation

Index

Constants

View Source
const (
	AuthHeaderName      = "Authorization"
	TokenExpirationTime = time.Hour * 24
)

Variables

View Source
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}
View Source
var ErrRecordNotFound = errors.New("record not found")

Functions

func BuildAppOpts

func BuildAppOpts() []fx.Option

func BuildServerOpts

func BuildServerOpts() []fx.Option

func ErrInvalidRequest

func ErrInvalidRequest(err error) render.Renderer

func ErrRender

func ErrRender(err error) render.Renderer

func ErrUnauthorized

func ErrUnauthorized(err error) render.Renderer

func ErrUnknown

func ErrUnknown(err error) render.Renderer

func NewController

func NewController[M interfaces.Resource](
	svc interfaces.Service[M],
	logger LoggerService,
	authSvc interfaces.AuthService,
	createRequestConstructor ResourceRequestConstructor[M],
	updateRequestConstructor ResourceRequestConstructor[M],
	opts ...ControllerOption[M],
) interfaces.Controller[M]

func NewFxLogger

func NewFxLogger(logger LoggerService) fxevent.Logger

func NewRouter

func NewRouter() *chi.Mux

func NewServer

func NewServer(lc fx.Lifecycle, router *chi.Mux, logger LoggerService) *http.Server

Types

type AuthService

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

func (*AuthService) AdminRequired

func (svc *AuthService) AdminRequired() func(http.Handler) http.Handler

func (*AuthService) AuthRequired

func (svc *AuthService) AuthRequired() func(http.Handler) http.Handler

func (*AuthService) GetUserFromCtx

func (svc *AuthService) GetUserFromCtx(ctx context.Context) (interfaces.User, error)

func (*AuthService) LoginUser

func (svc *AuthService) LoginUser(ctx context.Context, username, password string) (string, error)

type AuthServiceParams

type AuthServiceParams struct {
	fx.In

	Logger      LoggerService
	UserService interfaces.UserService
}

type AuthServiceResult

type AuthServiceResult struct {
	fx.Out

	AuthService interfaces.AuthService
}

func NewAuthService

func NewAuthService(params AuthServiceParams) (AuthServiceResult, error)

type Claims added in v0.0.2

type Claims struct {
	Sub uint      `json:"sub"`
	Exp time.Time `json:"exp"`
	Iat time.Time `json:"iat"`
	Nbf time.Time `json:"nbf"`
	Aud string    `json:"aud"`
	Iss string    `json:"iss"`
}

func NewClaims added in v0.0.2

func NewClaims(user interfaces.User, audience, issuer string) *Claims

func (*Claims) GetAudience added in v0.0.2

func (c *Claims) GetAudience() (jwt.ClaimStrings, error)

func (*Claims) GetExpirationTime added in v0.0.2

func (c *Claims) GetExpirationTime() (*jwt.NumericDate, error)

func (*Claims) GetIssuedAt added in v0.0.2

func (c *Claims) GetIssuedAt() (*jwt.NumericDate, error)

func (*Claims) GetIssuer added in v0.0.2

func (c *Claims) GetIssuer() (string, error)

func (*Claims) GetNotBefore added in v0.0.2

func (c *Claims) GetNotBefore() (*jwt.NumericDate, error)

func (*Claims) GetSubject added in v0.0.2

func (c *Claims) GetSubject() (string, error)

type Controller

type Controller[M interfaces.Resource] struct {
	Router *chi.Mux
	// contains filtered or unexported fields
}

func (*Controller[M]) Create

func (c *Controller[M]) Create(w http.ResponseWriter, r *http.Request)

func (*Controller[M]) Delete

func (c *Controller[M]) Delete(w http.ResponseWriter, r *http.Request)

func (*Controller[M]) Get

func (c *Controller[M]) Get(w http.ResponseWriter, r *http.Request)

func (*Controller[M]) GetRouter

func (c *Controller[M]) GetRouter() *chi.Mux

func (*Controller[M]) ItemContextMiddleware

func (c *Controller[M]) ItemContextMiddleware(next http.Handler) http.Handler

func (*Controller[M]) ItemFromContext

func (c *Controller[M]) ItemFromContext(ctx context.Context) (M, error)

func (*Controller[M]) List

func (c *Controller[M]) List(w http.ResponseWriter, r *http.Request)

func (*Controller[M]) Update

func (c *Controller[M]) Update(w http.ResponseWriter, r *http.Request)

func (*Controller[M]) UserAccessMiddleware

func (c *Controller[M]) UserAccessMiddleware(next http.Handler) http.Handler

type ControllerOption

type ControllerOption[M interfaces.Resource] func(*Controller[M])

func WithDetailRoute

func WithDetailRoute[M interfaces.Resource](method, path string, handler http.HandlerFunc) ControllerOption[M]

type ErrResponse

type ErrResponse struct {
	Err            error `json:"-"` // low-level runtime error
	HTTPStatusCode int   `json:"-"` // http response status code

	StatusText string `json:"status"`          // user-level status message
	AppCode    int64  `json:"code,omitempty"`  // application-specific error code
	ErrorText  string `json:"error,omitempty"` // application-level error message, for debugging
}

func (*ErrResponse) Render

func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error

type LoggerService

type LoggerService interface {
	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
	Logger() *slog.Logger
}

type LoggerServiceParams

type LoggerServiceParams struct {
	fx.In
}

type LoggerServiceResult

type LoggerServiceResult struct {
	fx.Out

	LoggerService LoggerService
}

func NewLoggerService

func NewLoggerService(params LoggerServiceParams) (LoggerServiceResult, error)

type ResourceContextKey

type ResourceContextKey int

type ResourceRequestConstructor

type ResourceRequestConstructor[M interfaces.Resource] func(r *http.Request) (M, error)

type Route

type Route struct {
	Method  string
	Path    string
	Handler http.HandlerFunc
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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