core

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 20, 2024 License: MIT Imports: 17 Imported by: 1

README

📜 Caesar Core

Caesar is a Go web framework, designed for productivity. It takes inspiration from traditional web frameworks such as Ruby on Rails, Laravel, Django, Phoenix, AdonisJS, etc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPMux

func NewHTTPMux(router *Router, errorHandler *ErrorHandler) *http.ServeMux

func NewHTTPServer

func NewHTTPServer(lc fx.Lifecycle, mux *http.ServeMux) *http.Server

func RetrieveErrorCode

func RetrieveErrorCode(err error) int

func ServeStaticAssets

func ServeStaticAssets(fs embed.FS) func(r *Router)

ServeStaticAssets returns a provider, which serves static assets from the embed.FS.

func Validate

func Validate[T interface{}](ctx *CaesarCtx) (data *T, validationErrors map[string]string, ok bool)

Validate validates the request body or form values. It returns the data, the validation errors, and a boolean indicating if the data is valid.

func ValidateEnvironmentVariables

func ValidateEnvironmentVariables[T interface{}]() *T

ValidateEnvironmentVariables validates the environment variables, and panics if any of the environment variables aren't correctly set.

Types

type App

type App struct {
	Providers []any
	Invokers  []any
	Config    *AppConfig
}

func NewApp

func NewApp(cfg *AppConfig) *App

func (*App) RegisterInvokers

func (app *App) RegisterInvokers(invokers ...any)

func (*App) RegisterProviders

func (app *App) RegisterProviders(providers ...any)

func (*App) RetrieveRouter

func (app *App) RetrieveRouter() *Router

func (*App) Run

func (app *App) Run()

type AppConfig

type AppConfig struct {
	Addr string
}

type CaesarCtx

type CaesarCtx struct {
	ResponseWriter http.ResponseWriter
	Request        *http.Request
	// contains filtered or unexported fields
}

CaesarCtx is a wrapper around http.ResponseWriter and *http.Request, that is augmented with some Caesar-specific methods.

func (*CaesarCtx) Context

func (c *CaesarCtx) Context() context.Context

func (*CaesarCtx) DecodeJSON

func (c *CaesarCtx) DecodeJSON(v any) error

DecodeJSON decodes the JSON body of the request into the provided value.

func (*CaesarCtx) GetHeader

func (ctx *CaesarCtx) GetHeader(key string) string

GetHeader returns the value of a header in the request.

func (*CaesarCtx) PathValue

func (c *CaesarCtx) PathValue(key string) string

PathValue returns the value of a path parameter.

func (*CaesarCtx) Redirect

func (ctx *CaesarCtx) Redirect(to string) error

Redirect redirects the client to the provided URL.

func (*CaesarCtx) Render

func (c *CaesarCtx) Render(component templ.Component) error

func (*CaesarCtx) SendJSON

func (c *CaesarCtx) SendJSON(v interface{}, statuses ...int) error

func (*CaesarCtx) SendText

func (c *CaesarCtx) SendText(text string, statuses ...int) error

func (*CaesarCtx) SetHeader

func (ctx *CaesarCtx) SetHeader(key string, value string)

SetHeader sets a header in the response.

func (*CaesarCtx) WithStatus

func (c *CaesarCtx) WithStatus(statusCode int) *CaesarCtx

type Error

type Error struct {
	Code int `json:"code"`
}

func NewError

func NewError(code int) *Error

func (*Error) Error

func (e *Error) Error() string

type ErrorHandler

type ErrorHandler struct {
	Handle func(c *CaesarCtx, err error)
}

type Handler

type Handler func(*CaesarCtx) error

type Route

type Route struct {
	Method     string
	Pattern    string
	Handler    Handler
	Middleware []Handler
}

func (*Route) Use

func (route *Route) Use(handler Handler) *Route

Use adds middleware to the route.

type Router

type Router struct {
	Routes     []*Route
	Mux        *http.ServeMux
	Middleware []Handler
}

func NewRouter

func NewRouter() *Router

func (*Router) Any

func (r *Router) Any(pattern string, handler Handler) *Route

Any adds a route that matches any HTTP method.

func (*Router) Delete

func (r *Router) Delete(pattern string, handler Handler) *Route

Delete adds a route that handles DELETE requests.

func (*Router) Get

func (r *Router) Get(pattern string, handler Handler) *Route

Get adds a GET route to the router.

func (*Router) Patch

func (r *Router) Patch(pattern string, handler Handler) *Route

Patch adds a route that uses the PUT method.

func (*Router) Post

func (r *Router) Post(pattern string, handler Handler) *Route

Post adds a route that only matches POST requests.

func (*Router) Put

func (r *Router) Put(pattern string, handler Handler) *Route

Put adds a route that handles DELETE requests.

func (*Router) Render

func (r *Router) Render(pattern string, component templ.Component) *Route

Render adds a route (with a GET method) that renders a component.

func (*Router) Use

func (r *Router) Use(handler Handler)

Use adds middleware to the whole router (all routes).

Jump to

Keyboard shortcuts

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