RapidGo
No results found

Routing

Define HTTP routes with the Gin-powered router.

RapidGo's router wraps Gin and provides a clean API for defining routes.

Basic Routes

func RegisterWeb(r *router.Router) {
    r.Get("/", controllers.Home)
    r.Post("/contact", controllers.SubmitContact)
    r.Put("/posts/:id", controllers.UpdatePost)
    r.Delete("/posts/:id", controllers.DeletePost)
    r.Patch("/settings", controllers.PatchSettings)
}

Route Groups

Group routes under a shared prefix with optional middleware:

func RegisterAPI(r *router.Router) {
    api := r.Group("/api")
    api.Use(middleware.Auth())

    api.Get("/users", controllers.ListUsers)
    api.Post("/users", controllers.CreateUser)
    api.Get("/users/:id", controllers.ShowUser)
}

Resource Controllers

Register all 7 RESTful routes with a single line:

// Full resource (7 routes — includes create/edit form routes for SSR)
r.Resource("/posts", &controllers.PostController{})

// API resource (5 routes — no create/edit form routes)
api.APIResource("/posts", &controllers.PostController{})

This generates:

Method Path Handler Description
GET /posts Index List all
GET /posts/create Create Show create form (Resource only)
POST /posts Store Create new
GET /posts/:id Show Show one
GET /posts/:id/edit Edit Show edit form (Resource only)
PUT /posts/:id Update Update one
DELETE /posts/:id Destroy Delete one

Route Parameters

Access URL parameters via Gin's context:

func ShowUser(c *gin.Context) {
    id := c.Param("id")
    // ...
}

Query Parameters

func ListUsers(c *gin.Context) {
    page := c.DefaultQuery("page", "1")
    search := c.Query("q")
    // ...
}

Static Files and Templates

// Serve static files
r.Static("/static", "resources/static")

// Load HTML templates
r.SetFuncMap(template.FuncMap{...})
r.LoadTemplates("resources/views")

Global Middleware

r.Use(middleware.Recovery())
r.Use(middleware.RequestID())
r.Use(middleware.CORS())