-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (70 loc) · 1.63 KB
/
main.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
package main
import (
"github.com/jawher/mow.cli"
"net/http"
"log"
"os"
"fmt"
)
func getAddr(host,port string)string{
return fmt.Sprintf("%s:%s",host,port)
}
func main() {
hostname,_:=os.Hostname()
log.Println(hostname)
app := cli.App("tundra","HTTP-MQTT bridge")
cmd := app.Cmd
port := cmd.String(cli.StringOpt{
Name: "port",
Desc: "port to listen on!",
Value: "3000",
EnvVar: "TUNDRA_PORT",
})
mqtt_user := cmd.String(cli.StringOpt{
Name: "mqtt-user",
Desc: "username of mqtt broker",
Value: "",
EnvVar: "TUNDRA_MQTT_USER",
})
mqtt_port := cmd.String(cli.StringOpt{
Name: "mqtt-port",
Desc: "mqtt broker port",
Value: "",
EnvVar: "TUNDRA_MQTT_PORT",
})
mqtt_pwd := cmd.String(cli.StringOpt{
Name: "mqtt-pwd",
Desc: "mqtt broker password",
Value: "",
EnvVar: "TUNDRA_MQTT_PWD",
})
mqtt_host := cmd.String(cli.StringOpt{
Name: "mqtt-host",
Desc: "MQTT broker host",
Value: "",
EnvVar: "TUNDRA_MQTT_HOST",
})
mqtt_clientId := cmd.String(cli.StringOpt{
Name: "mqtt-client-id",
Desc: "mqtt client id",
Value: hostname,
EnvVar: "TUNDRA_MQTT_USER",
})
cmd.Action = func() {
mux := http.NewServeMux()
if *mqtt_host==""{
log.Fatal("No Broker host URL specified")
}
if *mqtt_port==""{
log.Fatal("No Broker Port specified")
}
address:=getAddr(*mqtt_host,*mqtt_port)
h:=NewHandler(address,*mqtt_user,*mqtt_pwd,*mqtt_clientId)
ph := http.HandlerFunc(h.PublishHandler)
mux.Handle("/publish", ph)
log.Println("Listening on port",*port)
addr:=fmt.Sprintf("0.0.0.0:%s",*port)
http.ListenAndServe(addr, mux)
}
app.Run(os.Args)
}