Created
June 18, 2018 03:18
-
-
Save dbjpanda/c51c0360a67c4b647c079320dc84eea6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"fmt" | |
"strings" | |
"net/http" | |
) | |
var entries = []string{} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":9000", nil) | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
switch r.Method { | |
case "GET": | |
//serve the resource | |
fmt.Fprintf(w, "<table><tr><th>Timestamp</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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment