Commit 346e0d04 authored by Peter Cheng's avatar Peter Cheng

寄信服務開發完成

parent e2adf4ba
env/env\.go
# homekeeper
服務管理員,日後會將工作上常用到的服務放在裡面,好比變動資料庫、區塊鏈上鏈、爬蟲抓資料等放進來,讓裡面可以有許多現成的微服務,帶了就走
## 資料夾結構
beans 用來裝Call API後之ResultObject
controllers Restful API呼叫用控制器
env 系統設定
route 系統路由設定
main.go 主程式
## 程式運行原理
本系統會啟動一個Restful API
目前它己有一個現成的寄信服務,其他可以依自己做擴充
本系統可以搭配佇列管理員管理員使用,這樣一來你就能安排一些非同步任務行程
https://github.com/teed7334-restore/counter
## 必須套件
本程式透過Google Protobuf 3產生所需之ResultObject,然Proto 3之後官方不支持Custom Tags,所以還需要多安裝一個寫入retags的套件
git clone https://github.com/qianlnk/protobuf.git $GOPATH/src/github.com/golang/protobuf
go install $GOPATH/src/github.com/golang/protobuf/protoc-gen-go
及Restful Framework
go get -u -v github.com/gin-gonic/gin
## 程式操作流程
1. 將./env/env.swp檔名改成env.go
2. 修改./env/env.go並設定您的SMTP Server
3. 到./beans底下,運行protoc --go_out=plugins=grpc+retag:. *.proto
4. go run main.go
## API呼叫網址與參數
寄信服務 http://[Your Host Name]:8806/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
// Code generated by protoc-gen-go.
// source: sendMail.proto
// DO NOT EDIT!
/*
Package beans is a generated protocol buffer package.
It is generated from these files:
sendMail.proto
It has these top-level messages:
SendMail
*/
package beans
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
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 SendMail struct {
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 fileDescriptor0, []int{0} }
func (m *SendMail) GetTo() string {
if m != nil {
return m.To
}
return ""
}
func (m *SendMail) GetSubject() string {
if m != nil {
return m.Subject
}
return ""
}
func (m *SendMail) GetContent() string {
if m != nil {
return m.Content
}
return ""
}
func init() {
proto.RegisterType((*SendMail)(nil), "beans.SendMail")
}
func init() { proto.RegisterFile("sendMail.proto", fileDescriptor0) }
var fileDescriptor0 = []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, 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,
}
syntax = "proto3";
package beans;
message SendMail {
string To = 1; //`form:"to"`
string Subject = 2; //`form:"subject"`
string Content = 3; //`form:"content"`
}
\ No newline at end of file
package controllers
import (
"log"
"../beans"
"../env"
"github.com/gin-gonic/gin"
"gopkg.in/gomail.v2"
)
//SendMail 寄信用API
func SendMail(c *gin.Context) {
params := &beans.SendMail{}
err := c.BindJSON(params)
if err != nil {
log.Println(err)
}
doSendMail(params)
}
//doSendMail 寄發通知郵件
func doSendMail(params *beans.SendMail) {
cfg := env.GetEnv()
mail := gomail.NewMessage()
mail.SetHeader("From", cfg.Mail.From)
mail.SetHeader("To", params.GetTo())
mail.SetHeader("Subject", params.GetSubject())
mail.SetBody("text/html", params.GetContent())
send := gomail.NewPlainDialer(cfg.Mail.Host, cfg.Mail.Port, cfg.Mail.User, cfg.Mail.Password)
if err := send.DialAndSend(mail); err != nil {
log.Panic(err)
}
}
package env
//Config 系統參數
type Config struct {
Env string
TimeFormat string
Mail struct {
Host string
Port int
From string
User string
Password string
}
}
var cfg = &Config{}
func init() {
cfg.Env = "release"
cfg.TimeFormat = "2006-01-02 15:04:05"
cfg.Mail.Host = ""
cfg.Mail.Port = 25
cfg.Mail.From = ""
cfg.Mail.User = ""
cfg.Mail.Password = ""
}
//GetEnv 取得環境參數
func GetEnv() *Config {
return cfg
}
package main
import (
"./env"
"./route"
)
var cfg = env.GetEnv()
func main() {
webService()
}
//webService Restful API服務
func webService() {
api := route.API()
api.Run(":8806")
}
package route
import (
"../controllers"
"../env"
"github.com/gin-gonic/gin"
)
var cfg = env.GetEnv()
//API Restful路由
func API() *gin.Engine {
gin.SetMode(cfg.Env)
route := gin.Default()
route.POST("/Mail/SendMail", controllers.SendMail)
return route
}
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