-
Notifications
You must be signed in to change notification settings - Fork 62
/
httpsvr.go
36 lines (29 loc) · 815 Bytes
/
httpsvr.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
package main
import (
"encoding/base64"
"log"
"net/http"
"github.com/vladimirvivien/automi/stream"
)
// HTTP example that returns a base64 encoding of http Body.
// Start server: go run httpsvr.go
// Test with: curl -d "Hello World" http://127.0.0.1:4040/
func main() {
http.HandleFunc(
"/",
func(resp http.ResponseWriter, req *http.Request) {
resp.Header().Add("Content-Type", "text/html")
resp.WriteHeader(http.StatusOK)
strm := stream.New(req.Body)
strm.Process(func(data []byte) string {
return base64.StdEncoding.EncodeToString(data)
}).Into(resp)
if err := <-strm.Open(); err != nil {
resp.WriteHeader(http.StatusInternalServerError)
log.Printf("Stream error: %s", err)
}
},
)
log.Println("Server listening on :4040")
http.ListenAndServe(":4040", nil)
}