Ginject
Core Concepts

Routing

Ginject parses controller method names into HTTP routes automatically. Learn the full token reference, versioning, and wildcard patterns.

Routing

Ginject derives HTTP routes from controller method names at startup. No annotations, struct tags, or code generation are needed. The method name is tokenized on underscores and each token has a semantic meaning.

Token Reference

TokenHTTP Method / MeaningExample method → route
READGETREADGET /
CREATEPOSTCREATEPOST /
UPDATEPUTUPDATEPUT /
MODIFYPATCHMODIFYPATCH /
DELETEDELETEDELETEDELETE /
PREFLIGHTOPTIONSPREFLIGHTOPTIONS /
BYPath parameter (:token)READ_BY_IDGET /:id
ANDAdditional path segmentREAD_BY_ID_AND_NAMEGET /:id/:name
OFNamed sub-resourceREAD_OF_USERGET /user
ANYWildcard (*)READ_ANYGET /*
VERSIONAPI version prefixREAD_VERSION_1 → versioned route
FILEStatic file servingREAD_FILE → static file handler

Basic Routes

type UserController struct {
    common.REST
}
 
func (c UserController) NewController() common.Controller {
    c.Prefix("users")
    return c
}
 
func (c *UserController) READ() {}                    // GET  /users
func (c *UserController) CREATE() {}                  // POST /users
func (c *UserController) READ_BY_ID() {}              // GET  /users/:id
func (c *UserController) UPDATE_BY_ID() {}            // PUT  /users/:id
func (c *UserController) MODIFY_BY_ID() {}            // PATCH /users/:id
func (c *UserController) DELETE_BY_ID() {}            // DELETE /users/:id

Multiple Path Parameters

Chain BY and AND for multiple params in the same route:

func (c *UserController) READ_BY_ID_AND_TYPE() {}
// GET /users/:id/:type
 
func (c *UserController) READ_BY_ORG_AND_REPO_AND_BRANCH() {}
// GET /users/:org/:repo/:branch

Sub-Resources with OF

func (c *UserController) READ_OF_PROFILE() {}
// GET /users/profile
 
func (c *UserController) READ_OF_PROFILE_BY_ID() {}
// GET /users/profile/:id

Wildcard Routes

func (c *UserController) READ_ANY() {}
// GET /users/*

Wildcards match all remaining path segments.

Static File Serving

func (c *StaticController) READ_FILE() {}
// Serves files from the configured public directory

Route Versioning

Enable versioning on the app before creating:

import "github.com/dangduoc08/ginject/versioning"
 
app := core.New()
app.EnableVersioning(versioning.URL) // or versioning.Header, versioning.Query
app.Create(AppModule)

Then append _VERSION_N to method names:

func (c *UserController) READ_VERSION_1() {}  // GET /v1/users
func (c *UserController) READ_VERSION_2() {}  // GET /v2/users

Versioning modes:

ModeURL pattern
versioning.URL/v1/users, /v2/users
versioning.HeaderAccept-Version: 1 header
versioning.Query/users?version=1

Global Prefix

Set a global prefix that applies to all routes in the app:

app.SetGlobalPrefix("api")
// All routes become /api/users, /api/products, etc.

Path Parameters

Access path parameters via ctx.Param in handler arguments:

func (c *UserController) READ_BY_ID(exec *ctx.ExecutionContext, param ctx.Param) User {
    id := param.Get("id")
    return c.UserService.FindOne(exec, id)
}

Query Parameters

Access query parameters via ctx.Query:

func (c *UserController) READ(exec *ctx.ExecutionContext, query ctx.Query) []User {
    page := query.Get("page")    // e.g. ?page=2
    limit := query.Get("limit")  // e.g. ?limit=20
    return c.UserService.FindAll(exec, page, limit)
}

Naming Conventions

  • Tokens are case-insensitive in the parsing, but must be uppercase in the method name for Go to export them.
  • All tokens between BY, AND, and OF are lowercased for the actual path segment. READ_BY_USER_ID produces /:userID (camelCased from the multi-word segment).

On this page