Skip to content

Commit

Permalink
重构
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Jan 30, 2022
2 parents 3eee438 + d234caf commit 9b20c60
Show file tree
Hide file tree
Showing 48 changed files with 2,315 additions and 949 deletions.
22 changes: 9 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# Binaries for programs and plugins
# OS
*.exe
*.exe~
*.dll
*.so
*.dylib
*.tar.gz
target/
.DS_Store

# Test binary, built with `go test -c`
*.test
# IDE
.idea/
.vscode/
*.iml

# Output of the go coverage tool, specifically when used with LiteIDE
# Program
target/
*.test
*.out

# logs
*.log

# ide
.idea/
*.iml
.vscode/*
23 changes: 16 additions & 7 deletions FUTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

### Future versions

* 支持邮件中携带附件 (P0)
* dialer 对象池或连接池优化 (P1)
* 邮件发送信息监控平台 (P2)
* 增加 Vex 远程调用接口 (P3)
* 增加 JsonRPC 远程调用接口 (P3)
* 增加 UDP 远程调用接口 (P3)
* 增加 WebSocket 远程调用接口 (P3)
* [ ] dialer 对象池或连接池优化 (P0)
* [ ] 增加 net/rpc 远程调用接口 (P1)

### v0.3.x

* [ ] 支持邮件中携带附件 (P0)
* [ ] 接入监控平台 (P1)
* [ ] TLS 证书支持 (P1)
* [x] 增加 http 远程调用接口 (P0)
* [x] 增加 grpc 远程调用接口 (P0)
* [ ] 增加 vex 远程调用接口 (P1)
* [ ] 增加 udp 远程调用接口 (P2)

### v0.1.x - v0.2.x

* [x] 邮件发送功能
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v0.3.0-alpha

> 此版本发布于 2022-01-30
* 全新重构版本!!!

### v0.2.3-alpha

> 此版本发布于 2021-09-03
Expand Down
19 changes: 12 additions & 7 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

* Plain and Html form email supports
* Synchronous/Asynchronous mode supports, and timeout is available in synchronous mode
* HTTP api supports, and use http2 is ok
* Gracefully shutdown with signal mechanism supports
* Support http/http2/grpc/vex/udp protocol
* Gracefully shutdown with signal mechanism

_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._

Expand All @@ -31,9 +31,9 @@ Postar has two ways to get binary:

1. Invoking `./build.sh` in the root of source code will generate target directory, which contains all binary files.

2. Build by `go` command, see `go build`.
2. Building by `go build` (or running by `go run`) in `cmd/postar`, see `go`.

_Notice: Default config file is `/opt/postar/conf/postar.ini`, default log output directory is `/opt/postar/log/`, and you need them to start service._
_Notice: Default config file is `./postar.ini`, default log output directory is `./log/service.log`._

> Want to know how to use? See [_examples](_examples).
Expand All @@ -43,6 +43,11 @@ If you find that something is not working as expected please open an _**issue**_

### 📦 Projects postar used

| Project | Author | Description | link |
| -----------|--------|-------------|------------------|
| logit | FishGoddess | A high-performance and easy-to-use logging foundation | [Gitee](https://gitee.com/FishGoddess/logit) / [GitHub](https://github.com/FishGoddess/logit) |
| Project | Author | Description | link |
|---------|-------------|-------------------------------------------------------|-------------------------------------------------------------------------------------------------|
| logit | FishGoddess | A high-performance and easy-to-use logging foundation | [Gitee](https://gitee.com/FishGoddess/logit) / [GitHub](https://github.com/FishGoddess/logit) |
| errors | FishGoddess | A lib for handling error gracefully in Go | [Gitee](https://gitee.com/FishGoddess/errors) / [GitHub](https://github.com/FishGoddess/errors) |
| ants | panjf2000 | A high-performance and low-cost goroutine pool | [GitHub](https://github.com/panjf2000/ants) |
| gomail | alexcesaro | The best way to send emails in Go | [GitHub](https://github.com/go-gomail/gomail/tree/v2) |
| ini | unknwon | Provides INI file read and write functionality in Go | [GitHub](https://github.com/go-ini/ini) |
| httprouter | julienschmidt | A high performance HTTP request router | [GitHub](https://github.com/julienschmidt/httprouter) |
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

* 支持发送文本邮件和 HTML 邮件
* 支持同步、异步邮件发送,同步模式可配置超时
* 支持 http 请求调用接口,可配置 http2 协议
* 支持 http/http2/grpc/vex/udp 等网络协议
* 支持 signal 通知的平滑下线

_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_
Expand All @@ -31,19 +31,23 @@ Postar 的二进制执行包可以通过源码进行编译得到,一共有两

1. 在源码根目录执行 `./build.sh` 会生成 target 目录,所有的二进制包都在里面

2. 通过 `go` 命令构建或启动服务,参考 `go build`
2. `cmd/postar` 目录下使用 `go build` 构建服务(或 `go run` 启动服务),参考 `go` 命令

_注意:默认的配置文件路径是 `/opt/postar/conf/postar.ini`默认的日志输出路径是 `/opt/postar/log/`,需要有对应的文件和文件夹才可以启动_
_注意:默认的配置文件路径是 `./postar.ini`默认的日志输出是 `./log/service.log`_

> 想知道怎么使用?查看 [_examples](_examples)
### 👥 贡献者

如果您觉得 **postar** 缺少您需要的功能,请不要犹豫,马上参与进来,发起一个 _**issue**_

### 📦 postar 使用的技术

| 项目 | 作者 | 描述 | 链接 |
| -----------|--------|-------------|-------------------|
| logit | FishGoddess | 一个高性能、功能强大且极易上手的日志库 | [码云](https://gitee.com/FishGoddess/logit) / [GitHub](https://github.com/FishGoddess/logit) |
### 📦 Postar 使用的技术

| 项目 | 作者 | 描述 | 链接 |
|--------|-------------|---------------------|----------------------------------------------------------------------------------------------|
| logit | FishGoddess | 一个高性能、功能强大且极易上手的日志库 | [码云](https://gitee.com/FishGoddess/logit) / [GitHub](https://github.com/FishGoddess/logit) |
| errors | FishGoddess | 一个用于优雅地处理 Go 中错误的库 | [码云](https://gitee.com/FishGoddess/errors) / [GitHub](https://github.com/FishGoddess/errors) |
| ants | panjf2000 | 一个高性能且低损耗的 goroutine 池 | [GitHub](https://github.com/panjf2000/ants) |
| gomail | alexcesaro | 一个用于在 Go 中发送邮件的库 | [GitHub](https://github.com/go-gomail/gomail/tree/v2) |
| ini | unknwon | 一个操作 ini 配置的库 | [GitHub](https://github.com/go-ini/ini) |
| httprouter | julienschmidt | 一个高性能的 http 路由库 | [GitHub](https://github.com/julienschmidt/httprouter) |
25 changes: 0 additions & 25 deletions _examples/basic.http

This file was deleted.

84 changes: 33 additions & 51 deletions _examples/config/postar.ini
Original file line number Diff line number Diff line change
@@ -1,52 +1,34 @@
# Copyright 2021 Ye Zi Jie. All rights reserved.
# Use of this source code is governed by a MIT style
# license that can be found in the LICENSE file.
#
# Postar 配置文件案例
# Author: FishGoddess

# Global 是全局配置区块,主要用于配置整体的运行状态。
# sender_type 配置邮件发送器的类型,可选项为 smtp。默认是 smtp。
# server_type 配置网络服务器的类型,可选项为 http。默认是 http。
[global]
#sender_type = "smtp"
#server_type = "http"

# Logger 是日志配置区块,主要用于配置日志的运行状态。
# level 配置日志记录的级别,可选项为 debug/info/warn/error/off,其中 off 表示关闭日志记录。默认是 info。
# time_format 配置日志记录的时间格式化模板,若配置为 "",则将时间记录为 unix 秒级时间戳。默认为 2006-01-02 15:04:05.000。
# output_file 配置非错误日志的输出文件路径。windows 默认是 ../log/postar.log,其他系统默认是 /opt/postar/log/postar.log。
# error_output_file 配置错误日志的输出文件路径。windows 默认是 ../log/postar.error.log,其他系统默认是 /opt/postar/log/postar.error.log。
[logger]
#level = "info"
#time_format = "2006-01-02 15:04:05.000"
#output_file = "/opt/postar/log/postar.log"
#error_output_file = "/opt/postar/log/postar.error.log"

# Sender 是邮件发送器配置区块,主要用于配置邮件发送器的运行状态。
# smtp_host 配置 smtp 服务器的主机名。默认从环境变量中取 POSTAR_SMTP_HOST 的值。
# smtp_port 配置 smtp 服务器的端口号。默认为 587。
# smtp_user 配置 smtp 服务器的用户名。默认从环境变量中取 POSTAR_SMTP_USER 的值。
# smtp_password 配置 smtp 服务器的密码。默认从环境变量中取 POSTAR_SMTP_PASSWORD 的值。
# worker_number 配置邮件发送器的异步工作协程数,仅在 sender_type 为 smtp 时有效。默认为 64。
# request_channel_size 配置发送请求任务队列的最大大小,若任务堆积导致队列满了,则新的发送请求会被阻塞。默认为 65536。
[sender]
#smtp_host = "smtp.xxx.com"
#smtp_port = 587
#smtp_user = "[email protected]"
#smtp_password = "xxx"
#worker_number = 64
#request_channel_size = 65536

# Server 是网络服务器配置区块,主要用于配置网络服务器的运行状态。
# address 配置网络服务器的绑定地址,包括 IP 和端口号。默认为 :5897。
# use_http2 选择是否使用 http2(建议使用 h2 标准配置证书),可选项为 true/false。默认为 false。
# tls_cert 配置 TLS 证书文件的路径,只有 use_http2 = true,该选项才会生效。windows 默认为 ../conf/postar_cert.pem,其它系统默认是 /opt/postar/conf/postar_cert.pem。
# tls_cert_key 配置 TLS 证书 key 文件的路径,只有 use_http2 = true,该选项才会生效。windows 默认为 ../conf/postar_cert.key,其它系统默认是 /opt/postar/conf/postar_cert.key。
# wait_for_closing 配置服务器关闭等待时间,服务关闭时会等待请求处理完成,若等待超过改时间,服务会强制退出。单位为秒,默认是 30 秒。
[task]
# Start how many workers to finish tasks.
# Default is 64.
worker_number = 64
# The max size of task queue, tasks will be blocked if queue is full.
# Set to 0 if you want queue is unlimited.
queue_size = 0
# The sending mode of task, available values: true, false.
# This will apply to all tasks and you can use options to override this value in request.
# Default is false.
async = false
# The sending timeout in millisecond of task.
# This will apply to all tasks and you can use options to override this value in request.
# Default is 10s.
timeout = 10000
[server]
#address = ":5897"
#use_http2 = false
#tls_cert = "../conf/postar_cert.pem"
#tls_cert_key = "../conf/postar_cert.key"
#wait_for_closing = 30
# Available values: http, grpc.
# Default is http.
type = "http"
# You can bind a specific ip and port for your server.
address = ":5897"
# The closing timeout in second of server.
# Default is 30s.
stop_timeout = 30
[smtp]
# Specify your smtp server host.
host = ""
# Specify your smtp server port.
# Default is 587.
port = 587
# Specify your smtp server user.
user = ""
# Specify your smtp server password.
password = ""
36 changes: 36 additions & 0 deletions _examples/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"context"
"fmt"
"github.com/avinoplan/postar/api"
"google.golang.org/grpc"
"os"
)

func main() {
conn, err := grpc.Dial("127.0.0.1:5897", grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()

req := &api.SendEmailRequest{
Email: &api.Email{
Receivers: []string{os.Getenv("POSTAR_RECEIVER")},
Subject: "测试邮件",
BodyType: "text/html",
Body: "<p>邮件内容</p>",
},
Options: nil,
}
fmt.Printf("client req: %+v\n", req)

client := api.NewPostarServiceClient(conn)
rsp, err := client.SendEmail(context.Background(), req)
if err != nil {
panic(err)
}

fmt.Printf("server rsp: %+v\n", rsp)
}
8 changes: 0 additions & 8 deletions _examples/http-client.env.json

This file was deleted.

50 changes: 50 additions & 0 deletions _examples/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"bytes"
"fmt"
"github.com/avinoplan/postar/api"
"google.golang.org/protobuf/proto"
"io/ioutil"
"net/http"
"os"
)

func main() {
url := "http://127.0.0.1:5897/sendEmail"

emailReq := &api.SendEmailRequest{
Email: &api.Email{
Receivers: []string{os.Getenv("POSTAR_RECEIVER")},
Subject: "测试邮件",
BodyType: "text/html",
Body: "<p>邮件内容</p>",
},
Options: nil,
}
fmt.Printf("client req: %+v\n", emailReq)

marshaled, err := proto.Marshal(emailReq)
if err != nil {
panic(err)
}

resp, err := http.Post(url, "application/octet-stream", bytes.NewReader(marshaled))
if err != nil {
panic(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}

emailRsp := new(api.SendEmailResponse)
err = proto.Unmarshal(body, emailRsp)
if err != nil {
panic(err)
}

fmt.Printf("server rsp: %+v\n", emailRsp)
}
4 changes: 2 additions & 2 deletions _icons/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9b20c60

Please sign in to comment.