Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Refactor pdf sender
Browse files Browse the repository at this point in the history
Using config from file, all the parameters are in following orders:
  1. args
  2. config files
Supporting different document kinds for user to download.
Adding sub-commands, run and config
  • Loading branch information
orangedeng committed Jun 16, 2021
1 parent 49bfbfe commit 3fb761f
Show file tree
Hide file tree
Showing 16 changed files with 900 additions and 529 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea
/bin
/dist
.dapper
.dapper
test-config.yml
145 changes: 100 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,100 @@

一个用来发送 Rancher 中文文档的服务

## 环境变量配置

- SMTP_RANCHER_TO_DAY: 每日定期接收消息邮箱
- SMTP_RANCHER_TO_MON: 每月定期接收消息邮箱
- DAY_CRON: 每日定时时间
- MON_CRON: 每月定时时间

- SMTP_USER: 服务邮箱用户名
- SMTP_PWD: 服务邮箱用户密码
- SMTP_ENDPOINT: SMTP 端点
- SMTP_PORT: 服务邮箱端口
- SENDER_EMAIL: 服务邮箱端口

- Rancher2_PDF_URL: Rancher2.x PDF 文件地址
- Rancher2_PWD: Rancher2.x PDF 文件密码
- RKE_PDF_URL: RKE PDF 文件地址
- RKE_PWD: RKE PDF 文件密码
- RKE2_PDF_URL: RKE2 PDF 文件地址
- RKE2_PWD: RKE2 PDF 文件密码
- K3s_PDF_URL: K3s PDF 文件地址
- K3s_PWD: K3s PDF 文件密码
- Octopus_PDF_URL: Octopus PDF 文件地址
- Octopus_PWD: Octopus PDF 文件密码
- Harvester_PDF_URL: Harvester PDF 文件地址
- Harvester_PWD: Harvester PDF 文件密码

- DB_HOST_IP: 数据库地址
- DB_USERNAME: 数据库用户名
- DB_PASSWORD: 数据库密码
- DB_NAME: 数据库名

- ALI_REGION_ID: 阿里云区域
- ALI_ACCESS_KEYID: 阿里云访问令牌
- ALI_ACCESS_SECRET: 阿里云访问令牌密钥
- ALI_SIGN_NAME: 阿里云短信签名
- ALI_TEMPLATE_CODE: 阿里云短信模板
## 启动配置

```text
NAME:
pdf-sender - Send pdf documents to our lovely users.
USAGE:
main [global options] command [command options] [arguments...]
VERSION:
v0.0.0-dev
COMMANDS:
config Print out the merged configuration
run Run pdf sender server
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--debug Enable Debug log for pdf sender
--config-file value, -f value (default: "/etc/pdf-sender.yml") [$CONFIG_FILE]
--help, -h show help
--version, -v print the version
```

配置文件示例可以参考程序默认配置 `package/init.yml`,配置文件内容如下:

```yaml
documents:
- name: k3s
title: K3s
filename: K3s.pdf
kind:
- pdf
- name: harvester
title: Harvester
filename: Harvester.pdf
kind:
- pdf
- name: octopus
title: Octopus
filename: Octopus_CN_Doc.pdf
kind:
- pdf
- name: rancher2
title: Rancher2.x
filename: Rancher2.x_CN_Doc.pdf
kind:
- pdf
- name: rancher2.5
title: Rancher2.5
filename: Rancher2.5_CN_Doc.pdf
kind:
- pdf
- name: rancher1
title: Rancher1.6
filename: rancher1.6.pdf
kind:
- pdf
- name: rancher-ent
title: 白皮书
filename: Rancher企业版拓展功能概述及购买方式(2021).pdf
ossPathPrefixOverride: RancherEntPDF
kind:
- ent
kinds:
pdf:
header: |
您好,
您可以通过下面的链接下载 Rancher 中文文档。
footer: |
Best Regards,
SUSE/Rancher 中国团队
subject: Rancher 2.x 中文文档
senderName: Rancher Labs 中国
description: "中文文档"
ent:
subject: 「Rancher 企业版拓展功能概述及购买方式」白皮书下载
header: |
您好,
您可以通过下面的链接下载 「Rancher企业版拓展功能概述及购买方式」
footer: |
Best Regards,
SUSE/Rancher 中国团队
description: "企业版白皮书"
senderName: Rancher Labs 中国
```

## 建表 SQL 语句

当前版本建表语句如下:

```sql
CREATE TABLE `user` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
Expand All @@ -51,19 +106,19 @@ CREATE TABLE `user` (
`email` varchar(255) DEFAULT NULL,
`savetime` datetime DEFAULT NULL,
`status` tinyint(1) DEFAULT NULL,
`kind` varchar(20) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=77 DEFAULT CHARSET=utf8mb4;
```

## 后续功能规划
如从 v0.2.x 升级到 v0.3.0,则需要对表进行更变

