app

package
v0.0.0-...-1b35c59 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2022 License: MIT Imports: 16 Imported by: 0

README

app

app是一个用来快速构建应用框架的包.

说明

通过学习孔令飞老师的课程:Go 语言项目开发实战,了解了一个优秀的企业应用框架所应具备的功能。

本仓库中所有的代码都来自于老师课程中的项目。为了加深理解和方便使用,稍作修改,留做己用。

概览

我们目前见到的 Go 后端服务,基本上可以分为 API 服务和非 API 服务两类:

  • API 服务:通过对外提供 HTTP/RPC 接口来完成指定的功能。比如订单服务,通过调用创建订单的 API 接口,来创建商品订单。
  • 非 API 服务:通过监听、定时运行等方式,而不是通过 API 调用来完成某些任务。比如数据处理服务,定时从 Redis 中获取数据,处理后存入后端存储中。再比如消息处理服务,监听消息队列(如 NSQ/Kafka/RabbitMQ),收到消息后进行处理。

对于 API 服务和非 API 服务来说,它们的启动流程基本一致,都可以分为三步:

  1. 应用框架的构建,这是最基础的一步。
  2. 应用初始化。
  3. 服务启动。

aaa

图中,命令行程序、命令行参数解析和配置文件解析,是所有服务都需要具备的功能,这些功能有机结合到一起,共同构成了应用框架。

所以,我们要构建的任何一个应用程序,至少要具备命令行程序、命令行参数解析和配置文件解析这 3 种功能。

  • 命令行程序:用来启动一个应用。命令行程序需要实现诸如应用描述、help、参数校验等功能。根据需要,还可以实现命令自动补全、打印命令行参数等高级功能。
  • 命令行参数解析:用来在启动时指定应用程序的命令行参数,以控制应用的行为。
  • 配置文件解析:用来解析不同格式的配置文件。

上述 3 类功能跟业务关系不大,可以抽象成一个统一的框架。appframe包就是对此抽象框架的代码实现

应用初始化、创建 API/ 非 API 服务、启动服务,跟业务联系比较紧密,难以抽象成一个统一的框架。

如何使用

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatBaseName

func FormatBaseName(basename string) string

FormatBaseName 根据操作系统增加二进制后缀

Types

type App

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

App 构建应用框架结构

func NewApp

func NewApp(basename string, name string, opts ...Options) *App

NewApp 创建应用

func (*App) AddCommand

func (a *App) AddCommand(cmd *Command)

AddCommand 给应用添加命令

func (*App) AddCommands

func (a *App) AddCommands(cmds ...*Command)

AddCommands 给应用添加多个命令

func (*App) Command

func (a *App) Command() *cobra.Command

Command return cmd

func (*App) Run

func (a *App) Run()

Run 命令行运行

type CliOptions

type CliOptions interface {
	//返回应用程序自定义的所有命令行标识集
	Flags() (fss cliflag.NamedFlagSets)
	//验证运行命令行时给定的标识参数
	Validate() []error
}

CliOptions 命令行配置选项的抽象接口 每个应用程序的命令行选项有哪些由应用自己决定。框架层面配置命令行选项时逻辑统一。

type Command

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

Command 命令行程序子命令结构。

func NewCommad

func NewCommad(usage string, desc string, opts ...CommandOption) *Command

NewCommad 创建子命令

func (*Command) AddCommand

func (c *Command) AddCommand(cmd *Command)

AddCommand 给命令添加子命令

func (*Command) AddCommands

func (c *Command) AddCommands(cmds ...*Command)

AddCommands 添加多个子命令

type CommandOption

type CommandOption func(*Command)

CommandOption 选项模式初始化Command结构

func WithCommandOptions

func WithCommandOptions(cliOptions CliOptions) CommandOption

WithCommandOptions 设置选项参数

func WithCommandRunFunc

func WithCommandRunFunc(run RunCommandFunc) CommandOption

WithCommandRunFunc 设置命令行回调函数

type CompleteableOptions

type CompleteableOptions interface {
	Complete() error
}

CompleteableOptions 选项自动补全接口

type ConfigurableOptions

type ConfigurableOptions interface {
	ApplyFlags() []error
}

ConfigurableOptions 从配置文件读取参数配置选项

type Options

type Options func(*App)

Options 选项模式配置实例参数

func WithDefaultValidArgs

func WithDefaultValidArgs() Options

WithDefaultValidArgs 设置默认的参数验证函数验证非选项参数

func WithDescription

func WithDescription(desc string) Options

WithDescription 设置描述

func WithNoConfig

func WithNoConfig() Options

WithNoConfig 设置应用程序不提供 config选项参数

func WithNoVersion

func WithNoVersion() Options

WithNoVersion 设置应用程序不提供 version选项参数

func WithOptions

func WithOptions(opt CliOptions) Options

WithOptions 设置命令行选项

func WithRunFunc

func WithRunFunc(run RunFunc) Options

WithRunFunc 设置应用程序run回调

func WithSilence

func WithSilence() Options

WithSilence 设置静默选项

func WithValidArgs

func WithValidArgs(args cobra.PositionalArgs) Options

WithValidArgs 设置非选项参数的验证函数

type PrintableOptions

type PrintableOptions interface {
	String() string
}

PrintableOptions 选项打印接口

type RunCommandFunc

type RunCommandFunc func(args []string) error

RunCommandFunc 定义命令的回调函数

type RunFunc

type RunFunc func(basename string) error

RunFunc 程序运行回调函数

Jump to

Keyboard shortcuts

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