Introduction
HTTP service module, creates and manages the HTTP server based on the Gin framework. Responsible for service startup, CORS cross-origin, Gzip compression, Session management, static resource loading, route dispatching, platform detection, and port occupancy checking. In the ee-go initialization flow, it is automatically started by eboot calling CreateServer().
Import
import "github.com/wallace5303/ee-go/ehttp"Constants
PlatformPC
string — "pc", the platform identifier for the Electron desktop client.
PlatformBrowser
string — "browser", the platform identifier for the browser desktop client.
PlatformPhone
string — "phone", the platform identifier for the mobile client.
PlatformPad
string — "pad", the platform identifier for the tablet client.
Conf
map[string]any — HTTP configuration global variable, written by CreateServer(), stores http configuration items (port, network, etc.).
API
CreateServer(cfg)
Description: Create and start the HTTP server. Based on the Gin framework, it sequentially registers CORS, Session, and Gzip middleware, loads debug routes (pprof), static resources and view routes, checks port occupancy, and then listens for service.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| cfg | map[string]any | Yes | HTTP configuration, contains port (float64→int), network (bool), and other fields |
Returns: None
Example:
httpCfg := econfig.GetHttp()
if httpCfg["enable"] == true {
ehttp.CreateServer(httpCfg)
}See also: econfig.GetHttp(), erouter
GetPlatform(ctx)
Description: Determine the current access platform type based on the User-Agent header of the HTTP request. Electron client returns "pc"; tablets or large-screen Android return "browser"; mobile returns "phone"; other browsers return "browser".
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| ctx | *gin.Context | Yes | Gin request context |
Returns: string — platform identifier, one of PlatformPC, PlatformBrowser, PlatformPhone
Example:
platform := ehttp.GetPlatform(ctx)
if platform == ehttp.PlatformPC {
// Desktop logic
} else if platform == ehttp.PlatformPhone {
// Mobile logic
}IsPortOpen(port)
Description: Check whether the specified TCP port is already occupied. Attempts to connect to 127.0.0.1:port via net.DialTimeout within 1 second. If the connection succeeds, the port is open (occupied).
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| port | string | Yes | Port number string, e.g. "7073" |
Returns: bool — true indicates the port is occupied, false indicates the port is available
Example:
port := "7073"
if ehttp.IsPortOpen(port) {
fmt.Printf("Port %s is already in use\n", port)
} else {
fmt.Printf("Port %s is available\n", port)
}