via

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

commit
1c7b58065e33680f5961a1cc38d15cdffaa070c2
parent
1d389dd00d56a1df2a9e5903173b4def51505fd8
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-10-15 08:01
keep message history

Diffstat

M via.go 9 +++++++++

1 files changed, 9 insertions, 0 deletions


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

@@ -23,6 +23,8 @@ type Topic struct {
   23    23 	sync.RWMutex
   24    24 	channels map[chan Msg]bool
   25    25 	password string
   -1    26 	hasHistory bool
   -1    27 	history []Msg
   26    28 	lastId int
   27    29 }
   28    30 
@@ -54,6 +56,8 @@ func pushChannel(key string, password string, ch chan Msg) bool {
   54    56 		topic = &Topic{
   55    57 			channels: make(map[chan Msg]bool, 0),
   56    58 			password: password,
   -1    59 			hasHistory: strings.HasPrefix(key, "/hmsg/"),
   -1    60 			history: make([]Msg, 0),
   57    61 			lastId: 0,
   58    62 		}
   59    63 		mux.Lock()
@@ -118,6 +122,10 @@ func post(w http.ResponseWriter, r *http.Request) {
  118   122 	topic.lastId += 1
  119   123 	msg := Msg{topic.lastId, body}
  120   124 
   -1   125 	if topic.hasHistory {
   -1   126 		topic.history = append(topic.history, msg)
   -1   127 	}
   -1   128 
  121   129 	for channel := range topic.channels {
  122   130 		go func(ch chan Msg) {
  123   131 			ch <- msg
@@ -197,6 +205,7 @@ func main() {
  197   205 	}
  198   206 
  199   207 	http.HandleFunc("/msg/", handler)
   -1   208 	http.HandleFunc("/hmsg/", handler)
  200   209 
  201   210 	log.Printf("Serving on http://%s", addr)
  202   211 	log.Fatal(http.ListenAndServe(addr, nil))