- commit
- 80985760eb91605dd57c2d95adaa0a70fe799060
- parent
- 9831d3f9b359090bfe809d7abcecc81a50104f45
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2020-11-19 10:02
implement hmsg DELETE
Diffstat
| M | README.md | 3 | +++ |
| M | via.go | 31 | +++++++++++++++++++++++++++++++ |
2 files changed, 34 insertions, 0 deletions
diff --git a/README.md b/README.md
@@ -26,6 +26,9 @@ Use the `hmsg` prefix if you want to keep a history: 26 26 # POST works just as before 27 27 curl http://localhost:8001/hmsg/someid -d somedata 28 28 -1 29 # DELETE deletes the history -1 30 curl http://localhost:8001/hmsg/someid -X DELETE -1 31 29 32 # the history only keeps up to 100 entries. 30 33 # you can optimize it by replacing all entries by a single message 31 34 curl http://localhost:8001/hmsg/someid -d combined -H 'Last-Event-Id: 3' -X PUT
diff --git a/via.go b/via.go
@@ -90,6 +90,15 @@ func (topic *Topic) restoreHistory(key string) {
90 90 }
91 91 }
92 92
-1 93 func (topic *Topic) deleteHistory(key string) {
-1 94 path := getStorePath(fmt.Sprintf("%s:%s", key, topic.password))
-1 95
-1 96 err := os.Remove(path)
-1 97 if err != nil && !os.IsNotExist(err) {
-1 98 log.Println("error deleting history:", err)
-1 99 }
-1 100 }
-1 101
93 102 func (topic *Topic) post(data []byte) {
94 103 topic.lastId += 1
95 104 msg := Msg{topic.lastId, data}
@@ -306,6 +315,26 @@ func put(w http.ResponseWriter, r *http.Request) {
306 315 topic.storeHistory(key)
307 316 }
308 317
-1 318 func del(w http.ResponseWriter, r *http.Request) {
-1 319 key, password := splitPassword(r.URL.Path)
-1 320
-1 321 topic, allowed := getTopic(key, password)
-1 322
-1 323 if !allowed {
-1 324 http.Error(w, "Forbidden", http.StatusForbidden)
-1 325 return
-1 326 } else if !topic.hasHistory {
-1 327 http.Error(w, "No history", http.StatusBadRequest)
-1 328 return
-1 329 }
-1 330
-1 331 topic.Lock()
-1 332 defer topic.Unlock()
-1 333
-1 334 topic.history = make([]Msg, 0)
-1 335 topic.deleteHistory(key)
-1 336 }
-1 337
309 338 func handler(w http.ResponseWriter, r *http.Request) {
310 339 if verbose {
311 340 log.Println(r.Method, r.URL)
@@ -317,6 +346,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
317 346 post(w, r)
318 347 } else if r.Method == http.MethodPut {
319 348 put(w, r)
-1 349 } else if r.Method == http.MethodDelete {
-1 350 del(w, r)
320 351 } else {
321 352 http.Error(w, "Unsupported Method", http.StatusMethodNotAllowed)
322 353 }