Spine不隐藏请求流程的框架
Spine 通过显式执行管道公开如何解释请求、执行顺序、何时调用业务逻辑以及如何在响应中完成请求。控制器仅表达用例,运行时负责所有有关执行的决策。
IoC容器·执行管道·拦截器链
没有魔法的执行结构
如果您是 Spring、NestJS 开发人员,请立即开始。控制器→服务→存储库层次结构、构造函数注入和拦截器链。它借用了熟悉的企业架构,但使用 Spine 的显式管道来执行。
func NewUserService(repo *UserRepository) *UserService {
return &UserService{repo: repo}
}
func NewUserController(svc *UserService) *UserController {
return &UserController{svc: svc}
}无需 JVM 预热。也没有 Node.js 运行时初始化。立即请求已编译的 Go 二进制文件。
仅使用类型系统表达依赖关系,而不需要 @Injectable、@Controller 和 @Autowired。
代替注释或约定, 签名本身揭示了结构和合同。
可以在请求之前/之后/完成时插入逻辑。 横切关注点,例如身份验证、事务和日志记录 将其与业务代码分离,放入执行流程中。
执行顺序由 Spine 的管道显式控制。
goapp.Interceptor(
&TxInterceptor{},
&AuthInterceptor{},
&LoggingInterceptor{},
)在Spine中,请求无一例外地通过一个执行管道。
路由器只是选择运行什么, 只有Pipeline知道执行顺序和流程。
无注释,无模块定义
// main.go
func main() {
app := spine.New()
// ✅ 只需注册构造函数即可自动解决依赖关系
// ✅ 可以任意顺序注册
app.Constructor(NewUserRepository, NewUserService, NewUserController)
routes.RegisterUserRoutes(app)
app.Run(boot.Options{
Address: ":8080",
EnableGracefulShutdown: true,
ShutdownTimeout: 10 * time.Second,
HTTP: &boot.HTTPOptions{},
})
}// 路线.go
func RegisterUserRoutes(app spine.App) {
// ✅ 路由和处理程序的连接是明确的
// ✅ 一目了然知道哪个方法是哪个路径
app.Route("GET", "/users", (*UserController).GetUser)
app.Route("POST", "/users", (*UserController).CreateUser)
}// 控制器.go
// ✅ 无注释——纯 Go 结构
// ✅ 测试时易于模拟
type UserController struct {
svc *UserService
}
// ✅ 构造函数参数 = 依赖声明
// ✅ 没有隐藏的魔法
func NewUserController(svc *UserService) *UserController {
return &UserController{svc: svc}
}
// ✅ 函数签名是 API 规范
// ✅ 输入(query.Values)和输出(UserResponse、error)清晰
func (c *UserController) GetUser(ctx context.Context, q query.Values) (UserResponse, error) {
return c.svc.Get(ctx, q.Int("id", 0))
}// 服务.go
// ✅ 无注释
type UserService struct {
repo *UserRepository
}
func NewUserService(repo *UserRepository) *UserService {
return &UserService{repo: repo}
}// 存储库.go
// ✅ 无注释
type UserRepository struct {
db *bun.DB
}
func NewUserRepository(db *bun.DB) *UserRepository {
return &UserRepository{db: db}
}