README 调整
This commit is contained in:
parent
1670b28d2d
commit
29ae5b6fd3
|
@ -0,0 +1,172 @@
|
|||
# ohUrlShortener HTTP API
|
||||
|
||||
### `/api` 接口权限说明
|
||||
|
||||
所有 `/api/*` 接口需要通过 `Bearer Token` 方式验证权限,亦即:每个请求 Header 须携带
|
||||
|
||||
```shell
|
||||
Authorization: Bearer {sha256_of_password}
|
||||
```
|
||||
|
||||
`sha256_of_password` 的加密规则,与 `storage/users_storage.go` 中的 `PasswordBase58Hash()` 保持同步
|
||||
|
||||
### 1. 新增短链接 `POST /api/url`
|
||||
|
||||
接受参数:
|
||||
1. `dest_url` 目标链接,必填
|
||||
2. `memo` 备注信息,选填
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request POST \
|
||||
--url http://localhost:9092/api/url \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data dest_url=http://localhost:9092/admin/dashboard \
|
||||
--data memo=dashboard
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": {
|
||||
"short_url": "http://localhost:9091/BUUtpbGp"
|
||||
},
|
||||
"date": "2022-04-10T21:31:29.36559+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 禁用/启用 短链接 `PUT /api/url/:url/change_state`
|
||||
|
||||
接受参数:
|
||||
1. `url` path 参数,指定短链接,必填
|
||||
2. `enable` 禁用时,传入 false;启用时,传入 true
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request PUT \
|
||||
--url http://localhost:9092/api/url/33R5QUtD/change_state \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data enable=false
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": true,
|
||||
"date": "2022-04-10T21:31:25.7744402+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 查询短链接统计数据 `GET /api/url/:url`
|
||||
|
||||
接受参数:
|
||||
1. `url` path 参数,指定短链接,必填
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request GET \
|
||||
--url http://localhost:9092/api/url/33R5QUtD \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded'
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": {
|
||||
"short_url": "33R5QUtD",
|
||||
"today_count": 3,
|
||||
"yesterday_count": 0,
|
||||
"last_7_days_count": 0,
|
||||
"monthly_count": 3,
|
||||
"total_count": 3,
|
||||
"d_today_count": 1,
|
||||
"d_yesterday_count": 0,
|
||||
"d_last_7_days_count": 0,
|
||||
"d_monthly_count": 1,
|
||||
"d_total_count": 1
|
||||
},
|
||||
"date": "2022-04-10T21:31:22.059596+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 4. 新建管理员 `POST /api/account`
|
||||
|
||||
接受参数:
|
||||
1. `account` 管理员帐号,必填
|
||||
2. `password` 管理员密码,必填,最小长度8
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request POST \
|
||||
--url http://localhost:9092/api/account \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data account=hello1 \
|
||||
--data password=12345678
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": null,
|
||||
"date": "2022-04-10T21:31:39.7353132+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 5. 修改管理员密码 `PUT /api/account/:account/update`
|
||||
|
||||
接受参数:
|
||||
1. `account` path 参数,管理员帐号,必填
|
||||
1. `password` 管理员密码,必填,最小长度8
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request PUT \
|
||||
--url http://localhost:9092/api/account/hello/update \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data password=world123
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": null,
|
||||
"date": "2022-04-10T21:31:32.5880538+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 6. 删除短链接 `DELETE /api/url/:url`
|
||||
|
||||
接受参数:
|
||||
1. `url` path 参数,要删除的短链接地址
|
||||
|
||||
(此处省略示例)
|
249
README.md
249
README.md
|
@ -2,7 +2,7 @@
|
|||
|
||||
适合中小型社区网站使用的短链接服务系统,支持短链接生产、查询及302转向,并自带点击量统计、独立IP数统计、访问日志查询:
|
||||
|
||||
1. 支持 Docker One Step Start 部署、Makefile 编译打包
|
||||
1. 支持 Docker One Step Start 部署启动
|
||||
1. 支持短链接生产、查询、存储、302转向
|
||||
1. 支持访问日志查询、访问量统计、独立IP数统计
|
||||
1. 支持 HTTP API 方式新建短链接、禁用/启用短链接、查看短链接统计信息、新建管理员、修改管理员密码
|
||||
|
@ -57,12 +57,6 @@ admin_port = 9092
|
|||
# 例如:https://t.cn/ 是前缀(不要忘记最后一个/符号)
|
||||
url_prefix = http://localhost:9091/
|
||||
|
||||
[redis]
|
||||
...
|
||||
|
||||
[postgres]
|
||||
...
|
||||
|
||||
```
|
||||
|
||||
## Admin 后台默认帐号
|
||||
|
@ -87,176 +81,9 @@ func PasswordBase58Hash(password string) (string, error) {
|
|||
|
||||
## HTTP API 支持
|
||||
|
||||
### `/api` 接口权限说明
|
||||
管理端 HTTP API 支持请参阅 [ohUrlShortener HTTP API](API.md)
|
||||
|
||||
所有 `/api/*` 接口需要通过 `Bearer Token` 方式验证权限,亦即:每个请求 Header 须携带
|
||||
|
||||
```shell
|
||||
Authorization: Bearer {sha256_of_password}
|
||||
```
|
||||
|
||||
`sha256_of_password` 的加密规则,与 `storage/users_storage.go` 中的 `PasswordBase58Hash()` 保持同步
|
||||
|
||||
### 1. 新增短链接 `POST /api/url`
|
||||
|
||||
接受参数:
|
||||
1. `dest_url` 目标链接,必填
|
||||
2. `memo` 备注信息,选填
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request POST \
|
||||
--url http://localhost:9092/api/url \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data dest_url=http://localhost:9092/admin/dashboard \
|
||||
--data memo=dashboard
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": {
|
||||
"short_url": "http://localhost:9091/BUUtpbGp"
|
||||
},
|
||||
"date": "2022-04-10T21:31:29.36559+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 禁用/启用 短链接 `PUT /api/url/:url/change_state`
|
||||
|
||||
接受参数:
|
||||
1. `url` path 参数,指定短链接,必填
|
||||
2. `enable` 禁用时,传入 false;启用时,传入 true
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request PUT \
|
||||
--url http://localhost:9092/api/url/33R5QUtD/change_state \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data enable=false
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": true,
|
||||
"date": "2022-04-10T21:31:25.7744402+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 查询短链接统计数据 `GET /api/url/:url`
|
||||
|
||||
接受参数:
|
||||
1. `url` path 参数,指定短链接,必填
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request GET \
|
||||
--url http://localhost:9092/api/url/33R5QUtD \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded'
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": {
|
||||
"short_url": "33R5QUtD",
|
||||
"today_count": 3,
|
||||
"yesterday_count": 0,
|
||||
"last_7_days_count": 0,
|
||||
"monthly_count": 3,
|
||||
"total_count": 3,
|
||||
"d_today_count": 1,
|
||||
"d_yesterday_count": 0,
|
||||
"d_last_7_days_count": 0,
|
||||
"d_monthly_count": 1,
|
||||
"d_total_count": 1
|
||||
},
|
||||
"date": "2022-04-10T21:31:22.059596+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 4. 新建管理员 `POST /api/account`
|
||||
|
||||
接受参数:
|
||||
1. `account` 管理员帐号,必填
|
||||
2. `password` 管理员密码,必填,最小长度8
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request POST \
|
||||
--url http://localhost:9092/api/account \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data account=hello1 \
|
||||
--data password=12345678
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": null,
|
||||
"date": "2022-04-10T21:31:39.7353132+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 5. 修改管理员密码 `PUT /api/account/:account/update`
|
||||
|
||||
接受参数:
|
||||
1. `account` path 参数,管理员帐号,必填
|
||||
1. `password` 管理员密码,必填,最小长度8
|
||||
|
||||
请求示例:
|
||||
|
||||
```shell
|
||||
curl --request PUT \
|
||||
--url http://localhost:9092/api/account/hello/update \
|
||||
--header 'Authorization: Bearer EZ2zQjC3fqbkvtggy9p2YaJiLwx1kKPTJxvqVzowtx6t' \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data password=world123
|
||||
```
|
||||
|
||||
返回结果:
|
||||
|
||||
```shell
|
||||
{
|
||||
"code": 200,
|
||||
"status": true,
|
||||
"message": "success",
|
||||
"result": null,
|
||||
"date": "2022-04-10T21:31:32.5880538+08:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 6. 删除短链接 `DELETE /api/url/:url`
|
||||
|
||||
接受参数:
|
||||
1. `url` path 参数,要删除的短链接地址
|
||||
|
||||
(此处省略示例)
|
||||
---
|
||||
|
||||
## 短链接在应用启动时会存入 Redis 中
|
||||
|
||||
|
@ -280,25 +107,25 @@ curl --request PUT \
|
|||
|
||||
```golang
|
||||
func GenerateShortLink(initialLink string) (string, error) {
|
||||
if utils.EemptyString(initialLink) {
|
||||
if utils.EmptyString(initialLink) {
|
||||
return "", fmt.Errorf("empty string")
|
||||
}
|
||||
urlHash, err := utils.Sha256Of(initialLink)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
number := new(big.Int).SetBytes(urlHash).Uint64()
|
||||
str := utils.Base58Encode([]byte(fmt.Sprintf("%d", number)))
|
||||
str := utils.Base58Encode(urlHash)
|
||||
return str[:8], nil
|
||||
}
|
||||
```
|
||||
|
||||
## 定时器1分钟清理一次访问日志
|
||||
## 访问日志处理间隔:1分钟
|
||||
|
||||
所在文件 `main.go`
|
||||
|
||||
```golang
|
||||
const ACCESS_LOG_CLEAN_INTERVAL = 1 * time.Minute
|
||||
//清理 Redis 中的访问日志的时间间隔
|
||||
const ACCESS_LOG_CLEAN_INTERVAL = 1 * time.Minute
|
||||
|
||||
func startTicker() error {
|
||||
ticker := time.NewTicker(ACCESS_LOG_CLEAN_INTERVAL)
|
||||
|
@ -313,6 +140,66 @@ func startTicker() error {
|
|||
}
|
||||
```
|
||||
|
||||
## 当日 Top25 访问 URL 榜单处理间隔:5分钟
|
||||
所在文件 `main.go`
|
||||
|
||||
```golang
|
||||
// Top25 榜单计算间隔
|
||||
TOP25_CALC_INTERVAL = 5 * time.Minute
|
||||
|
||||
func startTicker2() error {
|
||||
top25Ticker := time.NewTicker(TOP25_CALC_INTERVAL)
|
||||
for range top25Ticker.C {
|
||||
log.Println("[Top25Urls Ticker] Start.")
|
||||
if err := storage.CallProcedureStatsTop25(); err != nil {
|
||||
log.Printf("Error while trying to calculate Top25Urls %s", err)
|
||||
}
|
||||
log.Println("[Top25Urls Ticker] Finish.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
## 仪表盘中几个相关统计处理间隔:5分钟
|
||||
所在文件 `main.go`
|
||||
|
||||
```golang
|
||||
// 仪表盘页面中其他几个统计数据计算间隔
|
||||
STATS_SUM_CALC_INTERVAL = 5 * time.Minute
|
||||
|
||||
func startTicker4() error {
|
||||
statsSumTicker := time.NewTicker(STATS_SUM_CALC_INTERVAL)
|
||||
for range statsSumTicker.C {
|
||||
log.Println("[StatsSum Ticker] Start.")
|
||||
if err := storage.CallProcedureStatsSum(); err != nil {
|
||||
log.Printf("Error while trying to calculate StatsSum %s", err)
|
||||
}
|
||||
log.Println("[StatsSum Ticker] Finish.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
## 全部访问日志统计处理间隔:30分钟
|
||||
所在文件 `main.go`
|
||||
|
||||
```golang
|
||||
//全部访问日志分析统计的间隔
|
||||
STATS_IP_SUM_CALC_INTERVAL = 30 * time.Minute
|
||||
|
||||
func startTicker3() error {
|
||||
statsIpSumTicker := time.NewTicker(STATS_IP_SUM_CALC_INTERVAL)
|
||||
for range statsIpSumTicker.C {
|
||||
log.Println("[StatsIpSum Ticker] Start.")
|
||||
if err := storage.CallProcedureStatsIPSum(); err != nil {
|
||||
log.Printf("Error while trying to calculate StatsIpSum %s", err)
|
||||
}
|
||||
log.Println("[StatsIpSum Ticker] Finish.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
## Contributor License Agreement
|
||||
|
||||
在 **第一次提交 Pull Request 时** ,请您在 Pull Request 内容中明确写明「本人自愿接受并签署 [《ohUrlShortener Contributor License Agreement》](CLA.md)」,并在 Pull Request 信息中附带该协议链接信息。
|
||||
|
|
Loading…
Reference in New Issue