Skip to content

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

go
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:

go
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:

ParameterTypeRequiredDescription
httpMethodstringYesHTTP method, e.g. "GET", "POST", "PUT", "DELETE"
pathstringYesRoute path, e.g. for "GET" it could be "/api/hello"
handlerHandlerFuncYesRoute handler function, signature is func(*Ctx)

Returns: None

Example:

go
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.

go
type HandlerFunc func(*Ctx)

Example:

go
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.

FieldTypeDescription
GinCtx*gin.ContextGin native request context, can directly call Gin API
ErranyPanic error captured during request processing
Timedint64Request execution time (milliseconds), automatically calculated by Handle() internally
Argsmap[string]anyRequest 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:

ParameterTypeRequiredDescription
dataanyYesResponse data, typically a *ehelper.ResultJson struct

Returns: None

Example:

go
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:

ParameterTypeRequiredDescription
codeintYesHTTP status code, e.g. http.StatusOK (200), http.StatusBadRequest (400)
dataanyYesResponse data

Returns: None

Example:

go
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 valueTypeDescription
argmap[string]anyRequest parameter map; nil when parsing fails
okbooltrue indicates parsing succeeded, false indicates request body is empty or parsing failed

Example:

go
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:

go
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