Commit add68a21 authored by Peter Cheng's avatar Peter Cheng

移除寄信執行任務

parent ed304797
......@@ -12,8 +12,6 @@ env 系統設定
route 系統路由設定
services 系統任務執行主程式
main.go 主程式
## 程式運行原理
......@@ -23,9 +21,9 @@ main.go 主程式
本系統透過nsq做為Message Quete
目前它己有一個現成的寄信服務,其他可以依自己做擴充,要修改Resful API,就自己在Route/Web.go裡面設定路由,然後將對應的程式放到Controllers去,要開發供MQ執行的任務,就將程式放到Services去,並修改./main.go的runService內容
本系統可以搭配服務管理員一起使用
本來有想過這隻程式只做佇列排程就好,最後在透過gRPC或是CURL去呼叫其他的Restful API,這樣會比較乾淨,只是透過外部呼叫效能不會比同一個系統裡面執行還好
https://github.com/teed7334-restore/homekeeper
如果連nsq都不想自己架的人,可以自己安裝Docker與Docker Compose,自己到./dev_env資料夾下打docker-compose up -d --build,nsq會自己架好
......@@ -46,6 +44,17 @@ go get -u -v github.com/gin-gonic/gin
## 程式操作流程
1. 將./env/env.swp檔名改成env.go
2. 修改./env/env.go並設定您的SMTP Server與Message Quete Server
2. 修改./env/env.go並設定您的Message Quete Server
3. 到./beans底下,運行protoc --go_out=plugins=grpc+retag:. *.proto
4. go run main.go
\ No newline at end of file
4. go run main.go
## API呼叫網址與參數
寄信服務(須搭配服務管理員) http://[Your Host Name]:8805/Mail/SendMail
```
//HTTP Header需設定成Content-Type: application/json
{
"to": "admin@admin.com",
"subject": "這是一封測試信",
"content": "這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />"
}
```
\ No newline at end of file
......@@ -2,6 +2,17 @@
// source: quete.proto
// DO NOT EDIT!
/*
Package beans is a generated protocol buffer package.
It is generated from these files:
quete.proto
sendMail.proto
It has these top-level messages:
Quete
SendMail
*/
package beans
import proto "github.com/golang/protobuf/proto"
......@@ -13,6 +24,12 @@ var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Quete struct {
UseService string `protobuf:"bytes,1,opt,name=UseService" json:"UseService"`
UseParams string `protobuf:"bytes,2,opt,name=UseParams" json:"UseParams"`
......@@ -21,7 +38,7 @@ type Quete struct {
func (m *Quete) Reset() { *m = Quete{} }
func (m *Quete) String() string { return proto.CompactTextString(m) }
func (*Quete) ProtoMessage() {}
func (*Quete) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
func (*Quete) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *Quete) GetUseService() string {
if m != nil {
......@@ -41,9 +58,9 @@ func init() {
proto.RegisterType((*Quete)(nil), "beans.Quete")
}
func init() { proto.RegisterFile("quete.proto", fileDescriptor1) }
func init() { proto.RegisterFile("quete.proto", fileDescriptor0) }
var fileDescriptor1 = []byte{
var fileDescriptor0 = []byte{
// 98 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2e, 0x2c, 0x4d, 0x2d,
0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc, 0x2b, 0x56, 0x72,
......
......@@ -14,21 +14,21 @@ var _ = fmt.Errorf
var _ = math.Inf
type SendMail struct {
To []string ` protobuf:"bytes,1,rep,name=To" json:"To,omitempty" form:"to"`
Subject string ` protobuf:"bytes,2,opt,name=Subject" json:"Subject,omitempty" form:"subject"`
Content string ` protobuf:"bytes,3,opt,name=Content" json:"Content,omitempty" form:"content"`
To string ` protobuf:"bytes,1,opt,name=To" json:"To,omitempty" form:"to"`
Subject string ` protobuf:"bytes,2,opt,name=Subject" json:"Subject,omitempty" form:"subject"`
Content string ` protobuf:"bytes,3,opt,name=Content" json:"Content,omitempty" form:"content"`
}
func (m *SendMail) Reset() { *m = SendMail{} }
func (m *SendMail) String() string { return proto.CompactTextString(m) }
func (*SendMail) ProtoMessage() {}
func (*SendMail) Descriptor() ([]byte, []int) { return fileDescriptor2, []int{0} }
func (*SendMail) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
func (m *SendMail) GetTo() []string {
func (m *SendMail) GetTo() string {
if m != nil {
return m.To
}
return nil
return ""
}
func (m *SendMail) GetSubject() string {
......@@ -49,15 +49,15 @@ func init() {
proto.RegisterType((*SendMail)(nil), "beans.SendMail")
}
func init() { proto.RegisterFile("sendMail.proto", fileDescriptor2) }
func init() { proto.RegisterFile("sendMail.proto", fileDescriptor1) }
var fileDescriptor2 = []byte{
// 109 bytes of a gzipped FileDescriptorProto
var fileDescriptor1 = []byte{
// 106 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x4e, 0xcd, 0x4b,
0xf1, 0x4d, 0xcc, 0xcc, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc,
0x2b, 0x56, 0xf2, 0xe3, 0xe2, 0x08, 0x86, 0x4a, 0x08, 0xf1, 0x71, 0x31, 0x85, 0xe4, 0x4b, 0x30,
0x2a, 0x30, 0x6b, 0x70, 0x06, 0x31, 0x85, 0xe4, 0x0b, 0x49, 0x70, 0xb1, 0x07, 0x97, 0x26, 0x65,
0xa5, 0x26, 0x97, 0x48, 0x30, 0x29, 0x30, 0x6a, 0x70, 0x06, 0xc1, 0xb8, 0x20, 0x19, 0xe7, 0xfc,
0xbc, 0x92, 0xd4, 0xbc, 0x12, 0x09, 0x66, 0x88, 0x0c, 0x94, 0x9b, 0xc4, 0x06, 0x36, 0xdd, 0x18,
0x10, 0x00, 0x00, 0xff, 0xff, 0x69, 0x4f, 0x3a, 0xdc, 0x6f, 0x00, 0x00, 0x00,
0x2a, 0x30, 0x6a, 0x70, 0x06, 0x31, 0x85, 0xe4, 0x0b, 0x49, 0x70, 0xb1, 0x07, 0x97, 0x26, 0x65,
0xa5, 0x26, 0x97, 0x48, 0x30, 0x81, 0x05, 0x61, 0x5c, 0x90, 0x8c, 0x73, 0x7e, 0x5e, 0x49, 0x6a,
0x5e, 0x89, 0x04, 0x33, 0x44, 0x06, 0xca, 0x4d, 0x62, 0x03, 0x9b, 0x6e, 0x0c, 0x08, 0x00, 0x00,
0xff, 0xff, 0x98, 0x81, 0xc8, 0x1d, 0x6f, 0x00, 0x00, 0x00,
}
......@@ -3,7 +3,7 @@ syntax = "proto3";
package beans;
message SendMail {
repeated string To = 1; //`form:"to"`
string To = 1; //`form:"to"`
string Subject = 2; //`form:"subject"`
string Content = 3; //`form:"content"`
}
\ No newline at end of file
package controllers
import "github.com/gin-gonic/gin"
//Home 首頁
func Home(response *gin.Context) {
response.String(200, "")
}
......@@ -32,7 +32,7 @@ func SendMail(c *gin.Context) {
log.Println(err)
}
json, _ := json.Marshal(sendMail)
quete := "SendMail</UseService>" + string(json)
quete := "Mail/SendMail</UseService>" + string(json)
postMessage(quete)
c.JSON(http.StatusOK, gin.H{"status": "true"})
}
......@@ -14,11 +14,8 @@ type Config struct {
Topic string
Channel string
}
Mail struct {
Host string
User string
Password string
Port string
Housekeeper struct {
Host string
}
}
......@@ -31,10 +28,7 @@ func init() {
cfg.Message.Received.Address = "127.0.0.1:4161"
cfg.Message.Topic = "Mail"
cfg.Message.Channel = "SendMail"
cfg.Mail.Host = ""
cfg.Mail.User = ""
cfg.Mail.Password = ""
cfg.Mail.Port = ""
cfg.Housekeeper.Host = "http://127.0.0.1:8806"
}
//GetEnv 取得環境參數
......
package main
import (
"encoding/json"
"bytes"
"log"
"net/http"
"strings"
"time"
"./services"
"./beans"
"./env"
"./route"
"github.com/bitly/go-nsq"
......@@ -17,7 +15,6 @@ import (
var cfg = env.GetEnv()
func main() {
services.Create("INFO", "存入Elasticsearch")
upStream := make(chan time.Time)
go func() {
webService()
......@@ -71,12 +68,20 @@ func getQuete(message string) []string {
}
func runServices(quete []string) {
useService := quete[0]
path := quete[0]
params := []byte(quete[1])
switch useService {
case "SendMail":
sendMail := &beans.SendMail{}
json.Unmarshal(params, sendMail)
services.SendMail(sendMail)
url := cfg.Housekeeper.Host + "/" + path
getURL(url, params)
}
func getURL(url string, params []byte) {
request, err := http.NewRequest("POST", url, bytes.NewBuffer(params))
request.Header.Set("X-Custom-Header", "counter")
request.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Panic(err)
}
defer resp.Body.Close()
}
......@@ -12,7 +12,6 @@ var cfg = env.GetEnv()
func API() *gin.Engine {
gin.SetMode(cfg.Env)
route := gin.Default()
route.Any("/", controllers.Home)
route.POST("/Mail/SendMail", controllers.SendMail)
return route
}
package services
import (
"net/smtp"
"../beans"
"../env"
)
//SendMail 寄發通知郵件
func SendMail(params *beans.SendMail) error {
cfg := env.GetEnv()
host := cfg.Mail.Host + ":" + cfg.Mail.Port
auth := smtp.PlainAuth("", cfg.Mail.User, cfg.Mail.Password, cfg.Mail.Host)
message := []byte(
"Subject: " + params.GetSubject() + "\r\n" +
"To: " + params.GetTo()[0] + "\r\n" +
"From: " + cfg.Mail.User + "\r\n" +
"Content-Type: text/plain; charset=UTF-8" + "\r\n" +
"\r\n" +
params.GetContent() + "\r\n" +
"\r\n",
)
err := smtp.SendMail(host, auth, cfg.Mail.User, params.GetTo(), message)
return err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment