Commit 5e87fee0 authored by Peter Cheng's avatar Peter Cheng

追加員工打卡記錄上鏈API

parent 590fd10f
...@@ -57,4 +57,34 @@ go get -u -v github.com/gin-gonic/gin ...@@ -57,4 +57,34 @@ go get -u -v github.com/gin-gonic/gin
"subject": "這是一封測試信", "subject": "這是一封測試信",
"content": "這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />" "content": "這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />這是一封測試信<br />"
} }
```
將每天員工打卡資料上鏈(須搭配服務管理員) http://[Your Host Name]:8805/PunchClock/UploadDailyPunchclockData
```
//HTTP Header需設定成Content-Type: application/json
{
"employee": {
"identify": "00190",
"firstName": "Peter",
"lastName": "Cheng"
},
"punchclock": {
"begin": {
"year": "2019",
"month": "08",
"day": "28",
"hour": "09",
"minute": "24",
"second": "00"
},
"end": {
"year": "2019",
"month": "08",
"day": "28",
"hour": "20",
"minute": "05",
"second": "00"
}
}
}
``` ```
\ No newline at end of file
...@@ -7,10 +7,12 @@ Package beans is a generated protocol buffer package. ...@@ -7,10 +7,12 @@ Package beans is a generated protocol buffer package.
It is generated from these files: It is generated from these files:
quete.proto quete.proto
response.proto
sendMail.proto sendMail.proto
It has these top-level messages: It has these top-level messages:
Quete Quete
Response
SendMail SendMail
*/ */
package beans package beans
......
// Code generated by protoc-gen-go.
// source: response.proto
// DO NOT EDIT!
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
type Response struct {
Status bool `protobuf:"varint,1,opt,name=Status" json:"Status"`
Channel string `protobuf:"bytes,2,opt,name=Channel" json:"Channel"`
Message string `protobuf:"bytes,3,opt,name=Message" json:"Message"`
}
func (m *Response) Reset() { *m = Response{} }
func (m *Response) String() string { return proto.CompactTextString(m) }
func (*Response) ProtoMessage() {}
func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
func (m *Response) GetStatus() bool {
if m != nil {
return m.Status
}
return false
}
func (m *Response) GetChannel() string {
if m != nil {
return m.Channel
}
return ""
}
func (m *Response) GetMessage() string {
if m != nil {
return m.Message
}
return ""
}
func init() {
proto.RegisterType((*Response)(nil), "beans.Response")
}
func init() { proto.RegisterFile("response.proto", fileDescriptor1) }
var fileDescriptor1 = []byte{
// 112 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x4a, 0x2d, 0x2e,
0xc8, 0xcf, 0x2b, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc,
0x2b, 0x56, 0x0a, 0xe3, 0xe2, 0x08, 0x82, 0x4a, 0x08, 0x89, 0x71, 0xb1, 0x05, 0x97, 0x24, 0x96,
0x94, 0x16, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x04, 0x41, 0x79, 0x42, 0x12, 0x5c, 0xec, 0xce,
0x19, 0x89, 0x79, 0x79, 0xa9, 0x39, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x30, 0x2e, 0x48,
0xc6, 0x37, 0xb5, 0xb8, 0x38, 0x31, 0x3d, 0x55, 0x82, 0x19, 0x22, 0x03, 0xe5, 0x26, 0xb1, 0x81,
0x6d, 0x31, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x6f, 0xd0, 0xdf, 0xca, 0x77, 0x00, 0x00, 0x00,
}
syntax="proto3";
package beans;
message Response {
bool Status = 1; //`json:"Status"`
string Channel = 2; //`json:"Channel"`
string Message = 3; //`json:"Message"`
}
\ No newline at end of file
...@@ -23,7 +23,7 @@ type SendMail struct { ...@@ -23,7 +23,7 @@ type SendMail struct {
func (m *SendMail) Reset() { *m = SendMail{} } func (m *SendMail) Reset() { *m = SendMail{} }
func (m *SendMail) String() string { return proto.CompactTextString(m) } func (m *SendMail) String() string { return proto.CompactTextString(m) }
func (*SendMail) ProtoMessage() {} func (*SendMail) ProtoMessage() {}
func (*SendMail) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} } func (*SendMail) Descriptor() ([]byte, []int) { return fileDescriptor2, []int{0} }
func (m *SendMail) GetTo() string { func (m *SendMail) GetTo() string {
if m != nil { if m != nil {
...@@ -57,9 +57,9 @@ func init() { ...@@ -57,9 +57,9 @@ func init() {
proto.RegisterType((*SendMail)(nil), "beans.SendMail") proto.RegisterType((*SendMail)(nil), "beans.SendMail")
} }
func init() { proto.RegisterFile("sendMail.proto", fileDescriptor1) } func init() { proto.RegisterFile("sendMail.proto", fileDescriptor2) }
var fileDescriptor1 = []byte{ var fileDescriptor2 = []byte{
// 116 bytes of a gzipped FileDescriptorProto // 116 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x2b, 0x4e, 0xcd, 0x4b, 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, 0xf1, 0x4d, 0xcc, 0xcc, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4d, 0x4a, 0x4d, 0xcc,
......
package controllers
import (
"fmt"
"log"
"github.com/bitly/go-nsq"
"github.com/teed7334-restore/counter/env"
"github.com/gin-gonic/gin"
)
var cfg = env.GetEnv()
//將要執行的排程寫到MQ postMessage
func postMessage(channel string, message string) {
config := nsq.NewConfig()
w, _ := nsq.NewProducer(cfg.Message.Post.Address, config)
err := w.Publish(channel, []byte(message))
if err != nil {
fmt.Println(err)
}
w.Stop()
}
//getParams 取得HTTP POST帶過來之參數
func getParams(c *gin.Context, params interface{}) {
err := c.BindJSON(params)
if err != nil {
log.Println(err)
}
}
...@@ -2,39 +2,21 @@ package controllers ...@@ -2,39 +2,21 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"fmt"
"log"
"net/http" "net/http"
"github.com/teed7334-restore/counter/beans" "github.com/teed7334-restore/counter/beans"
"github.com/teed7334-restore/counter/env"
"github.com/bitly/go-nsq"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var cfg = env.GetEnv()
func postMessage(message string) {
config := nsq.NewConfig()
w, _ := nsq.NewProducer(cfg.Message.Post.Address, config)
err := w.Publish("Mail", []byte(message))
if err != nil {
fmt.Println(err)
}
w.Stop()
}
//SendMail 寄信用API //SendMail 寄信用API
func SendMail(c *gin.Context) { func SendMail(c *gin.Context) {
sendMail := &beans.SendMail{} params := &beans.SendMail{}
err := c.BindJSON(sendMail) getParams(c, params)
if err != nil { json, _ := json.Marshal(params)
log.Println(err) channel := "Mail"
} message := "Mail/SendMail</UseService>" + string(json)
json, _ := json.Marshal(sendMail) postMessage(channel, message)
quete := "Mail/SendMail</UseService>" + string(json) response := &beans.Response{Status: true, Channel: channel, Message: message}
postMessage(quete) c.JSON(http.StatusOK, response)
c.JSON(http.StatusOK, gin.H{"status": "true"})
} }
package controllers
import (
"encoding/json"
"net/http"
"github.com/gin-gonic/gin"
"github.com/teed7334-restore/counter/beans"
homekeeper "github.com/teed7334-restore/homekeeper/beans"
)
//UploadDailyPunchclockData 將每天員工打卡資料上鏈
func UploadDailyPunchclockData(c *gin.Context) {
params := &homekeeper.DailyPunchclockData{}
getParams(c, params)
json, _ := json.Marshal(params)
message := "PunchClock/UploadDailyPunchclockData</UseService>" + string(json)
channel := "PunchClock"
postMessage(channel, message)
response := &beans.Response{Status: true, Channel: channel, Message: message}
c.JSON(http.StatusOK, response)
}
...@@ -43,11 +43,23 @@ func messageService() { ...@@ -43,11 +43,23 @@ func messageService() {
//InitConsumer 初始化消費者 //InitConsumer 初始化消費者
func InitConsumer(topic string, channel string, host string) bool { func InitConsumer(topic string, channel string, host string) bool {
interval := time.Second * 2
upStream := make(chan int, 1) upStream := make(chan int, 1)
go func() {
interval := time.Second * 10
mQuery(host, "PunchClock", "UploadDailyPunchclockData", interval)
}()
go func() {
interval := time.Second * 2
mQuery(host, "Mail", "SendMail", interval)
}()
<-upStream
return true
}
func mQuery(host string, topic string, channel string, interval time.Duration) *nsq.Consumer {
config := nsq.NewConfig() config := nsq.NewConfig()
config.LookupdPollInterval = interval config.LookupdPollInterval = interval
query, err := nsq.NewConsumer("Mail", "SendMail", config) query, err := nsq.NewConsumer(topic, channel, config)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
...@@ -57,11 +69,11 @@ func InitConsumer(topic string, channel string, host string) bool { ...@@ -57,11 +69,11 @@ func InitConsumer(topic string, channel string, host string) bool {
runServices(quete) runServices(quete)
return nil return nil
})) }))
if err = query.ConnectToNSQLookupd(host); err != nil { err = query.ConnectToNSQLookupd(host)
if err != nil {
panic(err) panic(err)
} }
<-upStream return query
return true
} }
func getQuete(message string) []string { func getQuete(message string) []string {
......
...@@ -15,5 +15,6 @@ func API() *gin.Engine { ...@@ -15,5 +15,6 @@ func API() *gin.Engine {
gin.SetMode(cfg.Env) gin.SetMode(cfg.Env)
route := gin.Default() route := gin.Default()
route.POST("/Mail/SendMail", controllers.SendMail) route.POST("/Mail/SendMail", controllers.SendMail)
route.POST("/PunchClock/UploadDailyPunchclockData", controllers.UploadDailyPunchclockData)
return route 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