演示 Darkit Gin 的基础功能,包括路由定义、参数获取和响应方法。
cd examples/basic
go run main.go服务将在 http://localhost:8080 启动。
请求:
curl http://localhost:8080/users/1响应:
{
"code": 200,
"message": "success",
"data": {
"id": 1,
"name": "张三",
"email": "zhangsan@example.com"
},
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": 1703001234
}请求:
curl "http://localhost:8080/users?page=1&per_page=10"响应:
{
"code": 200,
"message": "success",
"data": [
{
"id": 1,
"name": "张三",
"email": "zhangsan@example.com"
},
{
"id": 2,
"name": "李四",
"email": "lisi@example.com"
},
{
"id": 3,
"name": "王五",
"email": "wangwu@example.com"
}
],
"pagination": {
"page": 1,
"per_page": 10,
"total": 3,
"total_pages": 1
},
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": 1703001234
}请求:
curl -X POST http://localhost:8080/users \
-H "Content-Type: application/json" \
-d '{
"name": "赵六",
"email": "zhaoliu@example.com"
}'响应:
{
"code": 201,
"message": "created",
"data": {
"id": 100,
"name": "赵六",
"email": "zhaoliu@example.com"
},
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": 1703001234
}请求:
curl -X PUT http://localhost:8080/users/1 \
-H "Content-Type: application/json" \
-d '{
"name": "张三(已更新)",
"email": "zhangsan_new@example.com"
}'请求:
curl -X DELETE http://localhost:8080/users/1响应:
HTTP/1.1 204 No Content
400 Bad Request:
curl "http://localhost:8080/errors/demo?type=400"{
"code": 400,
"message": "错误的请求参数",
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": 1703001234
}422 Validation Error:
curl "http://localhost:8080/errors/demo?type=422"{
"code": 422,
"message": "validation failed",
"errors": [
{
"field": "name",
"message": "名称不能为空"
},
{
"field": "email",
"message": "邮箱格式不正确"
}
],
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": 1703001234
}请求:
curl http://localhost:8080/info \
-H "User-Agent: Mozilla/5.0" \
-H "X-Requested-With: XMLHttpRequest"响应:
{
"code": 200,
"message": "success",
"data": {
"ip": "127.0.0.1",
"user_agent": "Mozilla/5.0",
"is_ajax": true,
"is_json": false
},
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": 1703001234
}所有响应自动包装为统一格式:
code: HTTP 状态码message: 响应消息data: 业务数据request_id: 请求追踪 IDtimestamp: Unix 时间戳
c.Success(data)- 200 OKc.Created(data)- 201 Createdc.Accepted(data)- 202 Acceptedc.NoContent()- 204 No Contentc.Paginated(data, page, perPage, total)- 分页响应c.BadRequest(message)- 400 Bad Requestc.Unauthorized(message)- 401 Unauthorizedc.Forbidden(message)- 403 Forbiddenc.NotFound(message)- 404 Not Foundc.Conflict(message)- 409 Conflictc.ValidationError(errors)- 422 Unprocessable Entityc.InternalError(message)- 500 Internal Server Errorc.Error(code, message)- 自定义状态码
c.Param(key, default...)- 获取字符串参数(路径/查询/表单)c.ParamInt(key, default...)- 获取整数参数c.ParamInt64(key, default...)- 获取 int64 参数c.ParamFloat(key, default...)- 获取 float64 参数c.ParamBool(key, default...)- 获取 bool 参数c.GetIP()- 获取客户端 IPc.GetUserAgent()- 获取 User-Agentc.IsAjax()- 判断是否 AJAX 请求c.IsJSON()- 判断是否 JSON 请求
engine.Default() 自动启用:
- RequestID: 自动生成请求 ID
- Recovery: Panic 恢复
- Logger: 请求日志
监听 SIGINT/SIGTERM/SIGQUIT 信号,自动执行优雅停机:
- 停止接受新请求
- 等待当前请求完成
- 超时后强制退出