diff --git a/README.md b/README.md index 4ce011b0..e17d5560 100644 --- a/README.md +++ b/README.md @@ -6,61 +6,85 @@ ```bash git clone https://github.com/decert-me/backend-go.git ``` +## 运行环境 + +```shell +- Golang >= v1.19 && < v1.21 +- Redis +- PostgreSQL +- Docker +``` + ## 环境配置 1、安装 Docker 脚本一键安装: `sudo curl -sSL https://get.daocloud.io/docker | sh` 详细步骤参照: https://docs.docker.com/install/ ## 编译 + ```bash # 主程序 go build -o bin/app/decert-app internal/app/cmd/main.go -# 定时处理程序 +# 定时任务程序 go build -o bin/job/decert-job internal/job/cmd/main.go +# 授权登录程序 +go build -o bin/auth/decert-auth internal/auth/cmd/main.go + # 判题程序 go build -o bin/judge/decert-judge internal/judge/cmd/main.go ``` + ## 配置 + +查看各个项目配置说明: +- [主程序配置说明](./internal/app/README.md) +- [定时任务程序配置说明](./internal/job/README.md) +- [授权登录程序配置说明](./internal/auth/README.md) +- [判题程序配置说明](./internal/judge/README.md) + + ```bash # 主程序配置 cp ./internal/app/cmd/config.demo.yaml ./bin/app/config.yaml vi ./bin/app/config.yaml -cp ./internal/app/cmd/locale.json ./bin/app/locale.json -# 定时处理程序配置 +# 定时任务程序配置 cp ./internal/job/cmd/config.demo.yaml ./bin/job/config.yaml vi ./bin/job/config.yaml +# 授权登录程序 +cp ./internal/auth/cmd/config.demo.yaml ./bin/auth/config.yaml +vi ./bin/auth/config.yaml + # 判题程序配置 cp ./internal/judge/cmd/config.demo.yaml ./bin/judge/config.yaml vi ./bin/judge/config.yaml ``` -## 运行判题 Docker + +## Docker 构建判题镜像 + ```shell sudo docker build -t judge:1.0 -f internal/judge/Dockerfile . ``` + ## 运行 + ```bash # 主程序 cd bin/app ./decert-app -# 定时处理程序 +# 定时任务程序 cd bin/job ./decert-job +# 授权登录程序 +cd bin/auth +./decert-auth + # 判题程序 cd bin/judge ./decert-judge -``` - - - -## 测试 -```bash -go test ./internal/app/service -go test ./internal/job/service -go test ./pkg/... ``` \ No newline at end of file diff --git a/internal/app/README.md b/internal/app/README.md index da1b0417..5e109d8f 100644 --- a/internal/app/README.md +++ b/internal/app/README.md @@ -1,9 +1,11 @@ # App +后端程序 + ## 安装 ```bash -git clone https://github.com/decert-me/backend-go.git```] +git clone https://github.com/decert-me/backend-go.git ``` ## 编译 @@ -24,7 +26,7 @@ vi ./bin/app/config.yaml 配置项: -``` +```yaml # system configuration system: env: develop @@ -34,8 +36,11 @@ system: ``` env:运行环境,可选值为 develop、test、production + addr:运行端口 + i18n:国际化配置文件 + website:前端网站index.html文件路径,用于代理meta标签 ### 数据库配置 @@ -60,17 +65,29 @@ pgsql: ``` path:数据库地址 + port:数据库端口 + config:数据库配置 + db-name:数据库名称 + username:数据库用户名 + password:数据库密码 + auto-migrate:是否自动迁移数据库 + prefix:数据库表前缀 + slow-threshold:慢查询阈值,单位毫秒 + max-idle-conns:最大空闲连接数 + max-open-conns:最大连接数 + log-mode:日志级别 + log-zap:是否使用zap日志库 @@ -87,8 +104,11 @@ redis: ``` db:Redis数据库 + addr:Redis地址和端口 + password:密码 + prefix:前缀 ### 日志级别配置 @@ -109,13 +129,21 @@ log: ``` level:日志级别 debug、info、warn、error、dpanic、panic、fatal + save:是否保存日志 + format:日志格式 + log-in-console:是否在控制台输出日志 + prefix:日志前缀 + director:日志保存路径 + show-line:是否显示行号 + encode-level:日志编码级别 + stacktrace-key:堆栈信息 ### JWT 配置 @@ -131,7 +159,9 @@ auth: ``` signing-key:签名密钥 + expires-time:过期时间,单位秒 + issuer:签发人 ### 文件上传配置 @@ -146,6 +176,7 @@ local: ``` path:本地文件保存路径 + ipfs:IPFS文件保存路径 ### 区块链配置 @@ -162,8 +193,11 @@ blockchain: ``` ens-rpc:ENS查找RPC地址 + sign-private-key:签名私钥 + signature:签名内容 + signature-exp:签名过期时间,单位秒 ### 合约配置 @@ -179,7 +213,9 @@ contract: ``` badge:badge合约地址 + quest:quest合约地址 + quest-minter:quest-minter合约地址 ### 挑战信息配置 @@ -206,6 +242,7 @@ ipfs: ``` api:IPFS节点地址 + upload-api:IPFS上传API地址 ### 代码运行配置 @@ -220,8 +257,9 @@ judge: weight: 10 ``` -url:代码运行API地址 -weight:权重 +url:[judge服务](../judge) 运行API地址 + +weight:权重,数字越大权重越高 ### 空投配置 @@ -233,10 +271,11 @@ share: callback: "http://192.168.1.10:8105" # 回调接口 ``` -verify-key:校验key +verify-key:校验key,空投服务加密通讯,需与空投服务配置保持一致 + callback:回调接口 -### discord 消息通知配置 +### discord 消息通知配置(接收空投结果通知) 配置项: @@ -250,9 +289,12 @@ discord: ``` active:是否启用 -token:机器人token -success-channel:成功通知频道ID -failed-channel:失败通知频道ID + +token:Discord 机器人token + +success-channel:空投成功通知 Discord 频道ID + +failed-channel:空投失败通知 Discord 频道ID ### sentry 配置 @@ -266,8 +308,10 @@ sentry: enable-tracing: true # Enable performance tracing. ``` -dsn:sentry dsn +dsn:sentry dsn 配置为空不启用 + traces-sample-rate:采样率 + enable-tracing:是否启用性能追踪 ### 社交账号绑定配置 @@ -285,7 +329,9 @@ social: ``` wechat:微信配置 + discord:discord配置 api-key:API密钥,与Auth配置一致 + call-url:回调地址,Auth接口地址 \ No newline at end of file diff --git a/internal/auth/README.md b/internal/auth/README.md index c2628fa0..e091a9d6 100644 --- a/internal/auth/README.md +++ b/internal/auth/README.md @@ -1,5 +1,7 @@ # Auth +授权登录模块:用户绑定Github、微信、Discord + ## 编译 ```bash @@ -37,6 +39,7 @@ project: callback-url: "http://127.0.0.1:8087/api" ``` 1、api-key 需与decert app 模块 social configuration 配置项保持一致 + 2、callback-url 填写 decert app 模块接口 URL @@ -61,8 +64,11 @@ auth: ![](https://ipfs.io/ipfs/bafybeihmyjqidtjtjt5yulxuxclfzvaucgg736fkana4wqsrntrl4w47pq/Snipaste_2023-12-19_16-43-37.png) 服务器配置: + 1、部署 Auth 程序,程序的 Token 和 EncodingAESKey 与微信公众号配置保持一致 + 2、到微信公众号服务器配置中,配置 URL、Token 和 EncodingAESKey + (https://ipfs.io/ipfs/QmQFgHGLDBUqR8ezUpxCutKwe6mvZF7EA2G3Ln8MmLhcaX/Snipaste_2023-12-19_16-39-51.png) ### Discord 配置 @@ -79,8 +85,32 @@ auth: ``` 1、进入 [DEVELOPER PORTAL](https://discord.com/developers/applications) + 2、选择用户需要授权绑定的 Applications,未创建可以点击 New Application + 3、进入 Applications -> Oauth2 页面复制 CLIENT ID 和 CLIENT SECRET 写入配置文件 + 4、Redirects 添加 decert 回调链接 https://decert.me/callback/discord -![](https://ipfs.io/ipfs/bafybeihw43kchnyicpn2m7y4o2eb6ma4sgkksaymcx6osm7igcscc35wpe/Snipaste_2023-12-19_17-14-08.png) \ No newline at end of file +![](https://ipfs.io/ipfs/bafybeihw43kchnyicpn2m7y4o2eb6ma4sgkksaymcx6osm7igcscc35wpe/Snipaste_2023-12-19_17-14-08.png) + +### Github 配置 + +Github 用户绑定配置 + +配置项: +```yaml +# auth configuration +auth: + github: + client-id: "" + client-secret: "" +``` + +1、进入 [Github OAuth Apps](https://github.com/settings/developers) + +2、选择用户需要授权绑定的 Apps,未创建可以点击 New OAuth App + +3、进入 Apps 页面复制 CLIENT ID 和 CLIENT SECRET 写入配置文件 + +4、Authorization callback URL 添加 decert 回调链接 https://decert.me/ \ No newline at end of file diff --git a/internal/job/README.md b/internal/job/README.md new file mode 100644 index 00000000..e23ee582 --- /dev/null +++ b/internal/job/README.md @@ -0,0 +1,180 @@ +# Job + +定时任务模块:解析交易 + +## 编译 + +```bash +go build -o bin/job/decert-job internal/job/cmd/main.go +``` + +## 运行环境配置 + +```bash +cp ./internal/job/cmd/config.demo.yaml ./bin/job/config.yaml +vi ./bin/job/config.yaml +``` + +### 数据库配置 + +配置项: +```yaml +# pgsql configuration +pgsql: + path: "127.0.0.1" + port: "5432" + config: "" + db-name: "" + username: "postgres" + password: "123456" + auto-migrate: true + prefix: "" + slow-threshold: 200 + max-idle-conns: 10 + max-open-conns: 100 + log-mode: "info" + log-zap: false +``` + +path:数据库地址 + +port:数据库端口 + +config:数据库配置 + +db-name:数据库名称 + +username:数据库用户名 + +password:数据库密码 + +auto-migrate:是否自动迁移数据库 + +prefix:数据库表前缀 + +slow-threshold:慢查询阈值,单位毫秒 + +max-idle-conns:最大空闲连接数 + +max-open-conns:最大连接数 + +log-mode:日志级别 + +log-zap:是否使用zap日志库 + +### 日志级别配置 + +配置项: +```yaml +# log configuration +log: + level: info + save: true + format: console + log-in-console: true + prefix: '[backend-go]' + director: log + show-line: true + encode-level: LowercaseColorLevelEncoder + stacktrace-key: stacktrace +``` + +level:日志级别 debug、info、warn、error、dpanic、panic、fatal + +save:是否保存日志 + +format:日志格式 + +log-in-console:是否在控制台输出日志 + +prefix:日志前缀 + +director:日志保存路径 + +show-line:是否显示行号 + +encode-level:日志编码级别 + +stacktrace-key:堆栈信息 + + +### 区块链配置 + +配置项: +```yaml +# blockchain configuration +blockchain: + chain-id: 5 + attempt: 200 + provider: + - url: "https://rpc.ankr.com/eth_goerli" + weight: 5 + - url: "https://rpc.ankr.com/eth_goerli" + weight: 10 +``` + +chain-id: 链ID + +attempt: 尝试次数 + +provider:RPC + +url:RPC地址 + +weight:权重,越大权重越高 + +### 合约配置(V1) + +配置项: +```yaml +contract: + badge: "" + quest: "" + quest-minter: "" +``` + +badge: badge 合约地址 + +quest: quest 合约地址 + +quest-minter: quest minter 合约地址 + +### 合约配置(V2多链) + +配置项: +```yaml +contract-v2: + 137: + badge: "0xaD1789A4cA2640a570a1268c84EA87e09A10A1f5" + quest: "0xd1E50047cEbaD1d826c3F7961A35048B518652F2" + quest-minter: "0xeb4E2EE6f165e7E1B9a4F1c4532C0433a6Ef397B" + provider: + - url: "https://rpc.ankr.com/eth_goerli" + weight: 5 + - url: "https://rpc.ankr.com/eth_goerli" + weight: 10 +``` + +137:链ID + +badge: badge 合约地址 + +quest: quest 合约地址 + +quest-minter: quest minter 合约地址 + +provider:RPC 配置 + +url:RPC地址 +weight:权重,越大权重越高 + +### IPFS 配置 + +配置项: +```yaml +# ipfs configuration +ipfs: + api: "https://ipfs.io/ipfs/" +``` + +api:IPFS 节点地址 \ No newline at end of file diff --git a/internal/job/cmd/config.demo.yaml b/internal/job/cmd/config.demo.yaml index b599acf6..2a68e7cc 100644 --- a/internal/job/cmd/config.demo.yaml +++ b/internal/job/cmd/config.demo.yaml @@ -29,7 +29,6 @@ log: # blockchain configuration blockchain: sign-private-key: "" - airdrop-private-key: "" chain-id: 5 attempt: 200 provider: diff --git a/internal/job/config/blockchain.go b/internal/job/config/blockchain.go index 487377fd..2c02b72c 100644 --- a/internal/job/config/blockchain.go +++ b/internal/job/config/blockchain.go @@ -6,9 +6,7 @@ type Provider struct { } type BlockChain struct { - SignPrivateKey string `mapstructure:"sign-private-key" json:"sign-private-key" yaml:"sign-private-key"` // 签名私钥 - AirdropPrivateKey string `mapstructure:"airdrop-private-key" json:"airdrop-private-key" yaml:"airdrop-private-key"` // Airdrop私钥 - ChainID int64 `mapstructure:"chain-id" json:"chain-id" yaml:"chain-id"` // Chain ID - Attempt int `mapstructure:"attempt" json:"attempt" yaml:"attempt"` // 尝试次数 - Provider []Provider `mapstructure:"provider" json:"provider" yaml:"provider"` // Provider + ChainID int64 `mapstructure:"chain-id" json:"chain-id" yaml:"chain-id"` // Chain ID + Attempt int `mapstructure:"attempt" json:"attempt" yaml:"attempt"` // 尝试次数 + Provider []Provider `mapstructure:"provider" json:"provider" yaml:"provider"` // Provider } diff --git a/internal/judge/README.md b/internal/judge/README.md new file mode 100644 index 00000000..1f6ea471 --- /dev/null +++ b/internal/judge/README.md @@ -0,0 +1,129 @@ +# Judge + +Solidity 代码运行和判题服务 + +## 配置项 + +### 运行端口配置 + +配置项: + +``` +# system configuration +system: + env: develop + addr: 8888 +``` + +env:运行环境,可选值为 develop、public + +addr:运行端口 + +### 日志级别配置 + +配置项: +```yaml +# log configuration +log: + level: info + save: true + format: console + log-in-console: true + prefix: '[decert-judge]' + director: log + show-line: true + encode-level: LowercaseColorLevelEncoder + stacktrace-key: stacktrace +``` + +level:日志级别 debug、info、warn、error、dpanic、panic、fatal + +save:是否保存日志 + +format:日志格式 + +log-in-console:是否在控制台输出日志 + +prefix:日志前缀 + +director:日志保存路径 + +show-line:是否显示行号 + +encode-level:日志编码级别 + +stacktrace-key:堆栈信息 + +### JWT 配置 + +配置项(需要与app程序配置保持一致): + +```yaml +# auth configuration +auth: + signing-key: "Decert" + expires-time: 86400 + issuer: "Decert" +``` + +signing-key:签名密钥 + +expires-time:过期时间,单位秒 + +issuer:签发人 + +### 挑战信息配置 + +配置项: + +```yaml +# quest configuration +quest: + encrypt-key: "eb5a5bb2-ebbd-45cc-9d37-77a9377f2aca" +``` + +encrypt-key:挑战信息加密密钥 + +### Docker 配置 + +配置项: + +```yaml +# docker configuration +docker: + clear-enabled: true + clear-time: 15 +``` + +clear-enabled:是否开启 Docker 定时清空容器 + +clear-time: Docker 定时清空超过闲置时长的容器(分钟) + +### 代码运行配置(未启用) + +配置项: + +```yaml +judge: + sandbox-service: "http://192.168.1.15:5050/" # 沙盒服务URL + work-path: "/Users/mac/Code/resource/" # 判题模块工作目录,临时保存用户代码 + cache-path: "/Users/mac/Code/resource/cache" # 判题模块缓存目录,缓存 solc 等 + javascript-path: "/Users/mac/.nvm/versions/node/v18.16.0/bin/node" + typescript-path: "/usr/local/bin/ts-node" + golang-path: "/usr/local/go/bin/go" + python-path: "/usr/bin/python3" +``` + +sandbox-service:沙盒服务URL,请查看 sandbox 项目 + +work-path:判题模块工作目录,临时保存用户代码 + +cache-path:判题模块缓存目录,缓存 solc 等 + +javascript-path:nodejs 路径 + +typescript-path:ts-node 路径 + +golang-path:golang 路径 + +python-path:python 路径 \ No newline at end of file diff --git a/internal/judge/cmd/config.demo.yaml b/internal/judge/cmd/config.demo.yaml index 242e1968..7a1e0e1f 100644 --- a/internal/judge/cmd/config.demo.yaml +++ b/internal/judge/cmd/config.demo.yaml @@ -3,29 +3,13 @@ system: env: develop addr: 8888 -# pgsql configuration -pgsql: - path: "127.0.0.1" - port: "5432" - config: "" - db-name: "" - username: "postgres" - password: "123456" - auto-migrate: true - prefix: "" - slow-threshold: 200 - max-idle-conns: 10 - max-open-conns: 100 - log-mode: "info" - log-zap: false - # log configuration log: level: info # 日志等级 save: true # 日志本地化存储 format: console # 日志格式 log-in-console: true # 输出到终端 - prefix: '[backend-go]' # 日志前缀 + prefix: '[decert-judge]' # 日志前缀 director: log # 日志目录 show-line: true encode-level: LowercaseColorLevelEncoder