-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·46 lines (39 loc) · 1.23 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
// GO Main
// https://github.com/infchg/j2/
package main
import (
"log"
"fmt"
"net/http"
"os"
)
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == "" {
return "", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}
func hola(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "<h1> This is the human readable response </h1> The J2 app can respond to this <a href=/json>/json</a> url with HTTP JSON data. Deployed Probes and Sensors push data to <a href=/write>/write</a> <img src=d3diagram.png > ")
}
func jso(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{\"response\":\"this is J2 http response\"}")
}
func hstat(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "<a href=/>Home</a></br> The /write URL is for receiving updates via http from Deployed Sensors in the field")
}
func main() {
addr, err := determineListenAddress()
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/write", hstat)
http.HandleFunc("/index.html", hola)
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/json", jso)
log.Printf("Listening on %s...\n", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
panic(err)
}
}