blob: 6ff3bb16ff2b9d862dd5cafea5c3e27ecbaa3eea (
plain)
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
|
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"go.wit.com/log"
)
// remove '?' part and trailing '/'
func cleanURL(url string) string {
url = "/" + strings.Trim(url, "/")
return url
}
func okHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Got URL Path: ", r.URL.Path)
route := cleanURL(r.URL.Path)
hostname := r.URL.Query().Get("hostname")
msg, err := ioutil.ReadAll(r.Body) // Read the body as []byte
if err != nil {
log.Info("ReadAll() error =", err)
return
}
if route == "/" {
return
}
if route == "/status" {
if hostname == "" {
// ignore junk
log.Info("hostname was blank")
return
}
log.Info("Got URL msg length:", len(msg))
return
}
log.Warn("BAD URL =", route)
}
// starts and sits waiting for HTTP requests
func startHTTP() {
http.HandleFunc("/", okHandler)
p := fmt.Sprintf(":%d", argv.Port)
log.Println("Running on port", p)
err := http.ListenAndServe(p, nil)
if err != nil {
log.Println("Error starting server:", err)
}
}
|