-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
106 lines (92 loc) · 2 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"github.com/sriramsv/tundra/mqtt"
"net/http"
"encoding/json"
"io/ioutil"
"log"
"fmt"
)
type ConnectHandler func()error
const SuccessStatus="SUCCESS"
const FailureStatus="FAILURE"
type Handler struct{
mqttclient *mqtt.MQTTClient
ConnectHandler ConnectHandler
}
type Payload struct {
Topic string `json:"topic"`
Message string `json:"message"`
Qos int `json:"qos"`
Retain bool `json:"retain"`
}
type Response struct {
Status string
Error string
}
func NewHandler(mqttbroker,username,password,clientID string) *Handler{
mclient:=mqtt.New()
connection:=mclient.GetConnectionHandler(mqttbroker,clientID,username,password)
err:=connection()
if err!=nil{
log.Fatalln("V:",err)
}
handler:=&Handler{
mqttclient:mclient,
ConnectHandler:connection,
}
return handler
}
func (h *Handler) PublishHandler(w http.ResponseWriter, r *http.Request){
var p Payload;
defer r.Body.Close()
if r.Method!="POST"{
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
body, err := ioutil.ReadAll(r.Body)
if err!=nil{
w.Write([]byte(err.Error()))
return
}
err = json.Unmarshal(body,&p)
if err!=nil{
w.Write([]byte(err.Error()))
return
}
resp:=*h.publishhandler(&p)
log.Println(resp)
js, err := json.Marshal(resp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(js)
}
func logHandler(p Payload){
log.Println(fmt.Sprintf("Received message:(%s) on topic:(%s) [QOS:%d,retain:%v]",p.Message,p.Topic,p.Qos,p.Retain))
}
func (h *Handler) publishhandler( p *Payload)(*Response){
payload:=*p
logHandler(payload)
resp:=new(Response)
if payload.Topic==""{
resp.Error="topic is empty"
resp.Status=FailureStatus
return resp
}
if payload.Message==""{
resp.Error="message is empty"
resp.Status=FailureStatus
return resp
}
err:=h.mqttclient.Publish(p.Topic,p.Message,p.Retain,p.Qos)
if err!=nil{
resp.Error=err.Error()
resp.Status=FailureStatus
} else{
resp.Error=""
resp.Status=SuccessStatus
}
return resp
}