- commit
- 4f0e2f4644cca27d5f3758d6adc139325bf02dd9
- parent
- 48b6f24e8ce3c4aca10381eb3f99183223cf0520
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-05-05 04:12
rm blocking get
Diffstat
| M | README.md | 6 | ++---- |
| M | via.go | 34 | ++++------------------------------ |
2 files changed, 6 insertions, 34 deletions
diff --git a/README.md b/README.md
@@ -13,11 +13,8 @@ Start the server: 13 13 14 14 Then start sending requests on the client: 15 1516 -1 # initiate GET and wait for data17 -1 curl http://localhost:8001/someid18 -119 16 # start listening for server sent event stream20 -1 curl http://localhost:8001/someid?sse-1 17 curl http://localhost:8001/someid 21 18 22 19 # POST some data 23 20 curl http://localhost:8001/someid -d somedata @@ -34,5 +31,6 @@ it at the same time: 34 31 35 32 - no support for MPMC (blocking POST) 36 33 - no support for req/res -1 34 - no support for blocking GET 37 35 - support for [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events) 38 36 - support for passwords
diff --git a/via.go b/via.go
@@ -1,11 +1,10 @@ 1 1 // Simple pubsub server inspired by https://patchbay.pub 2 2 // 3 3 // Usage: via [-v] [[host]:port]4 -1 // curl http://localhost:8001/someid # block5 -1 // curl http://localhost:8001/someid?sse # server sent event stream-1 4 // curl http://localhost:8001/someid # server sent event stream 6 5 // curl http://localhost:8001/someid -d somedata 7 6 //8 -1 // curl http://localhost:8001/someid:somepassword?sse-1 7 // curl http://localhost:8001/someid:somepassword 9 8 // curl http://localhost:8001/someid # 403 10 9 // curl http://localhost:8001/someid -d somedata # 200 11 10 package main @@ -117,28 +116,7 @@ func post(w http.ResponseWriter, r *http.Request) { 117 116 } 118 117 } 119 118120 -1 func getBlocking(w http.ResponseWriter, r *http.Request) {121 -1 key, password := splitPassword(r.URL.Path)122 -1123 -1 ch := make(chan []byte)124 -1 allowed := pushChannel(key, password, ch)125 -1 if !allowed {126 -1 http.Error(w, "Forbidden", http.StatusForbidden)127 -1 return128 -1 }129 -1 defer popChannel(key, ch)130 -1131 -1 ctx := r.Context()132 -1133 -1 select {134 -1 case <-ctx.Done():135 -1 return136 -1 case s := <-ch:137 -1 w.Write(s)138 -1 }139 -1 }140 -1141 -1 func getSse(w http.ResponseWriter, r *http.Request) {-1 119 func get(w http.ResponseWriter, r *http.Request) { 142 120 key, password := splitPassword(r.URL.Path) 143 121 144 122 ch := make(chan []byte) @@ -186,11 +164,7 @@ func handler(w http.ResponseWriter, r *http.Request) { 186 164 } 187 165 188 166 if r.Method == "GET" {189 -1 if r.URL.RawQuery == "sse" {190 -1 getSse(w, r)191 -1 } else {192 -1 getBlocking(w, r)193 -1 }-1 167 get(w, r) 194 168 } else if r.Method == "POST" { 195 169 post(w, r) 196 170 } else {