Skip to content

Latest commit

 

History

History
264 lines (235 loc) · 11 KB

18.GoFrame工具链之项目构建.md

File metadata and controls

264 lines (235 loc) · 11 KB

GoFrame工具链之项目构建

项目结构

推荐的Go业务型项目目录结构如下:

/
├── Makefile
├── README.MD
├── api
│   └── hello
├── go.mod
├── go.sum
├── hack
│   ├── config.yaml
│   ├── hack-cli.mk
│   └── hack.mk
├── internal
│   ├── cmd
│   ├── consts
│   ├── controller
│   ├── dao
│   ├── logic
│   ├── model
│   ├── packed
│   └── service
├── main.go
├── manifest
│   ├── config
│   ├── deploy
│   ├── docker
│   ├── i18n
│   └── protobuf
├── resource
│   ├── public
│   └── template
└── utility
目录/文件名称 说明 描述
api 对外接口 对外提供服务的输入/输出数据结构定义。考虑到版本管理需要,往往以api/xxx/v1...存在。
hack 工具脚本 存放项目开发工具、脚本等内容。例如,CLI工具的配置,各种shell/bat脚本等文件。
internal 内部逻辑 业务逻辑存放目录。通过Golang internal特性对外部隐藏可见性。
- cmd 入口指令 命令行管理目录。可以管理维护多个命令行。
- consts 常量定义 项目所有常量定义。
- controller 接口处理 接收/解析用户输入参数的入口/接口层。
- dao 数据访问 数据访问对象,这是一层抽象对象,用于和底层数据库交互,仅包含最基础的 CURD 方法
- logic 业务封装 业务逻辑封装管理,特定的业务逻辑实现和封装。往往是项目中最复杂的部分。
- model 结构模型 数据结构管理模块,管理数据实体对象,以及输入与输出数据结构定义。
- do 领域对象 用于dao数据操作中业务模型与实例模型转换,由工具维护,用户不能修改。
- entity 数据模型 数据模型是模型与数据集合的一对一关系,由工具维护,用户不能修改。
- service 业务接口 用于业务模块解耦的接口定义层。具体的接口实现在logic中进行注入。
manifest 交付清单 包含程序编译、部署、运行、配置的文件。常见内容如下:
- config 配置管理 配置文件存放目录。
- docker 镜像文件 Docker镜像相关依赖文件,脚本文件等等。
- deploy 部署文件 部署相关的文件。默认提供了Kubernetes集群化部署的Yaml模板,通过kustomize管理。
- protobuf 协议文件 GRPC协议时使用的protobuf协议定义文件,协议文件编译后生成go文件到api目录。
resource 静态资源 静态资源文件。这些文件往往可以通过 资源打包/镜像编译 的形式注入到发布文件中。
go.mod 依赖管理 使用Go Module包管理的依赖描述文件。
main.go 入口文件 程序入口文件。

在实践中,小伙伴们可以根据实际情况增删目录。

初始化项目init

D:\17.gfcli>gf init -h
USAGE
    gf init ARGUMENT [OPTION]

ARGUMENT
    NAME    name for the project. It will create a folder with NAME in current directory.
            The NAME will also be the module name for the project.

OPTION
    -m, --mono     initialize a mono-repo instead a single-repo
    -u, --update   update to the latest goframe version
    -h, --help     more information about this command

EXAMPLE
    gf init my-project
    gf init my-mono-repo -m


D:\17.gfcli>gf init gfcli
initializing...
initialization done! 
you can now run "cd gfcli && gf run main.go" to start your journey, enjoy!

D:\17.gfcli> tree /f
卷 Data 的文件夹 PATH 列表
卷序列号为 DA91-D877
D:.
.
├── Makefile
├── README.MD
├── api
│   └── hello
│       ├── hello.go
│       └── v1
│           └── hello.go
├── go.mod
├── go.sum
├── hack
│   ├── config.yaml
│   ├── hack-cli.mk
│   └── hack.mk
├── internal
│   ├── cmd
│   │   └── cmd.go
│   ├── consts
│   │   └── consts.go
│   ├── controller
│   │   └── hello
│   │       ├── hello.go
│   │       ├── hello_new.go
│   │       └── hello_v1_hello.go
│   ├── dao
│   ├── logic
│   │   └── logic.go
│   ├── model
│   │   ├── do
│   │   └── entity
│   ├── packed
│   │   └── packed.go
│   └── service
├── main.go
├── manifest
│   ├── config
│   │   └── config.yaml
│   ├── deploy
│   │   └── kustomize
│   │       ├── base
│   │       │   ├── deployment.yaml
│   │       │   ├── kustomization.yaml
│   │       │   └── service.yaml
│   │       └── overlays
│   │           └── develop
│   │               ├── configmap.yaml
│   │               ├── deployment.yaml
│   │               └── kustomization.yaml
│   ├── docker
│   │   ├── Dockerfile
│   │   └── docker.sh
│   ├── i18n
│   └── protobuf
├── resource
│   ├── public
│   │   ├── html
│   │   ├── plugin
│   │   └── resource
│   │       ├── css
│   │       ├── image
│   │       └── js
│   └── template
└── utility

