router

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package router provides HTTP routing functionality for the Velocity framework. It's built on top of gorilla/mux and provides automatic route discovery, RESTful resources, and a clean API for web applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentRoute

func CurrentRoute(r *http.Request) string

CurrentRoute returns the current route name if it exists

func LoadRoutes

func LoadRoutes()

LoadRoutes applies all registered routes to the global router

func Param

func Param(r *http.Request, name string) string

Param extracts a route parameter from the request

func Params

func Params(r *http.Request) map[string]string

Params returns all route parameters from the request

func Register

func Register(fn RegistrationFunc)

Register adds a route registration function to be called during initialization

func RegisterWithPrefix

func RegisterWithPrefix(prefix string, fn RegistrationFunc)

RegisterWithPrefix adds a route registration function with a prefix

func Route

func Route(name string, params map[string]string) (string, error)

Route generates a URL for a named route

func Wrap

Wrap converts a HandlerFunc to http.HandlerFunc

Types

type Context

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

Context wraps http.Request and http.ResponseWriter with helper methods

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request) *Context

NewContext creates a new Context from http.Request and http.ResponseWriter

func (*Context) BadRequest

func (c *Context) BadRequest(message ...string) error

BadRequest sends a 400 error response

func (*Context) Bind

func (c *Context) Bind(v interface{}) error

Bind parses the request body as JSON into the given struct

func (*Context) Cookie

func (c *Context) Cookie(name string) (*http.Cookie, error)

Cookie returns a cookie by name

func (*Context) Error

func (c *Context) Error(status int, message string) error

Error sends a JSON error response

func (*Context) Forbidden

func (c *Context) Forbidden(message ...string) error

Forbidden sends a 403 error response

func (*Context) Get

func (c *Context) Get(key string) interface{}

Get retrieves a value from the context

func (*Context) GetString

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

GetString retrieves a string value from the context

func (*Context) HTML

func (c *Context) HTML(status int, html string) error

HTML sends an HTML response

func (*Context) Header

func (c *Context) Header(name string) string

Header returns a request header value

func (*Context) IP

func (c *Context) IP() string

IP returns the client IP address

func (*Context) IsAjax

func (c *Context) IsAjax() bool

IsAjax returns true if the request is an AJAX request

func (*Context) JSON

func (c *Context) JSON(status int, data interface{}) error

JSON sends a JSON response with the given status code

func (*Context) Method

func (c *Context) Method() string

Method returns the HTTP method

func (*Context) NoContent

func (c *Context) NoContent() error

NoContent sends a 204 No Content response

func (*Context) NotFound

func (c *Context) NotFound(message ...string) error

NotFound sends a 404 error response

func (*Context) Param

func (c *Context) Param(name string) string

Param returns a route parameter by name

func (*Context) Path

func (c *Context) Path() string

Path returns the request path

func (*Context) Query

func (c *Context) Query(name string) string

Query returns a query parameter by name

func (*Context) QueryDefault

func (c *Context) QueryDefault(name, defaultValue string) string

QueryDefault returns a query parameter or default value if not set

func (*Context) Redirect

func (c *Context) Redirect(status int, url string) error

Redirect redirects to a URL with the given status code

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set stores a value in the context

func (*Context) SetCookie

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie sets a cookie on the response

func (*Context) SetHeader

func (c *Context) SetHeader(name, value string)

SetHeader sets a response header

func (*Context) Status

func (c *Context) Status(status int) error

Status sends a response with just a status code

func (*Context) String

func (c *Context) String(status int, text string) error

String sends a plain text response

func (*Context) Unauthorized

func (c *Context) Unauthorized(message ...string) error

Unauthorized sends a 401 error response

func (*Context) WantsJSON

func (c *Context) WantsJSON() bool

WantsJSON returns true if the client expects a JSON response

type Error

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

Error represents an HTTP error response

type HandlerFunc

type HandlerFunc func(c *Context) error

HandlerFunc is the Velocity handler function signature

type MiddlewareFunc

