# 开发个人Ollama-Chat--4 用户管理
先看下我的目录结构,可以根据个人爱好,进行重构
```bash
|-- Dockerfile
|-- LICENSE
|-- common
| |-- callmodel
| | |-- gemma.go
| | `-- models.go
| |-- consts
| | |-- code.go
| | |-- common.go
| | |-- config.go
| | `-- consts.go
| |-- cryptx
| | `-- crypt.go
| |-- curlhttp
| | `-- curl.go
| |-- database
| | |-- common.go
| | |-- connect.go
| | |-- dao.go
| | |-- ormLogx.go
| | |-- redisClient.go
| | `-- redisDao.go
| |-- go.mod
| |-- go.sum
| |-- jwtx
| | `-- jwt.go
| |-- middleware
| | `-- static.go
| |-- model
| | |-- chat.sql
| | |-- chatmodel.go
| | |-- chatmodel_client.go
| | |-- prompt.sql
| | |-- promptmodel.go
| | |-- promptmodel_client.go
| | |-- readMe.md
| | |-- user.sql
| | |-- usermodel.go
| | `-- usermodel_client.go
| `-- utils
| `-- utils.go
|-- docker-compose.yaml
|-- nginx
| `-- conf.d
| `-- default.conf
|-- readme.md
`-- service
|-- chat
| |-- api
| `-- rpc
`-- user
|-- api
`-- rpc
```
## 4.1 生成 user model 模型
- 创建 sql 文件 `OpenUI 前端`需要的字段,不可缺少。`mysql`服务需提前创建 `openui`库, `user` table
```sql
CREATE TABLE `user` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '用户姓名',
`email` varchar(255) NOT NULL DEFAULT '' COMMENT '用户电话',
`password` varchar(255) NOT NULL DEFAULT '' COMMENT '用户密码',
`role` varchar(264) NOT NULL DEFAULT '' COMMENT '用户角色',
`profile_image_url` varchar(255) NOT NULL DEFAULT '' COMMENT '用户头像',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
- 运行模板生成命令 `model`文件放置在通用目录,和`go-zero`官方案例不同
```bash
goctl model mysql ddl -src ./model/user.sql -dir ./model -c
```
##
## 4.2 生成 user api 服务
`前缀,路由,传参,响应不可变,否则 openui调用失败`
- 创建 `user.api `文件
```bash
type (
// 用户登录
LoginRequest {
Email string `json:"email"`
Password string `json:"password"`
}
LoginResponse {
Id int64 `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
ProfileImageUrl string `json:"profile_image_url"`
Token string `json:"token"`
}
// 用户登录
// 用户注册
RegisterRequest {
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
ProfileImageUrl string `json:"profile_image_url"`
}
RegisterResponse {
Id int64 `json:"id"`
Name string `json:"name"`
Token string `json:"token"`
}
// 用户注册
// 用户信息
UserInfoResponse {
Id int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Role string `json:"role"`
ProfileImageUrl string `json:"
profile_image_url"`
}
// 用户信息
)
@server (
prefix: /api/v1
)
service User {
@handler Login
post /auths/signin (LoginRequest) returns (LoginResponse)
@handler Register
post /auths/signup (RegisterRequest) returns (RegisterResponse)
}
@server (
jwt: Auth
prefix: /api/v1
)
service User {
@handler UserInfo
get /auths returns (UserInfoResponse)
}
```
- 运行模板生成命令
```bash
goctl api go -api ./api/user.api -dir ./api
```
## 4.3 生成 user rpc 服务
- 创建 `user.proto`文件
```protobuf
syntax = "proto3";
package userclient;
option go_package = "./user";
// 用户登录
message LoginRequest {
string Email = 1;
string Password = 2;
}
message LoginResponse {
int64 Id = 1;
string Name = 2;
string Token = 3;
string Role = 4;
string ProfileImageUrl = 5;
}
// 用户登录
// 用户注册
message RegisterRequest {
string Name = 1;
string Email = 2;
string Password = 3;
string ProfileImageUrl = 4;
}
message RegisterResponse {
int64 Id = 1;
string Name = 2;
string Token = 3;
}
// 用户注册
// 用户信息
message UserInfoRequest {
int64 Id = 1;
}
message UserInfoResponse {
int64 Id = 1;
string Name = 2;
string Email = 3;
string Role = 4;
string ProfileImageUrl = 5;
}
// 用户信息
service User {
rpc Login(LoginRequest) returns(LoginResponse);
rpc Register(RegisterRequest) returns(RegisterResponse);
rpc UserInfo(UserInfoRequest) returns(UserInfoResponse);
}
```
- 运行模板生成命令
```bash
goctl rpc protoc ./rpc/user.proto --go_out=./rpc/types --go-grpc_out=./rpc/types --zrpc_out=./rpc
```
## 4.3 配置文件
- rpc/etc
```yaml
Name: user.rpc
ListenOn: 0.0.0.0:9001
Etcd:
Hosts:
- xxxxxxxxxxxxxxxxxxxxxxxxx:2379
Key: user.rpc
Timeout: 0
Mysql:
Host: xxxxxxxxxxxxxx
Port: 3306
DbName: openui
User: xxxxxx
Password: "xxxxxxxxxxxxxxxxxxxxxxxxx"
DBZone: "TS"
Charset: utf8mb4
MaxIdle: 10
MaxOpen: 100
LogMode: true
Loc: Asia/Shanghai
Debug: true
TablePrefix: "v1_"
MaxLifetime: 300
# redis 支持选择db, 不使用go-zero官方库
CacheRedis:
Name: "openui"
Nettype: "tcp"
Address: "redis:6379"
Auth: ""
DB: 0
Salt: ********************
#日志配置
LogConf:
ServiceName: user.rpc
Mode: file
TimeFormat: 2006-01-02 15:04:05.000
Path: logs
Level: info
Compress: true
Stat: false # 不记录CPU、内存等信息
KeepDays: 10
MaxBackups: 2
```
- api/etc
```yaml
Name: user.rpc
ListenOn: 0.0.0.0:9001
Etcd:
Hosts:
- xxxxxxxxxxxxxxxxxxxxxxxxx:2379
Key: user.rpc
Timeout: 0
Mysql:
Host: xxxxxxxxxxxxxx
Port: 3306
DbName: openui
User: xxxxxx
Password: "xxxxxxxxxxxxxxxxxxxxxxxxx"
DBZone: "TS"
Charset: utf8mb4
MaxIdle: 10
MaxOpen: 100
LogMode: true
Loc: Asia/Shanghai
Debug: true
TablePrefix: "v1_"
MaxLifetime: 300
# redis 支持选择db, 不使用go-zero官方库
CacheRedis:
Name: "openui"
Nettype: "tcp"
Address: "redis:6379"
Auth: ""
DB: 0
Salt: *****************
#日志配置
LogConf:
ServiceName: user.rpc
Mode: file
TimeFormat: 2006-01-02 15:04:05.000
Path: logs
Level: info
Compress: true
Stat: false # 不记录CPU、内存等信息
KeepDays: 10
MaxBackups: 2
```
## 4.4 业务处理
- 业务处理就不过多描述了,具体处理流程可以看相应文件的实现
## 项目地址
[jackwillsmith/openui-svelte-build (github.com)](https://github.com/jackwillsmith/openui-svelte-build/)
[GitHub - jackwillsmith/openui-backend-go: openui-backend-go](https://github.com/jackwillsmith/openui-backend-go.git)
有疑问加站长微信联系(非本文作者))