pangu

package module
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: Apache-2.0 Imports: 43 Imported by: 14

README

pangu Golang应用程序框架

实际开发过程中,会写很多八股文代码,比如各种初始化

  • 数据库
  • Redis
  • Elasticsearch
  • 日志
  • Http客户端
  • 数据库Session操作
  • 分布式ID生成器

本代码库就是集合这些八股文代码,在基本Google Wire的基础上,提供开箱即用的功能

使用方法

创建主方法main.go
package main

import (
	`github.com/storezhang/pangu`
)

func main(){
	panic(pangu.New(
		pangu.Name("example"),
		pangu.Banner("./banner.txt", pangu.BannerTypeFile),
	).Run(newBootstrap))
}
创建程序启动器bootstrap.go
package main

import (
	`example/conf`
	`example/rest`

	`github.com/storezhang/pangu`
)

type bootstrap struct {
	application *pangu.Application
}

func newBootstrap(application *pangu.Application) pangu.Bootstrap {
	return &bootstrap{
		application: application,
	}
}

func (b *bootstrap) Setup() (err error) {
	if err = b.inject(); nil != err {
		return
	}
	if err = b.application.Get(func(server *rest.Server) error {
		return b.application.AddServe(server)
	}); nil != err {
		return
	}

	return
}

func (b *bootstrap) inject() (err error) {
	if err = conf.Provide(b.application); nil != err {
		return
	}
	if err = rest.Provide(b.application); nil != err {
		return
	}

	return
}

启动程序
./example serve

内置命令

配置文件 -c --conf --config --configuration

配置文件有默认值,顺序依次是

  • ./conf/application.yaml
  • ./conf/application.yml
  • ./conf/application.toml
  • ./conf/application.json
  • ./application.yaml
  • ./application.yml
  • ./application.toml
  • ./application.json
  • 参数指定
输出版本号 v version V Version
./example version

需要在编译的时候注入版本信息,使用方法

go build -ldflags "-s -X 'github.com/storezhang/pangu.AppName=$APP_NAME"

version

提供服务 s serve S Serve
./example serve

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AppName 应用名称
	AppName = `没有设置,请使用-ldflags "-s -X 'github.com/storezhang/pangu.AppName=$APP_NAME"来注入值`
	// AppVersion 应用版本
	AppVersion = `没有设置,请使用-ldflags "-s -X 'github.com/storezhang/pangu.AppVersion=$APP_VERSION"来注入值`
	// BuildVersion 编译版本
	BuildVersion = `没有设置,请使用-ldflags "-s -X 'github.com/storezhang/pangu.BuildVersion=$BUILD_VERSION"来注入值`
	// BuildTime 编译时间
	BuildTime = `没有设置,请使用-ldflags "-s -X 'github.com/storezhang/pangu.BuildTime=$BUILD_TIME"来注入值`
	// ScmRevision 分支版本
	ScmRevision = `没有设置,请使用-ldflags "-s -X 'github.com/storezhang/pangu.ScmRevision=$SCM_REVISION"来注入值`
	// ScmBranch 分支名称
	ScmBranch = `没有设置,请使用-ldflags "-s -X 'github.com/storezhang/pangu.ScmBranch=$SCM_BRANCH"来注入值`
	// GoVersion Golang信息
	GoVersion = runtime.Version()
)

Functions

func Banner(data interface{}, bannerType BannerType) *optionBanner

Banner 配置标志

func Default added in v1.0.2

func Default(isDefault bool) *optionDefault

Default 配置是否处理默认值

func Name

func Name(name string) *optionName

Name 配置应用名称

func NewElasticsearch

func NewElasticsearch(config gox.ElasticsearchConfig) (client *elastic.Client, err error)

NewElasticsearch 创建Elasticsearch客户端

func NewRedis

func NewRedis(config gox.RedisConfig) (client *redis.Client, err error)

NewRedis 创建Redis客户端

func NewResty

func NewResty(config gox.HttpClientConfig) (req *resty.Request)

NewResty 创建Http客户端

func NewXormEngine

func NewXormEngine(config gox.DatabaseConfig, debug gox.Debug) (engine *xorm.Engine, err error)

NewXormEngine 创建Xorm操作引擎

func Validate added in v1.0.2

func Validate(isValidate bool) *optionValidate

Validate 配置是否处理默认值

Types

type Application

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

Application 应用程序,可以加入两种种类型的程序 Serve 用于描述应用程序内的服务 Command 用于描述应用程序内可以被执行的命令

func New

func New(opts ...option) (application *Application)

New 创建一个应用程序

func (*Application) AddArgs

func (a *Application) AddArgs(args ...app.Arg) error

AddArgs 添加参数

func (*Application) AddCommands added in v1.0.1

func (a *Application) AddCommands(commands ...app.Command) error

AddCommands 添加一个可以被执行的命令到应用程序中

func (*Application) AddMigration

func (a *Application) AddMigration(source fs.FS) error

AddMigration 添加一个升级脚本到系统中

func (*Application) AddServes added in v1.0.1

func (a *Application) AddServes(serves ...Serve) error

AddServes 添加一个服务器到应用程序中

func (*Application) Get

func (a *Application) Get(function interface{}) error

func (*Application) GetConfig

func (a *Application) GetConfig(config interface{}) (err error)

GetConfig 取得解析后的配置

func (*Application) Run

func (a *Application) Run(bootstrap func(*Application) Bootstrap) (err error)

Run 启动应用程序

func (*Application) Set

func (a *Application) Set(constructor interface{}) (err error)

func (*Application) Sets

func (a *Application) Sets(constructors ...interface{}) (err error)

type BannerType

type BannerType string

BannerType 标志类型

const (
	// BannerTypeTxt 文本文件
	BannerTypeTxt BannerType = "txt"
	// BannerTypeFilepath 图片文件
	BannerTypeFilepath BannerType = "filepath"
	// BannerTypeString 直接显示
	BannerTypeString BannerType = "string"
	// BannerTypeAscii 内部转换
	BannerTypeAscii BannerType = "ascii"
	// BannerTypeBinary 二进制文件数据
	BannerTypeBinary BannerType = "binary"
	// BannerTypeFile 文件数据
	BannerTypeFile BannerType = "file"
)

type Bootstrap

type Bootstrap interface {
	// Setup 配置系统
	Setup() error
}

Bootstrap 描述一个启动器,全局只能有一个启动器,且只能返回 Bootstrap 才能被正确的启动,需要完成的事情有 添加系统的依赖关系 使用 pangu.Application.Set 添加 pangu.Serve 实现类到 pangu.Application 中 使用 pangu.Application.Set 添加 pangu.Command 实现类到 pangu.Application 中

type Serve

type Serve interface {
	// Run 运行服务器
	Run() (err error)
	// Name 服务器名称
	Name() string
}

Serve 描述一个服务器,可以是Http服务器,也可以是gRPC服务器或者一个MQ的消费者 泛指一个可以长期执行的服务

type Session

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

Session 事务控制

func NewSession

func NewSession(logger glog.Logger) Session

NewSession 事务控制

func (*Session) Close

func (s *Session) Close(tx *xorm.Session, fields ...gox.Field)

Close 关闭事务

func (*Session) Rollback

func (s *Session) Rollback(tx *xorm.Session, fields ...gox.Field)

Rollback 回退事务

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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