feat(lyra): 启动 HTTP 服务并添加示例接口
- 注册 GET /hello,使用统一响应结构并添加路由回归测试 - 使用 lyra.toml 配置 Gin 模式与监听地址,处理启动错误 - 明确 handler/service 业务分包约定并同步文档
This commit is contained in:
@@ -3,3 +3,4 @@
|
||||
- 负责参数绑定、API DTO 与内部模型的相互转换、响应输出。
|
||||
- 引用 internal/api 的 DTO,使用 Gin 完成绑定与响应。
|
||||
- 调用 service 时传递标准 context.Context,不下传 gin.Context。
|
||||
- 可按明确业务分包(如 `polaris/`);服务级简单接口保留在根包平铺,不预建空业务包。
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"lyra/internal/httpx"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Hello() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
httpx.OK(c, "Hello, World!")
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
# router 包
|
||||
|
||||
- 负责路由注册与项目路径前缀,例如 `/api/polaris`。
|
||||
- 负责路由注册与项目路径前缀,例如 `/api/polaris`;入口文件为 `router.go`,对外暴露 `RegisterRoutes`。
|
||||
- Gin 的路由与请求处理逻辑留在 HTTP 层;service 和 model 不依赖 Gin。
|
||||
- 路由随资源实现逐步补充,不按业务项目建立代码子目录。
|
||||
- 路由随资源实现逐步补充;包暂时平铺,不按业务项目建立代码子目录。
|
||||
- 路由变更以 httptest 回归测试守护,不监听实际端口、不连接数据库。
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"lyra/internal/handler"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(router *gin.Engine) {
|
||||
router.GET("/hello", handler.Hello())
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package router_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"lyra/internal/router"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestHelloRoute(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
router.RegisterRoutes(engine)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
engine.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/hello", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("GET /hello status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
want := `{"success":true,"data":"Hello, World!"}`
|
||||
if got := recorder.Body.String(); got != want {
|
||||
t.Fatalf("GET /hello body = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@
|
||||
- 承载业务规则与事务协调,通过 model 完成数据访问。
|
||||
- 需要原子性时,在同一事务内协调多次写入,不把各自独立提交的多次写入当成原子业务。
|
||||
- 使用标准 context.Context,不依赖 Gin,也不依赖 internal/api 的 DTO。
|
||||
- 可按明确业务分包(如 `polaris/`);服务级简单接口保留在根包平铺,不预建空业务包。
|
||||
|
||||
Reference in New Issue
Block a user