core

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2024 License: MIT Imports: 19 Imported by: 3

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 NewHTTPServer

func NewHTTPServer(addr string, 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 *Context) (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 {
	Config       *AppConfig
	Router       *Router
	ErrorHandler *ErrorHandler
}

func NewApp

func NewApp(cfg *AppConfig) *App

func (*App) Run

func (app *App) Run()

type AppConfig

type AppConfig struct {
	Addr  string
	Debug bool
}

type BaseResourceController added in v0.0.7

type BaseResourceController struct{}

BaseResourceController is a base controller for resources.

func (*BaseResourceController) Create added in v0.0.7

func (c *BaseResourceController) Create(ctx *Context) error

func (*BaseResourceController) Delete added in v0.0.7

func (c *BaseResourceController) Delete(ctx *Context) error

func (*BaseResourceController) Edit added in v0.0.7

func (c *BaseResourceController) Edit(ctx *Context) error

func (*BaseResourceController) Index added in v0.0.7

func (c *BaseResourceController) Index(ctx *Context) error

func (*BaseResourceController) Show added in v0.0.7

func (c *BaseResourceController) Show(ctx *Context) error

func (*BaseResourceController) Store added in v0.0.7

func (c *BaseResourceController) Store(ctx *Context) error

func (*BaseResourceController) Update added in v0.0.7

func (c *BaseResourceController) Update(ctx *Context) error

type Context added in v0.0.8

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

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

func (*Context) Context added in v0.0.8

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

func (*Context) DecodeJSON added in v0.0.8

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

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

func (*Context) GetHeader added in v0.0.8

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

GetHeader returns the value of a header in the request.

func (*Context) MakeURL added in v0.0.8

func (ctx *Context) MakeURL(name string, params map[string]string) (string, error)

MakeURL returns the URL for a route with the given name.

func (*Context) Next added in v0.0.8

func (c *Context) Next()

Next marks the middleware as having called the next handler in the chain.

func (*Context) PathValue added in v0.0.8

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

PathValue returns the value of a path parameter.

func (*Context) Redirect added in v0.0.8

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

Redirect redirects the client to the provided URL.

func (*Context) RedirectBack added in v0.0.8

func (ctx *Context) RedirectBack() error

RedirectBack redirects the client to the previous page.

func (*Context) Render added in v0.0.8

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

func (*Context) SendJSON added in v0.0.8

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

func (*Context) SendSSE added in v0.0.8

func (ctx *Context) SendSSE(name string, data string) error

SendSSE sends a Server-Sent Event to the client

func (*Context) SendText added in v0.0.8

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

func (*Context) SetHeader added in v0.0.8

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

SetHeader sets a header in the response.

func (*Context) SetSSEHeaders added in v0.0.8

func (ctx *Context) SetSSEHeaders()

SetSSEHeaders sets the headers for Server-Sent Events

func (*Context) WantsJSON added in v0.0.8

func (c *Context) WantsJSON() bool

WantsJSON returns true if the client accepts JSON responses.

func (*Context) WithStatus added in v0.0.8

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

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 *Context, err error)
}

type Handler

type Handler func(*Context) error

Handler is a function that can be used as middleware, or as a route handler.

type Resource added in v0.0.7

type Resource struct {
	Routes map[ResourceControllerMethod]*Route
}

Resource is the struct that holds the routes for a resource controller.

func (*Resource) Exclude added in v0.0.7

func (res *Resource) Exclude(methods ...ResourceControllerMethod) *Resource

Exclude excludes methods from the resource.

func (*Resource) Use added in v0.0.8

func (res *Resource) Use(middleware ...Handler) *Resource

Use adds middleware to the resource.

type ResourceController added in v0.0.7

type ResourceController interface {
	// Index returns a list of resources.
	Index(ctx *Context) error

	// Create creates a new resource.
	Create(ctx *Context) error

	// Store creates a new resource.
	Store(ctx *Context) error

	// Show returns a single resource.
	Show(ctx *Context) error

	// Edit returns a form to edit a resource.
	Edit(ctx *Context) error

	// Update updates a resource.
	Update(ctx *Context) error

	// Delete deletes a resource.
	Delete(ctx *Context) error
}

ResourceController is an interface for a controller that handles CRUD operations.

type ResourceControllerMethod added in v0.0.7

type ResourceControllerMethod string

ResourceControllerMethod is a type for the methods of a resource controller.

const (
	MethodIndex  ResourceControllerMethod = "Index"
	MethodCreate ResourceControllerMethod = "Create"
	MethodStore  ResourceControllerMethod = "Store"
	MethodShow   ResourceControllerMethod = "Show"
	MethodEdit   ResourceControllerMethod = "Edit"
	MethodUpdate ResourceControllerMethod = "Update"
	MethodDelete ResourceControllerMethod = "Delete"
)

type Route

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

Route is a route that can be added to a Router.

func (*Route) As added in v0.0.8

func (route *Route) As(name string) *Route

As sets the name of the route.

func (*Route) MakeURL added in v0.0.8

func (route *Route) MakeURL(params map[string]string) string

MakeURL generates a URL for the route.

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
	StandardRoutes map[string]http.Handler
}

Router is a router that can be used to add routes.

func NewRouter

func NewRouter() *Router

NewRouter creates a new 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) Handle added in v0.0.14

func (r *Router) Handle(pattern string, handler http.Handler)

Handle adds a standard http.Handler to the router.

func (*Router) MakeURL added in v0.0.8

func (r *Router) MakeURL(name string, values map[string]string) (string, error)

MakeURL returns the URL for a route.

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) Resource added in v0.0.7

func (r *Router) Resource(pattern string, controller ResourceController) *Resource

Resource adds a set of routes to the router for a resource controller.

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