Last active
January 2, 2020 18:00
-
-
Save geosoft1/77099cec979024b3999f8cbda266b4e0 to your computer and use it in GitHub Desktop.
Simple RESTful pipeline
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 ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
) | |
/* | |
curl -i -X PUT localhost:8080 -d 'a' | |
curl -i -X GET localhost:8080 | |
*/ | |
var messages = make(chan string, 3) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Println(r.Method, r.Host) | |
switch r.Method { | |
case "GET": | |
io.WriteString(w, <-messages) | |
case "PUT": | |
body, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
return | |
} | |
messages <- string(body) | |
} | |
}) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment