-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
76 lines (61 loc) · 2 KB
/
http.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
// Copyright Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
// Common support for all HTTP topic handlers
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
)
// HTTPInboundHandler kicks off inbound messages coming from all sources, then serve HTTP
func HTTPInboundHandler(port string) {
// Topics
http.HandleFunc("/github", inboundWebGithubHandler)
http.HandleFunc("/ping", inboundWebPingHandler)
http.HandleFunc("/echo", inboundWebEchoHandler)
http.HandleFunc("/audio", inboundWebAudioHandler)
http.HandleFunc("/audio/", inboundWebAudioHandler)
http.HandleFunc("/api", inboundWebAPIHandler)
http.HandleFunc("/send", inboundWebSendHandler)
http.HandleFunc("/proxy", inboundWebProxyHandler)
http.HandleFunc("/robots.txt", inboundWebPingHandler)
http.HandleFunc("/env", inboundWebEnvHandler)
http.HandleFunc("/lorawan", inboundWebLoRaWANHandler)
http.HandleFunc("/", inboundWebRootHandler)
// HTTP
fmt.Printf("Now handling inbound HTTP on %s\n", port)
go http.ListenAndServe(port, nil)
}
// HTTPArgs parses the request URI and returns interesting things
func HTTPArgs(req *http.Request, topic string) (target string, args map[string]string) {
args = map[string]string{}
// Trim the request URI
target = req.RequestURI[len(topic):]
// If nothing left, there were no args
if len(target) == 0 {
return
}
// Make sure that the prefix is "/", else the pattern matcher is matching something we don't want
target = strings.TrimPrefix(target, "/")
// See if there is a query, and if so process it
str := strings.SplitN(target, "?", 2)
if len(str) == 1 {
return
}
// Now that we know we have args, parse them
target = str[0]
values, err := url.ParseQuery(str[1])
if err != nil {
return
}
// Generate the return arg in the format we expect
for k, v := range values {
if len(v) == 1 {
args[k] = strings.TrimSuffix(strings.TrimPrefix(v[0], "\""), "\"")
}
}
// Done
return
}