## 1 文档

    https://www.iris-go.com/docs/#/?id=request-logging

## 2 热重启

```
# 安装rizla包
go get -u github.com/kataras/rizla
# 热重启方式启动iris项目
rizla main.go
```

## 3 配置

```
# 安装viper包
go get -u github.com/spf13/viper

```

## 4 日志

```
# 安装logrus包
go get -u github.com/sirupsen/logrus
```

## 5 GORM

https://github.com/go-gorm/gen

```
go get -u github.com/jinzhu/gorm
go get -u github.com/jinzhu/gorm/dialects/mysql@v1.9.16
```

## 6 Prisma

```
go get github.com/prisma/prisma-client-go
```

* sync the database with your schema

```
go run github.com/prisma/prisma-client-go migrate dev
```

* re-generate the client

```
go run github.com/prisma/prisma-client-go generate
```

* 图形数据库

```
go run github.com/prisma/prisma-client-go studio
```

参考：
https://github.com/mattn/go-prisma-example/blob/main/main.go

## 7 Swagger

### 7.1 安装swgger

```
配置代理
go env -w GOPROXY=https://goproxy.cn,direct
 
go get -u github.com/swaggo/swag/cmd/swag
 
go get github.com/iris-contrib/swagger/v12@v12.2.0-alpha
 
swag -version
```

### 7.2 初始化

在main.go的同级目录执行

```
swag init
```

### 7.3 在main.go中配置注释

```
_ "iris-demo/docs"

"github.com/iris-contrib/swagger/v12"
"github.com/iris-contrib/swagger/v12/swaggerFiles"

// swagger middleware for Iris
// swagger embed files

// @title GAdmin Golang API
// @version 1.0

// @host localhost:8199
// @BasePath /
```

### 7.4 在路由中加入下列代码

app.Get("/swagger/*any", swagger.WrapHandler(swaggerFiles.Handler))

### 7.5 使用官方注释配置接口

官方注释文档:
https://swaggo.github.io/swaggo.io/declarative_comments_format/general_api_info.html

### 7.6 重新生成swagger文档

每当接口注释写完后，都需要执行命令

```
swag init -g main.go
```

### 7.7 重新生成swagger文档

访问：
http://localhost:8199/swagger/index.html

注意：只能使用localhost访问，不能使用127.0.0.1，否则会出现跨域问题（TypeError: Failed to fetch）。

## 8 基于Prisma生成API

```
cd bin/prisma-parse
npm install
node index

```

PS: art-template语法(http://aui.github.io/art-template/zh-cn/docs/syntax.html)

## TODO

### swag不支持form的paramType

影响：Add及Update方法的swagger

```
form is not supported paramType for https://github.com/swaggo/swag/issues/422
```

## 9 Casbin

```
CREATE TABLE `casbin_rule` (
  `p_type` varchar(100) DEFAULT NULL,
  `v0` varchar(100) DEFAULT NULL,
  `v1` varchar(100) DEFAULT NULL,
  `v2` varchar(100) DEFAULT NULL,
  `v3` varchar(100) DEFAULT NULL,
  `v4` varchar(100) DEFAULT NULL,
  `v5` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
```

### 9.1 安装

```
go get -u github.com/casbin/gorm-adapter
go get -u github.com/go-sql-driver/mysql
go get -u github.com/casbin/casbin/v2@latest
```

### 9.2 使用

#### 9.2.1 增加用户角色

```golang
casbin.AddGroupingPolicy(userName, "admin")
+--------+--------+-------+------+------+------+------+
| p_type | v0     | v1    | v2   | v3   | v4   | v5   |
+--------+--------+-------+------+------+------+------+
| g      | aclice | admin |      |      |      |      |
+--------+--------+-------+------+------+------+------+
```

#### 9.2.2 删除用户角色

```
casbin.RemoveGroupingPolicy(userName, role)
```

### 9.2.3 角色授权Policy

```golang
casbin.AddPolicy(role, path, method)
+--------+--------+-------------+------+------+------+------+
| p_type | v0     | v1          | v2   | v3   | v4   | v5   |
+--------+--------+-------------+------+------+------+------+
| g      | aclice | admin       |      |      |      |      |
| p      | admin  | /admin/post | GET  |      |      |      |
+--------+--------+-------------+------+------+------+------+
```

### 9.2.4 删除角色授权Policy

```
casbin.RemovePolicy(role, path, method)
```

### 9.2.5 查询用户有权限

```
ok := casbin.Enforce(userName, path, method)
if !ok {
    logger.Error(fmt.Sprintf("PermissionError: %v %v %v", userName, path, method))
    response.JsonExit(ctx, msg.PermissionError, "权限错误")
    ctx.StopExecution()
}
```

## 10 开发步骤

### 10.1 生成prisma ORM

```
go run github.com/prisma/prisma-client-go migrate dev
go run github.com/prisma/prisma-client-go generate
```

### 10.2 生成API

```
cd bin/prisma-parse
npm install
node index
```

### 10.3 生成swagger

```
swag init -g main.go
```