type MiddlewareFunc func(next HandlerFunc) HandlerFunc

MiddlewareFunc is the Velocity middleware function signature

type RegistrationFunc

type RegistrationFunc func(Router)

RegistrationFunc is a function that registers routes

type ResourceRoute

type ResourceRoute interface {
	Only(methods ...string) ResourceRoute
	Except(methods ...string) ResourceRoute
}

ResourceRoute represents a resource with configurable methods

type RouteConfig

type RouteConfig interface {
	Name(name string) RouteConfig
	Use(middlewares ...MiddlewareFunc) RouteConfig
}

RouteConfig represents a single route that can be configured

type RouteNotFoundError

type RouteNotFoundError struct {
	Name string
}

RouteNotFoundError is returned when a named route doesn't exist

func (*RouteNotFoundError) Error

func (e *RouteNotFoundError) Error() string

type Router

type Router interface {
	// HTTP Methods - accepts Context-based handlers
	Get(path string, handler HandlerFunc) RouteConfig
	Post(path string, handler HandlerFunc) RouteConfig
	Put(path string, handler HandlerFunc) RouteConfig
	Delete(path string, handler HandlerFunc) RouteConfig
	Patch(path string, handler HandlerFunc) RouteConfig
	Options(path string, handler HandlerFunc) RouteConfig
	Head(path string, handler HandlerFunc) RouteConfig

	// Route Management
	Group(prefix string) Router
	Prefix(prefix string)
	Resource(path string, controller interface{}) ResourceRoute

	// Middleware - Context-based
	Use(middlewares ...MiddlewareFunc) Router

	// Serving
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	Handle() http.Handler
}

Router defines the interface for HTTP routing

type VelocityRouter

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

VelocityRouter is the main router implementation using gorilla/mux

func Get

func Get() *VelocityRouter

Get returns the global router instance, creating it if necessary

func New

func New() *VelocityRouter

New creates a new router instance

func (*VelocityRouter) Delete

func (r *VelocityRouter) Delete(path string, handler HandlerFunc) RouteConfig

Delete registers a DELETE route

func (*VelocityRouter) Get

func (r *VelocityRouter) Get(path string, handler HandlerFunc) RouteConfig

Get registers a GET route

func (*VelocityRouter) Group

func (r *VelocityRouter) Group(prefix string) Router

Group creates a new router group with a prefix

func (*VelocityRouter) Handle

func (r *VelocityRouter) Handle() http.Handler

Handle returns the underlying http.Handler

func (*VelocityRouter) Head

func (r *VelocityRouter) Head(path string, handler HandlerFunc) RouteConfig

Head registers a HEAD route

func (*VelocityRouter) Options

func (r *VelocityRouter) Options(path string, handler HandlerFunc) RouteConfig

Options registers an OPTIONS route

func (*VelocityRouter) Patch

func (r *VelocityRouter) Patch(path string, handler HandlerFunc) RouteConfig

Patch registers a PATCH route

func (*VelocityRouter) Post

func (r *VelocityRouter) Post(path string, handler HandlerFunc) RouteConfig

Post registers a POST route

func (*VelocityRouter) Prefix

func (r *VelocityRouter) Prefix(prefix string)

Prefix sets a prefix for all routes in this router

func (*VelocityRouter) Put

func (r *VelocityRouter) Put(path string, handler HandlerFunc) RouteConfig

Put registers a PUT route

func (*VelocityRouter) Resource

func (r *VelocityRouter) Resource(path string, controller interface{}) ResourceRoute

Resource creates RESTful routes for a controller

func (*VelocityRouter) ServeHTTP

func (r *VelocityRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements http.Handler interface

func (*VelocityRouter) Static

func (r *VelocityRouter) Static(directory string)

Static serves static files from the specified directory Files are served at the root level (e.g., /logo.png serves from public/logo.png)

func (*VelocityRouter) Use

func (r *VelocityRouter) Use(middlewares ...MiddlewareFunc) Router

Use adds middleware to the router/group

Jump to

Keyboard shortcuts

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