router

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultFuncMap

func DefaultFuncMap() template.FuncMap

DefaultFuncMap returns the template function map with framework helpers.

func Name

func Name(name, pattern string)

Name registers a route name mapped to a path pattern.

func ResetNamedRoutes

func ResetNamedRoutes()

ResetNamedRoutes clears all named routes. Used in tests only.

func Route

func Route(name string, params ...string) string

Route generates a URL from a named route with parameter substitution. Parameters replace :param placeholders in registration order. Returns "/" if the route name is not found.

Example: Route("users.show", "42") → "/users/42"

Types

type ResourceController

type ResourceController interface {
	Index(c *gin.Context)   // GET    /resource
	Create(c *gin.Context)  // GET    /resource/create  (SSR form)
	Store(c *gin.Context)   // POST   /resource
	Show(c *gin.Context)    // GET    /resource/:id
	Edit(c *gin.Context)    // GET    /resource/:id/edit (SSR form)
	Update(c *gin.Context)  // PUT    /resource/:id
	Destroy(c *gin.Context) // DELETE /resource/:id
}

ResourceController defines the interface for RESTful controllers. Implement all 7 methods for full CRUD with form routes, or use APIResource to skip Create/Edit form routes.

type RouteGroup

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

RouteGroup wraps a Gin router group for sub-route registration.

func (*RouteGroup) APIResource

func (g *RouteGroup) APIResource(path string, ctrl ResourceController)

APIResource registers 5 RESTful routes (no Create/Edit form routes) on the group.

func (*RouteGroup) Delete

func (g *RouteGroup) Delete(path string, handlers ...gin.HandlerFunc)

Delete registers a DELETE route in the group.

func (*RouteGroup) Get

func (g *RouteGroup) Get(path string, handlers ...gin.HandlerFunc)

Get registers a GET route in the group.

func (*RouteGroup) Group

func (g *RouteGroup) Group(prefix string, handlers ...gin.HandlerFunc) *RouteGroup

Group creates a nested sub-group with a shared prefix and optional middleware.

func (*RouteGroup) Options

func (g *RouteGroup) Options(path string, handlers ...gin.HandlerFunc)

Options registers an OPTIONS route in the group.

func (*RouteGroup) Patch

func (g *RouteGroup) Patch(path string, handlers ...gin.HandlerFunc)

Patch registers a PATCH route in the group.

func (*RouteGroup) Post

func (g *RouteGroup) Post(path string, handlers ...gin.HandlerFunc)

Post registers a POST route in the group.

func (*RouteGroup) Put

func (g *RouteGroup) Put(path string, handlers ...gin.HandlerFunc)

Put registers a PUT route in the group.

func (*RouteGroup) Resource

func (g *RouteGroup) Resource(path string, ctrl ResourceController)

Resource registers all 7 RESTful routes for a controller on the group.

func (*RouteGroup) Use

func (g *RouteGroup) Use(middleware ...gin.HandlerFunc)

Use adds middleware to the group.

type Router

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

Router wraps the Gin engine and provides framework-level route registration.

func New

func New() *Router

New creates a new Router with Gin mode set based on APP_ENV.

func (*Router) APIResource

func (r *Router) APIResource(path string, ctrl ResourceController)

APIResource registers 5 RESTful routes (no Create/Edit form routes) on the router.

func (*Router) Delete

func (r *Router) Delete(path string, handlers ...gin.HandlerFunc)

Delete registers a DELETE route.

func (*Router) DeprecatedVersion

func (r *Router) DeprecatedVersion(version, sunsetDate string) *RouteGroup

DeprecatedVersion creates a versioned route group (like Version) but injects middleware that adds deprecation headers to every response:

  • Sunset: {sunsetDate} — RFC 8594 sunset date in HTTP-date format
  • X-API-Deprecated: true — simple boolean signal for clients/monitoring

Example:

v1 := r.DeprecatedVersion("v1", "Sat, 01 Jun 2026 00:00:00 GMT")
v1.Get("/users", listUsersV1)

func (*Router) Engine

func (r *Router) Engine() *gin.Engine

Engine returns the underlying Gin engine.

func (*Router) Get

func (r *Router) Get(path string, handlers ...gin.HandlerFunc)

Get registers a GET route.

func (*Router) Group

func (r *Router) Group(prefix string, handlers ...gin.HandlerFunc) *RouteGroup

Group creates a new route group with a shared prefix and optional middleware.

func (*Router) LoadTemplates

func (r *Router) LoadTemplates(dir string)

LoadTemplates recursively loads all .html templates from the given directory.

func (*Router) Options

func (r *Router) Options(path string, handlers ...gin.HandlerFunc)

Options registers an OPTIONS route.

func (*Router) Patch

func (r *Router) Patch(path string, handlers ...gin.HandlerFunc)

Patch registers a PATCH route.

func (*Router) Post

func (r *Router) Post(path string, handlers ...gin.HandlerFunc)

Post registers a POST route.

func (*Router) Put

func (r *Router) Put(path string, handlers ...gin.HandlerFunc)

Put registers a PUT route.

func (*Router) Resource

func (r *Router) Resource(path string, ctrl ResourceController)

Resource registers all 7 RESTful routes for a controller on the router.

func (*Router) Run

func (r *Router) Run(addr string) error

Run starts the HTTP server on the given address.

func (*Router) ServeHTTP

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

ServeHTTP implements http.Handler, delegating to the Gin engine.

func (*Router) SetFuncMap

func (r *Router) SetFuncMap(funcMap template.FuncMap)

SetFuncMap sets the template function map on the Gin engine. Must be called before LoadTemplates.

func (*Router) Static

func (r *Router) Static(urlPath, dirPath string)

Static serves files from a local directory under the given URL path.

func (*Router) StaticFile

func (r *Router) StaticFile(urlPath, filePath string)

StaticFile serves a single file at the given URL path.

func (*Router) Use

func (r *Router) Use(middleware ...gin.HandlerFunc)

Use adds global middleware to the router.

func (*Router) Version

func (r *Router) Version(version string) *RouteGroup

Version creates a route group prefixed with /api/{version}. The returned RouteGroup supports all existing route registration methods.

Example:

v1 := r.Version("v1") // prefix: /api/v1
v1.Get("/users", listUsers)
v1.APIResource("/posts", &PostController{})

Jump to

Keyboard shortcuts

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