-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
executable file
·57 lines (51 loc) · 1.19 KB
/
server.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
package main
import (
"io/ioutil"
"fmt"
"log"
"os"
"strings"
"net/http"
)
var entries = []string{}
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == "" {
return "", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}
func main() {
addr, err := determineListenAddress()
if err != nil {
log.Printf("err is not nil !")
}
http.HandleFunc("/", handler)
http.ListenAndServe(addr, nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
//serve the resource
fmt.Fprintf(w, "<table><tr><th>Time</th><th>Action</th><th>Data</th></tr>")
for i, _ := range entries {
fmt.Fprintf(w, "%s", entries[len(entries) - i - 1])
}
fmt.Fprintf(w, "</table>")
case "POST":
//add entry
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, err.Error())
}
entry := strings.SplitN(string(body), "|", 3)
new_entry := fmt.Sprintf("<tr><td>%s</td><td>%s</td> <td>%s</td></tr>", entry[0], entry[1], entry[2])
entries = append(entries, new_entry)
if len(entries) > 100 {
entries = entries[1:]
}
fmt.Fprintf(w, "POST\n")
default:
//do nothing
}
}