Skip to content

bb-orz/goinfras

Repository files navigation

Goinfras

Goinfras是一个后端应用基础设施的资源组件启动器,其实现了一些后端常用的组件客户端或工具,并提供可执行的实例。 使用goinfras,你只需要在项目中注册所需要的组件,并编写所需要的配置信息,最后让Application启动即可使用。

实现的资源组件:

用例

简单项目模板项目: https://github.com/bb-orz/goapp-sample

通用账户项目模板: https://github.com/bb-orz/goapp-account

使用启动器项目,您只需做如下步骤:

Step 1:注册您所需要的启动器
import (
	"github.com/bb-orz/goinfras"
	"github.com/bb-orz/goinfras/XCron"
	"github.com/bb-orz/goinfras/XEtcd"
	"github.com/bb-orz/goinfras/XLogger"
	"github.com/bb-orz/goinfras/XMQ/XNats"
	"github.com/bb-orz/goinfras/XMQ/XRedisPubSub"
	"github.com/bb-orz/goinfras/XOAuth"
	"github.com/bb-orz/goinfras/XOss/XAliyunOss"
	"github.com/bb-orz/goinfras/XOss/XQiniuOss"
	"github.com/bb-orz/goinfras/XStore/XGorm"
	"github.com/bb-orz/goinfras/XStore/XMongo"
	"github.com/bb-orz/goinfras/XStore/XRedis"
	"github.com/bb-orz/goinfras/XValidate"
	"github.com/gin-gonic/gin"

	_ "github.com/bb-orz/goapp-sample/simple/core" // 自动载入核心业务逻辑注册
	_ "github.com/bb-orz/goapp-sample/simple/restful" // 自动载入Restful 模块路由注册
	"github.com/bb-orz/goinfras/XGin"
)

// 注册应用组件启动器,把基础设施各资源组件化
func RegisterStarter(viperCfg *viper.Viper) {
	
	goinfras.RegisterStarter(XLogger.NewStarter())

	// 注册Cron定时任务
	// 可以自定义一些定时任务给starter启动
	goinfras.RegisterStarter(XCron.NewStarter())

	// 注册ETCD
	goinfras.RegisterStarter(XEtcd.NewStarter())

	// 注册mongodb启动器
	goinfras.RegisterStarter(XMongo.NewStarter())

	// 注册mysql启动器
	goinfras.RegisterStarter(XGorm.NewStarter())
	// 注册Redis连接池
	goinfras.RegisterStarter(XRedis.NewStarter())
	// 注册Oss
	goinfras.RegisterStarter(XAliyunOss.NewStarter())
	goinfras.RegisterStarter(XQiniuOss.NewStarter())
	// 注册Mq
	goinfras.RegisterStarter(XNats.NewStarter())
	goinfras.RegisterStarter(XRedisPubSub.NewStarter())
	// 注册Oauth Manager
	goinfras.RegisterStarter(XOAuth.NewStarter())


	// 注册gin web 服务启动器
	// TODO add your gin middlewares
	middlewares := make([]gin.HandlerFunc, 0)
	goinfras.RegisterStarter(XGin.NewStarter(middlewares...))

	// 注册验证器
	goinfras.RegisterStarter(XValidate.NewStarter())

	// 对资源组件启动器进行排序
	goinfras.SortStarters()

}
Step 2:选择您的web引擎:gin/echo,定义相应的接口并在包初始化时注册接口路由
func init() {
	// 初始化时自动注册该API到Gin Engine
	XGin.RegisterApi(new(SimpleApi))
}

type SimpleApi struct {
	service1 services.IService1
}

// SetRouter由Gin Engine 启动时调用
func (s *SimpleApi) SetRoutes() {
	s.service1 = services.GetService1()

	engine := XGin.XEngine()

	engine.GET("simple/foo", s.Foo)
	engine.GET("simple/bar", s.Bar)
}

func (s *SimpleApi) Foo(ctx *gin.Context) {
	email := ctx.Param("email")
	// 调用服务
	err := s.service1.Foo(services.InDTO{Email: email})

	// 处理错误
	fmt.Println(err)
}

func (s *SimpleApi) Bar(ctx *gin.Context) {
	email := ctx.Param("email")
	// 调用服务
	err := s.service1.Bar(services.InDTO{Email: email})

	// 处理错误
	fmt.Println(err)
}


Step 3:创建应用并启动
var app *goinfras.Application // 应用实例

func main() {
	// 初始化Viper配置加载器,导入配置,启动参数由命令行flag输入
	fmt.Println("Viper Config Loading  ......")
	viperCfg := goinfras.ViperLoader()

	// 注册应用组件启动器
	fmt.Println("Register Starters  ......")
	RegisterStarter(viperCfg)

	// 创建应用程序启动管理器
	app = goinfras.NewApplication(viperCfg)

	// 运行应用,启动已注册的资源组件
	fmt.Println("Application Starting ......")
	app.Up()
}
Step4:确定你的配置信息,可通过环境变量、配置文件或远程配置中心设置

本项目提供模板配置文件供参考:example.yaml

运行goinfras的项目需注意:

  • 使用goinfras.ViperLoader(),载入初始viper配置实例时,默认接收以下命令行参数,获取viper实例初始配置:
    • -f :Config file flag,like: -f ../config/config.yaml
    • -P : Remote K/V config flag, system provider,support etcd/consul. like: -P=etcd
    • -E : Remote K/V config flag, system endpoint,etcd requires http://ip:port consul requires ip:port
    • -K : Remote K/V config flag, k is the path in the k/v store to retrieve configuration,like: -K /configs/myapp.json"
    • -T : Remote K/V config flag, upport: 'json', 'toml', 'yaml', 'yml', 'properties', 'props', 'prop', 'env', 'dotenv'. like: -T=json
    • -D : Remote K/V config flag, Currently, only tested with etcd support
    • -a : ENV config flag, enable automatic, like: -a=true
    • -e : ENV config flag, allow env empty,like: -e=false
    • -p : ENV config flag, env prefix,like: -p=goinfras_
    • -k : ENV config flag, env keys,like: -k=aaa -k=bbb
Step5:构建并启动
cd {YourProject}/app
go build


// 启动
./app -f=../config/example.yaml

==================StdOut====================
Viper Config Loading  ......
Viper File Config Was Loaded  ......
Register Starters  ......
Sorted Starters:
XLogger Starter Attention!
XAliyunOss Starter Attention!
XEtcd Starter Attention!
XMongo Starter Attention!
XGorm Starter Attention!
XRedis Starter Attention!
XCron Starter Attention!
XQiniuOss Starter Attention!
XNats Starter Attention!
XRedisPubSub Starter Attention!
XOAuth Starter Attention!
XGin Starter Attention!
XValidate Starter Attention!
Application Starting ......

......

[GIN-debug] Listening and serving HTTP on 127.0.0.1:8090


工具

gt https://github.com/bb-orz/gt

gt 是一个go应用脚手架生成工具,它会帮助您初始化一个应用程序脚手架,其内置一些代码生成命令,可帮您生成model/domain/service/restful/rpc/starter等代码模板,极大的提高您的开发效率。

About

一个通用的Go Web 项目基础设施启动器,让开发者更容易使用开源项目构建应用。

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages