via

Simple pubsub server inspired by https://patchbay.pub/
git clone https://git.ce9e.org/via.git

commit
ca8acff929cbfeb65d38833ba4b56cd748e12188
parent
1c7b58065e33680f5961a1cc38d15cdffaa070c2
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-10-15 08:02
replay history on connect

Diffstat

M via.go 20 ++++++++++++++++++--

1 files changed, 18 insertions, 2 deletions


diff --git a/via.go b/via.go

@@ -9,6 +9,7 @@ import (
    9     9 	"net/http"
   10    10 	"os"
   11    11 	"path"
   -1    12 	"strconv"
   12    13 	"strings"
   13    14 	"sync"
   14    15 	"time"
@@ -47,7 +48,7 @@ func getStorePath(key string) string {
   47    48 	return path.Join(dir, hash)
   48    49 }
   49    50 
   50    -1 func pushChannel(key string, password string, ch chan Msg) bool {
   -1    51 func pushChannel(key string, password string, ch chan Msg, lastId int) bool {
   51    52 	mux.RLock()
   52    53 	topic, ok := topics[key]
   53    54 	mux.RUnlock()
@@ -71,6 +72,16 @@ func pushChannel(key string, password string, ch chan Msg) bool {
   71    72 	topic.channels[ch] = true
   72    73 	topic.Unlock()
   73    74 
   -1    75 	if topic.hasHistory {
   -1    76 		go func(t Topic) {
   -1    77 			for _, msg := range t.history {
   -1    78 				if msg.Id > lastId {
   -1    79 					ch <- msg
   -1    80 				}
   -1    81 			}
   -1    82 		}(*topic)
   -1    83 	}
   -1    84 
   74    85 	return true
   75    86 }
   76    87 
@@ -136,8 +147,13 @@ func post(w http.ResponseWriter, r *http.Request) {
  136   147 func get(w http.ResponseWriter, r *http.Request) {
  137   148 	key, password := splitPassword(r.URL.Path)
  138   149 
   -1   150 	lastId, err := strconv.Atoi(r.Header.Get("Last-Event-ID"))
   -1   151 	if err != nil {
   -1   152 		lastId = 0
   -1   153 	}
   -1   154 
  139   155 	ch := make(chan Msg)
  140    -1 	allowed := pushChannel(key, password, ch)
   -1   156 	allowed := pushChannel(key, password, ch, lastId)
  141   157 	if !allowed {
  142   158 		http.Error(w, "Forbidden", http.StatusForbidden)
  143   159 		return