热编译运行项目run

D:\17.gfcli> go install
go: downloading github.com/gogf/gf/v2 v2.5.2

D:\17.gfcli> gf run .\main.go
build: ./main.go
go build -o ./main  ./main.go
./main 
build running pid: 2888
2023-09-28 14:38:59.408 [INFO] pid[2888]: http server started listening on [:8000]
2023-09-28 14:38:59.408 [INFO] {28b3d259bafc8817d1f342577e009f0f} swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2023-09-28 14:38:59.409 [INFO] {28b3d259bafc8817d1f342577e009f0f} openapi specification is serving at address: http://127.0.0.1:8000/api.json

  ADDRESS | METHOD |   ROUTE    |                             HANDLER                             |           MIDDLEWARE             
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /*         | github.com/gogf/gf/v2/net/ghttp.internalMiddlewareServerTracing | GLOBAL MIDDLEWARE                
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /api.json  | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec           |                                  
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | GET    | /hello     | gfcli/internal/controller/hello.(*ControllerV1).Hello           | ghttp.MiddlewareHandlerResponse  
----------|--------|------------|-----------------------------------------------------------------|----------------------------------
  :8000   | ALL    | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI             | HOOK_BEFORE_SERVE                
----------|--------|------------|-----------------------------------------------------------------|----------------------------------

交叉编译build

D:\17.gfcli> gf build -h
USAGE
    gf build ARGUMENT [OPTION]

ARGUMENT
    FILE    building file path

OPTION
    -n, --name             output binary name
    -v, --version          output binary version
    -a, --arch             output binary architecture, multiple arch separated with ','
    -s, --system           output binary system, multiple os separated with ','
    -o, --output           output binary path, used when building single binary file
    -p, --path             output binary directory path, default is './temp'
    -e, --extra            extra custom "go build" options
    -m, --mod              like "-mod" option of "go build", use "-m none" to disable go module
    -c, --cgo              enable or disable cgo feature, it's disabled in default
    -r, --varMap           custom built embedded variable into binary
    -ps, --packSrc         pack one or more folders into one go file before building
    -pd, --packDst         temporary go file path for pack, this go file will be automatically removed after built
    -ew, --exitWhenError   exit building when any error occurs, specially for multiple arch and system buildings.
                           default is false
    -de, --dumpEnv         dump current go build environment before building binary
    -h, --help             more information about this command

EXAMPLE
    gf build main.go
    gf build main.go --ps public,template
    gf build main.go --cgo
    gf build main.go -m none
    gf build main.go -n my-app -a all -s all
    gf build main.go -n my-app -a amd64,386 -s linux -p .
    gf build main.go -n my-app -v 1.0 -a amd64,386 -s linux,windows,darwin -p ./docker/bin

DESCRIPTION
    The "build" command is most commonly used command, which is designed as a powerful wrapper for
    "go build" command for convenience cross-compiling usage.
    It provides much more features for building binary:
    1. Cross-Compiling for many platforms and architectures.
    2. Configuration file support for compiling.
    3. Build-In Variables.

PLATFORMS
    darwin    amd64,arm64
    freebsd   386,amd64,arm
    linux     386,amd64,arm,arm64,ppc64,ppc64le,mips,mipsle,mips64,mips64le
    netbsd    386,amd64,arm
    openbsd   386,amd64,arm
    windows   386,amd64

D:\17.gfcli> gf build main.go -n my-app -a amd64,386 -s linux,windows
2023-09-28 14:40:47.759 start building...
2023-09-28 14:40:47.760 go build -o ./temp/windows_386/my-app.exe main.go
2023-09-28 14:40:55.985 go build -o ./temp/windows_amd64/my-app.exe main.go
2023-09-28 14:41:04.049 go build -o ./temp/linux_386/my-app main.go
2023-09-28 14:41:11.949 go build -o ./temp/linux_amd64/my-app main.go
2023-09-28 14:41:20.398 done!

D:\17.gfcli> tree /f .\bin
卷 Data 的文件夹 PATH 列表
卷序列号为 DA91-D877
D:\17.GFCLI\temp
├── linux_386
│   └── my-app
├── linux_amd64
│   └── my-app
├── windows_386
│   └── my-app.exe
└── windows_amd64
    └── my-app.exe