Introduction
Route module, encapsulates Gin route registration and request context handling. Provides the Handle() function for registering routes, the Ctx struct to uniformly encapsulate request parameters, execution time, and error information, as well as convenience response methods such as JSON() / JSONWithCode() / ArgJson(). Internally exposes GinRouter to support direct use of Gin API for route registration.
Import
import "github.com/wallace5303/ee-go/ehttp/router"Note: The import path is
ehttp/router, and it is typically referenced with an alias in business code:
import (
eRouter "github.com/wallace5303/ee-go/ehttp/router"
)API
Handle(httpMethod, path, handler)
Description: Register a route rule. Internally converts HandlerFunc to a Gin handler function, automatically parses request body JSON parameters into Ctx.Args, records execution time, and captures panic and prints access logs in defer.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| httpMethod | string | Yes | HTTP method, e.g. "GET", "POST", "PUT", "DELETE" |
| path | string | Yes | Route path, e.g. for "GET" it could be "/api/hello" |
| handler | HandlerFunc | Yes | Route handler function, signature is func(*Ctx) |
Returns: None
Example:
import (
eRouter "github.com/wallace5303/ee-go/ehttp/router"
"electron-egg/api"
)
func Api() {
eRouter.Handle("GET", "/api/hello", api.Hello)
eRouter.Handle("GET", "/api/exit", api.Exit)
eRouter.Handle("GET", "/api/getValue", api.GetValue)
eRouter.Handle("POST", "/api/setValue", api.SetValue)
}HandlerFunc
Description: Route handler function type definition.
type HandlerFunc func(*Ctx)Example:
func Hello(c *router.Ctx) {
ret := ehelper.GetJson()
defer c.JSON(ret)
ret.Data = "hello electron-egg"
}Ctx
Description: Route request context, encapsulates Gin context, request parameters, execution time, and error information.
| Field | Type | Description |
|---|---|---|
| GinCtx | *gin.Context | Gin native request context, can directly call Gin API |
| Err | any | Panic error captured during request processing |
| Timed | int64 | Request execution time (milliseconds), automatically calculated by Handle() internally |
| Args | map[string]any | Request body JSON parameters, automatically parsed by Handle() via ShouldBindJSON; nil when parsing fails |
Ctx.JSON(data)
Description: Return JSON format response data with HTTP 200 status code.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| data | any | Yes | Response data, typically a *ehelper.ResultJson struct |
Returns: None
Example:
func Hello(c *router.Ctx) {
ret := ehelper.GetJson()
ret.Data = "hello"
c.JSON(ret)
}See also: ehelper.GetJson()
Ctx.JSONWithCode(code, data)
Description: Return JSON format response data with the specified HTTP status code.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| code | int | Yes | HTTP status code, e.g. http.StatusOK (200), http.StatusBadRequest (400) |
| data | any | Yes | Response data |
Returns: None
Example:
func BadRequest(c *router.Ctx) {
ret := ehelper.GetJson()
ret.Code = -1
ret.Msg = "parameter error"
c.JSONWithCode(http.StatusBadRequest, ret)
}See also: ehelper.GetJson()
Ctx.ArgJson()
Description: Get request body JSON parameters. When parameters exist and are parsed successfully, returns the parameter map and true; when parameters are empty or parsing fails, returns an empty map and false, and marks the error in the returned ResultJson.
Parameters: None (uses the parameters already parsed in Ctx.Args)
Returns:
| Return value | Type | Description |
|---|---|---|
| arg | map[string]any | Request parameter map; nil when parsing fails |
| ok | bool | true indicates parsing succeeded, false indicates request body is empty or parsing failed |
Example:
func SetValue(c *router.Ctx) {
ret := ehelper.GetJson()
defer c.JSON(ret)
arg, ok := c.ArgJson()
if !ok {
ret.Code = -1
ret.Msg = "request parameter parsing failed"
return
}
keyName := arg["key"].(string)
value := arg["value"]
// Business logic...
}GinRouter
Description: Gin native engine instance, exposed to business code to support direct use of Gin API for route registration (such as Gin middleware, route groups, etc.).
var GinRouter *gin.Engine
Example:
import (
eRouter "github.com/wallace5303/ee-go/ehttp/router"
)
func Api() {
// Directly use Gin to register routes
eRouter.GinRouter.GET("/api/info", api.Info)
}See also: gin.Engine