1. 支持从配置文件中读取发送配置,发送配置包括
2. 构建镜像时将默认配置复制到容器中使用
3. 精简启动参数
```sql
ALTER TABLE pdf.`user` ADD kind varchar(20) NULL;
```

支持配置文件设置所有从环境变量中读取的参数,包括但不限于
## 后续版本规划

- 文档下载配置
- 阿里云 API 访问配置
- oss 配置
- 邮件发送配置
- 上传周期生成的文件到 oss
- 如果需要的情况下,做高可用支持
- 根据配置的结构自动生成命令行参数
55 changes: 35 additions & 20 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

var (
VERSION = "v0.0.0-dev"
port int
)

func main() {
Expand All @@ -25,47 +24,63 @@ func main() {
app.Version = VERSION
app.Usage = "Send pdf documents to our lovely users."
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "port,p",
EnvVar: "HTTP_PORT",
Value: 8080,
Destination: &port,
},
cli.BoolFlag{
Name: "debug",
Name: "debug",
Usage: "Enable Debug log for pdf sender",
},
cli.StringFlag{
Name: "config-file,f",
Required: true,
EnvVar: "CONFIG_FILE",
Value: "/etc/pdf-sender.yml",
},
}
app.Flags = append(app.Flags, apis.GetFlags()...)
app.Flags = append(app.Flags, types.GetFlags()...)
app.Action = func(ctx *cli.Context) error {
logrus.Infof("server running, listening at :%d\n", port)
return http.ListenAndServe(fmt.Sprintf(":%d", port), limiter.IPLimitMiddleware(apis.RegisterAPIs().ServeMux))
}
app.Before = before
app.Commands = append(app.Commands, types.GetConfigCommand())
app.Commands = append(app.Commands, getRunCommand())

if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}

func before(ctx *cli.Context) error {
if ctx.Bool("debug") {
if err := types.MergeConfig(ctx); err != nil {
return err
}

if ctx.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}

if err := apis.ConnectMysql(); err != nil {
return err
}

if err := types.InitAliyunClients(ctx); err != nil {
return err
}

if err := apis.InitEmailBody(ctx); err != nil {
if err := types.InitEmailBody(ctx); err != nil {
return err
}

if err := apis.ConnectMysql(); err != nil {
if err := types.Validate(); err != nil {
return err
}

apis.StartCorn(ctx)
return apis.StartCorn(ctx)
}

return nil
func getRunCommand() cli.Command {
cmd := cli.Command{
Name: "run",
Usage: "Run pdf sender server",
Flags: types.GetFlags(),
Action: func(ctx *cli.Context) error {
logrus.Infof("server running, listening at :%d\n", types.Config.Port)
return http.ListenAndServe(fmt.Sprintf(":%d", types.Config.Port), limiter.IPLimitMiddleware(apis.RegisterAPIs().ServeMux))
},
Before: before,
}
return cmd
}
1 change: 1 addition & 0 deletions package/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM alpine:3
COPY bin/pdf-sender /pdf-sender
COPY package/init.yml /etc/pdf-sender.yml
CMD ["/pdf-sender"]
71 changes: 71 additions & 0 deletions package/init.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
documents:
- name: k3s
title: K3s
filename: K3s.pdf
kind:
- pdf
- name: harvester
title: Harvester
filename: Harvester.pdf
kind:
- pdf
- name: octopus
title: Octopus
filename: Octopus_CN_Doc.pdf
kind:
- pdf
- name: rancher2
title: Rancher2.x
filename: Rancher2.x_CN_Doc.pdf
kind:
- pdf
- name: rancher2.5
title: Rancher2.5
filename: Rancher2.5_CN_Doc.pdf
kind:
- pdf
- name: rancher1
title: Rancher1.6
filename: rancher1.6.pdf
kind:
- pdf
- name: rke
title: RKE
filename: rke.pdf
kind:
- pdf
- name: rke2
title: RKE2
filename: rke2.pdf
kind:
- pdf
- name: rancher-ent
title: 白皮书
filename: Rancher企业版拓展功能概述及购买方式(2021).pdf
ossPathPrefixOverride: RancherEntPDF
kind:
- ent
kinds:
pdf:
header: |
您好,
您可以通过下面的链接下载 Rancher 中文文档。
footer: |
Best Regards,
SUSE/Rancher 中国团队
subject: Rancher 2.x 中文文档
senderName: SUSE/Rancher 中国团队
description: "中文文档"
ent:
subject: 「Rancher 企业版拓展功能概述及购买方式」白皮书下载
header: |
您好,
您可以通过下面的链接下载 「Rancher企业版拓展功能概述及购买方式」
footer: |
Best Regards,
SUSE/Rancher 中国团队
description: "企业版白皮书"
senderName: SUSE/Rancher 中国团队
Loading

0 comments on commit 3fb761f

Please sign in to comment.