Skip to content

Commit

Permalink
update for [email protected]
Browse files Browse the repository at this point in the history
  • Loading branch information
dapeng committed Nov 14, 2024
1 parent 6ebc29e commit 6efcc52
Show file tree
Hide file tree
Showing 10 changed files with 1,836 additions and 2,047 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"gonectr",
"mockgen"
]
}
9 changes: 3 additions & 6 deletions docs/quick-start/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ In this document, I will demonstrate how to create a production-ready web projec

## Install the gone Tool
```bash
go install github.com/gone-io/gone/tools/gone@latest
go install github.com/gone-io/gonectr@latest
go install go.uber.org/mock/mockgen@latest
```
For more information about the gone command, refer to: [Gone Tool](https://goner.fun/references/gone-tool.html)

## Create a Project Using the gone Command

```bash
gone create -t web+mysql web-mysql-docker
gonectr create -t web+mysql web-mysql-docker
```
The above command will create a directory named web-mysql-docker in the current directory.

Expand All @@ -29,10 +30,6 @@ Execute the following commands:
# Enter the project directory
cd web-mysql-docker

# Generate the Priest functions: https://goner.fun/guide/auto-gen-priest.html
make gone


# Start the MySQL service
docker compose up -d mysql

Expand Down
227 changes: 18 additions & 209 deletions docs/quick-start/simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,224 +6,33 @@ next: ./mysql

# Creating a Simple Web Project

## Installing the Gone Utility Tool
## Installing [gonectr](https://github.com/gone-io/gonectr) and [mockgen](https://github.com/uber-go/mock)
```bash
go install github.com/gone-io/gone/tools/gone@latest
go install github.com/gone-io/gonectr@latest
go install go.uber.org/mock/mockgen@latest
```

> After installation, you can use the gone command:
> ```bash
> gone -h
> ```
>
> ```bash
> ➜ demo gone -h
> NAME:
> gone - A new cli application
>
> USAGE:
> gone [global options] command [command options] [arguments...]
>
> DESCRIPTION:
> generate gone code or generate gone app
>
> COMMANDS:
> priest -s ${scanPackageDir} -p ${pkgName} -f ${funcName} -o ${outputFilePath} [-w]
> mock -f ${fromGoFile} -o ${outGoFile}
> create [-t ${template} [-m ${modName}]] ${appName}
> help, h Shows a list of commands or help for one command
>
> GLOBAL OPTIONS:
> --help, -h show help (default: false)
> ```
> Supported functionalities:
> 1. create: Create a Gone app, currently only supports creating web apps.
> 2. priest: Automatically generate the **Priest** function for the project. [Learn More](../)
> 3. Generating mock code for testing.

## Creating a Web Project and Running the Code

## Creating a Web Project
```bash
# Create a project named web-app
gone create web-app
cd web-app
make run
gonectr create myproject
```

## Project Structure
## Running the Project
```bash
├── Dockerfile
├── Makefile
├── README.md
├── cmd
│   └── server
│   └── main.go // Startup file, containing the main function.
├── config // Project configuration directory, supporting `.properties` files.
│   ├── default.properties
│   ├── dev.properties
│   ├── local.properties
│   └── prod.properties
├── docker-compose.yaml
├── go.mod
├── internal
│   ├── controller // Controller directory, for defining routes in files.
│   │   └── demo_ctr.go
│   ├── interface
│   │   ├── domain // Directory for domain objects.
│   │   │   ├── demo.go
│   │   │   └── user.go
│   │   └── service // Directory for service interface definitions.
│   │   └── i_demo.go
│   ├── master.go
│   ├── middleware // Middleware directory, for defining web middleware.
│   │   ├── authorize.go
│   │   └── pub.go
│   ├── module // Module directory, each subdirectory implements a module's functionality, typically defined in internal/interface/service/.
│   │   └── demo
│   │   ├── demo_svc.go
│   │   └── error.go
│   ├── pkg
│   │   └── utils
│   │   └── error.go
│   └── router // Define routers in this directory.
│   ├── auth_router.go
│   └── pub_router.go
└── tests
└── api
└── demo.http
```
## Router
In the directory `internal/router`, two `gin.IRouter`s are implemented:
- pubRouter: Public router, endpoints mounted under this router can be accessed without authorization.
- authRouter: Authenticated router, endpoints mounted under this router require authorization.
Let's analyze the code in `internal/router/pub_router.go`:
```go
package router
import (
"web-app/internal/middleware"
"github.com/gone-io/gone"
)
const IdRouterPub = "router-pub"
//go:gone
func NewPubRouter() (gone.Goner, gone.GonerId) {
return &pubRouter{}, IdRouterPub
}
type pubRouter struct {
gone.Flag
gone.IRouter
root gone.IRouter `gone:"gone-gin-router"`
pub *middleware.PubMiddleware `gone:"*"`
}
func (r *pubRouter) AfterRevive() gone.AfterReviveError {
r.IRouter = r.root.Group("/api", r.pub.Next)
return nil
}
```
1. For routers, they need to implement the methods defined in `gone.IRouter`.
2. The struct `pubRouter` embeds an `gone.IRouter`, which means it directly implements the `gone.IRouter` interface. It is only assigned a value in `AfterRevive()`.
3. `r.IRouter = r.root.Group("/api", r.pub.Next)` means the current router is a subrouter under the root router at `/api`, with an additional middleware `r.pub.Next`.
4. `root gone.IRouter` is a framework-level Goner provided by the `github.com/gone-io/gone/goner/gin` package, with a named injection `gone-gin-router`.
## Controller
Below is the definition of the Controller interface:
```go
// Controller interface, implemented by business code to mount and handle routes
// For usage, refer to [Sample Code](https://gitlab.openviewtech.com/gone/gone-example/-/tree/master/gone-app)
type Controller interface {
// Mount Route mounting interface, this function will be called before service startup, and the implementation of this function should usually return `nil`
Mount() MountError
}
```
To write HTTP interfaces, we need to implement the `Controller` interface and mount the interface routes in the `Mount` method, as shown in the code in `internal/controller/demo_ctr.go`:
```go
package controller
import (
"web-app/internal/interface/service"
"web-app/internal/pkg/utils"
"github.com/gone-io/gone"
)
//go:gone
func NewDemoController() gone.Goner {
return &demoController{}
}
type demoController struct {
gone.Flag
demoSvc service.IDemo `gone:"*"` // Dependency injection of service
authRouter gone.IRouter `gone:"router-auth"` // Router injection
pubRouter gone.IRouter `gone:"router-pub"` // Router injection
}
func (ctr *demoController) Mount() gone.GinMountError {
ctr.
authRouter.
Group("/demo").
GET("/show", ctr.showDemo)
ctr.
pubRouter.
Group("/demo2").
GET("/show", ctr.showDemo).
GET("/error", ctr.error).
GET("/echo", ctr.echo)
return nil
}
func (ctr *demoController) showDemo(ctx *gone.Context) (any, error) {
return ctr.demoSvc.Show()
}
func (ctr *demoController) error(ctx *gone.Context) (any, error) {
return ctr.demoSvc.Error()
}
func (ctr *demoController) echo(ctx *gone.Context) (any, error) {
type Req struct {
Echo string `form:"echo"`
}
var req Req
if err := ctx.Bind(&req); err != nil {
return nil, gone.NewParameterError(err.Error(), utils.ParameterParseError)
}
return ctr.demoSvc.Echo(req.Echo)
}
cd myproject
gonectr run ./cmd/server
```

## Service
As per the standard, we require service interfaces to be defined in the `internal/interface/service` directory. File names should begin with `i_`, and interface types should start with `I`, for example:
File: i_demo.go
```go
package service
import "web-app/internal/interface/domain"
type IDemo interface {
Show() (*domain.DemoEntity, error)
Echo(input string) (string, error)
Error() (any, error)
}
Or use run Make command if you have installed [make](https://www.gnu.org/software/make/):
```bash
cd myproject
make run
```
The logic implementation of services is placed in the `internal/module` directory, organized into modules.
## [Use Database](https://goner.fun/guide/xorm.html)
Or with docker compose:
```bash
cd myproject
docker compose build
docker compose up
```
57 changes: 37 additions & 20 deletions docs/references/gone-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,51 @@ next: ./http-inject
## Installation
Run the following command:
```bash
go install github.com/gone-io/gone/tools/gone@latest
go install github.com/gone-io/gonectr@latest
go install go.uber.org/mock/mockgen@latest
```

## Usage

### Testing Installation
After successful installation, run `gone -h` to verify the installation. You should see the following output:
```bash
➜ demo gone -h
NAME:
gone - A new cli application

USAGE:
gone [global options] command [command options] [arguments...]

DESCRIPTION:
generate gone code or generate gone app

COMMANDS:
priest -s ${scanPackageDir} -p ${pkgName} -f ${funcName} -o ${outputFilePath} [-w]
mock -f ${fromGoFile} -o ${outGoFile}
create [-t ${template} [-m ${modName}]] ${appName}
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help (default: false)
➜ gonectr -h
gonectr is a command-line tool designed for generating Gone projects
and serving as a utility for Gone projects, such as code generation,
compilation, and running Gone projects.

Usage:
gonectr [flags]
gonectr [command]

Available Commands:
build build gone project
completion Generate the autocompletion script for the specified shell
create Create a new Gone Project
generate generate gone loading code and import code
help Help about any command
mock generate mock goner code for interface
priest generate priest function
run run gone project

Flags:
-h, --help help for gonectr
-v, --version Show version

Use "gonectr [command] --help" for more information about a command.
```

### Generate Project
```bash
➜ gonectr create -h
Create a new Gone Project

Usage:
gonectr create [flags]

### Generate Priest Function
Flags:
-h, --help help for create
-m, --module-name string module name
-t, --template-name string support template names: web, web+mysql (default "web")
```
9 changes: 5 additions & 4 deletions docs/zh/guide/x-1-gen-priest.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ next: ./x-2-error
[什么是Priest?](./core-concept.md)

## 小试牛刀
`gone priest`命令可以扫描代码中的特殊注释`//go:gone`生成Priest函数;`//go:gone`暂时只能用于标记`func () gone.Goner``func () (gone.Goner, gone.GonerId)`这两种形式的函数,函数必须是公开(以大写字母开头)。
`gonectr priest`命令可以扫描代码中的特殊注释`//go:gone`生成Priest函数;`//go:gone`暂时只能用于标记`func () gone.Goner``func () (gone.Goner, gone.GonerId)`这两种形式的函数,函数必须是公开(以大写字母开头)。

### 1. 安装gone辅助工具
执行如下命令:
```bash
go install github.com/gone-io/gone/tools/gone@latest
go install github.com/gone-io/gonectr@latest
go install go.uber.org/mock/mockgen@latest
```
gone更多内容参考文档 [gone辅助工具](../references/gone-tool.md)
更多内容参考文档 [gone辅助工具](../references/gone-tool.md)

### 2. 编写Goner
创建项目,创建文件:
Expand Down Expand Up @@ -46,7 +47,7 @@ type Demo struct {
### 3. 生成代码
执行
```bash
gone priest -s ./ -f Priest -o priest.go -p demo
gonectr priest -s ./ -f Priest -o priest.go -p demo
```
将生成文件:priest.go
内容如下:
Expand Down
Loading

0 comments on commit 6efcc52

Please sign in to comment.