cago

package module
v0.0.0-...-83d2edf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2025 License: MIT Imports: 11 Imported by: 0

README

Cago

godoc Go Report Card License

Cago 一个快速开发的集成式框架.使用模块化的开发模式,每一个组件都可以单独的调用.

Cago 只对社区工具进行集成,大大减少迁移难度和学习成本,我们不生产代码,我们只是方案的搬运工.

使用 go 的struct来声明 API 和 swagger 文档,可以通过脚手架来帮助你生成相关内容.

快速开始

你可以通过简单示例来快速的了解 Cago 的使用。你也可以复制这个示例来创建你的项目。

简单示例

脚手架

你可以安装脚手架来帮助你生成代码。

脚手架文档

CI/CD

部署资源

cago 提供了gitlab-cigolanglint-ciKubernetes helm的 CI/CD 配置文件,可以快速帮你实现 CI/CD.

当本地调试时也可以使用docker-compose.yaml启动调试环境.

可以使用etcd作为配置中心,也支持文件作为配置启动。

组件

大多数组件都是基于社区工具进行封装,方便使用。

目录结构

Cago使用三层架构,并混合了DDD的思想,推荐使用下面的目录结构。你也可以根据自己的需求来调整目录结构。

  • cmd/app/main.go 项目的入口
  • configs
    • config.yaml 配置文件
    • ... 其他配置文件,也可以写配置读取方法,其它包调用:configs.GetConfig().XXX
  • docs 文档目录,包括 swagger api 文档、设计文档等
  • deploy 部署资源文件
  • internal 内部包
    • api API 请求结构体
      • example/example.go api请求与返回结构
      • router.go 路由定义
    • controller 控制器目录,API请求会调用控制器,做一些数据处理校验逻辑
    • model 数据模型
      • entity 数据库实体模型,推荐使用充血模型
      • xxx.go 一些数据模型的定义
    • pkg 项目内通用的模块
      • code 错误码定义
      • utils 工具包
    • repository 数据库操作
    • service 服务接口
    • task 任务模块
      • crontab 定时任务
      • queue 消息队列
        • handler 消息队列处理器
        • message 消息定义
        • xxx.go 消息队列定义
      • task.go 任务定义
  • migrations 数据库迁移文件
  • pkg 公共的模块,可以被其它项目引用
  • .golangci.yml golangci-lint 配置文件
  • Makefile 项目的 Makefile 文件

参考项目

  • GoFrame
  • GoMicro

Who use Cago?

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version return the version of cago

Types

type Cago

type Cago struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, cfg *configs.Config) *Cago

New create a new cago instance ctx 可以管理整个应用的生命周期,当ctx.Done()时,会传递到每一个组件,安全退出 cfg 配置文件,每一个组件都可以使用,通过 configs.NewConfig 去构建 应用启动时,会调用每一个组件的 Component.Start 方法,启动组件 应用停止时,会调用每一个组件的 Component.CloseHandle 方法,关闭组件 推荐链式调用的方式去使用 cago.New(ctx, cfg).Registry(component.Core()).RegistryCancel(mux.HTTP(api.Router)).Start()

func (*Cago) DisableLogger

func (r *Cago) DisableLogger() *Cago

DisableLogger 禁用框架日志

func (*Cago) Registry

func (r *Cago) Registry(component Component) *Cago

Registry 注册组件

func (*Cago) RegistryCancel

func (r *Cago) RegistryCancel(component ComponentCancel) *Cago

RegistryCancel 注册cancel组件,cancel组件可以停止整个应用

func (*Cago) Start

func (r *Cago) Start() error

Start 启动框架 在此之前组件已全部执行 Component.Start 方法启动,此处只做停止等待 可以通过ctx、cancelFunc和进程信号量来控制整个应用的生命周期 停止时会调用 Component.CloseHandle 方法关闭组件,会等待所有组件关闭完成,最终关闭整个应用

type CloseHandle

type CloseHandle func()

type Component

type Component interface {
	// Start 启动组件 会传入框架的context和配置文件
	Start(ctx context.Context, cfg *configs.Config) error
	// CloseHandle 关闭处理 当应用停止时,会调用该方法,停止组件
	CloseHandle()
}

Component 组件接口

type ComponentCancel

type ComponentCancel interface {
	Component
	// StartCancel 启动组件,会传入框架的context和配置文件,以及cancel方法
	// 可以通过cancel方法停止整个应用
	StartCancel(ctx context.Context, cancel context.CancelFunc, cfg *configs.Config) error
}

ComponentCancel 带cancel方法的组件 可以停止整个应用

type FuncComponent

type FuncComponent func(ctx context.Context, cfg *configs.Config) error

FuncComponent 函数式组件 适用于简单的组件 当你的组件不需要释放资源时,你可以只写一个函数,然后通过 FuncComponent 转换成组件 例如:

cago.FuncComponent(func(ctx context.Context, cfg *configs.Config) error {
	return nil
})

func (FuncComponent) CloseHandle

func (f FuncComponent) CloseHandle()

func (FuncComponent) Start

func (f FuncComponent) Start(ctx context.Context, cfg *configs.Config) error

type FuncComponentCancel

type FuncComponentCancel func(ctx context.Context, cancel context.CancelFunc, cfg *configs.Config) error

FuncComponentCancel 函数式带cancel方法的组件 适用于简单的组件,与 FuncComponent 的区别是多了一个cancel方法

func (FuncComponentCancel) CloseHandle

func (f FuncComponentCancel) CloseHandle()

func (FuncComponentCancel) Start

func (FuncComponentCancel) StartCancel

func (f FuncComponentCancel) StartCancel(ctx context.Context, cancel context.CancelFunc, cfg *configs.Config) error

Directories

Path Synopsis
cmd
cago command
database
db
examples
simple/cmd/app command
simple/docs
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag at 2024-04-12 17:50:02.541948 +0800 CST m=+0.060448417
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag at 2024-04-12 17:50:02.541948 +0800 CST m=+0.060448417
simple/internal/repository/user_repo/mock
Package mock_user_repo is a generated GoMock package.
Package mock_user_repo is a generated GoMock package.
internal
cmd
pkg
iam
oss
utils/testutils
一些用于测试的工具函数
一些用于测试的工具函数
server
mux

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL