via

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

commit
c63eace8a2e871dddfa40a3d1da23dfa69b698ff
parent
7a92a3cf56fc3ad6341ecd38e9ec91eb3f28f411
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-06-13 13:33
refactor: store path on topic

Diffstat

M via.go 32 +++++++++++++-------------------

1 files changed, 13 insertions, 19 deletions


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

@@ -31,6 +31,7 @@ type Topic struct {
   31    31 	channels   map[chan Msg]bool
   32    32 	hasHistory bool
   33    33 	history    []Msg
   -1    34 	path       string
   34    35 	lastId     int
   35    36 }
   36    37 
@@ -40,34 +41,26 @@ var verbose = false
   40    41 var maxHistorySize = 100
   41    42 var dir = ""
   42    43 
   43    -1 func getStorePath(key string) string {
   44    -1 	hash := base64.URLEncoding.EncodeToString([]byte(key))
   45    -1 	return path.Join(dir, hash)
   46    -1 }
   47    -1 
   48    44 func hasHistory(key string) bool {
   49    45 	return strings.HasPrefix(key, "/hmsg/")
   50    46 }
   51    47 
   52    -1 func (topic *Topic) storeHistory(key string) {
   -1    48 func (topic *Topic) storeHistory() {
   53    49 	content, err := json.Marshal(topic.history)
   54    50 	if err != nil {
   55    51 		log.Println("error storing history:", err)
   56    52 		return
   57    53 	}
   58    54 
   59    -1 	path := getStorePath(key)
   60    -1 	err = os.WriteFile(path, content, 0644)
   -1    55 	err = os.WriteFile(topic.path, content, 0644)
   61    56 	if err != nil {
   62    57 		log.Println("error storing history:", err)
   63    58 		return
   64    59 	}
   65    60 }
   66    61 
   67    -1 func (topic *Topic) restoreHistory(key string) {
   68    -1 	path := getStorePath(key)
   69    -1 
   70    -1 	content, err := os.ReadFile(path)
   -1    62 func (topic *Topic) restoreHistory() {
   -1    63 	content, err := os.ReadFile(topic.path)
   71    64 	if err != nil {
   72    65 		if !errors.Is(err, os.ErrNotExist) {
   73    66 			log.Println("error restoring history:", err)
@@ -88,9 +81,8 @@ func (topic *Topic) restoreHistory(key string) {
   88    81 	}
   89    82 }
   90    83 
   91    -1 func (topic *Topic) deleteHistory(key string) {
   92    -1 	path := getStorePath(key)
   93    -1 	err := os.Remove(path)
   -1    84 func (topic *Topic) deleteHistory() {
   -1    85 	err := os.Remove(topic.path)
   94    86 	if err != nil && !os.IsNotExist(err) {
   95    87 		log.Println("error deleting history:", err)
   96    88 	}
@@ -138,14 +130,16 @@ func getTopic(key string) *Topic {
  138   130 	topic, exists := topics[key]
  139   131 
  140   132 	if !exists {
   -1   133 		filename := base64.URLEncoding.EncodeToString([]byte(key))
  141   134 		topic = &Topic{
  142   135 			channels:   make(map[chan Msg]bool, 0),
  143   136 			hasHistory: hasHistory(key),
  144   137 			history:    make([]Msg, 0),
   -1   138 			path:       path.Join(dir, filename),
  145   139 			lastId:     0,
  146   140 		}
  147   141 		if topic.hasHistory {
  148    -1 			topic.restoreHistory(key)
   -1   142 			topic.restoreHistory()
  149   143 		}
  150   144 		topics[key] = topic
  151   145 	}
@@ -215,7 +209,7 @@ func post(w http.ResponseWriter, r *http.Request) {
  215   209 	topic.post(body)
  216   210 
  217   211 	if topic.hasHistory {
  218    -1 		topic.storeHistory(r.URL.Path)
   -1   212 		topic.storeHistory()
  219   213 		response["historyRemaining"] = maxHistorySize - len(topic.history)
  220   214 	}
  221   215 }
@@ -286,7 +280,7 @@ func put(w http.ResponseWriter, r *http.Request) {
  286   280 	defer topic.Unlock()
  287   281 
  288   282 	topic.put(body, lastId)
  289    -1 	topic.storeHistory(r.URL.Path)
   -1   283 	topic.storeHistory()
  290   284 }
  291   285 
  292   286 func del(w http.ResponseWriter, r *http.Request) {
@@ -302,7 +296,7 @@ func del(w http.ResponseWriter, r *http.Request) {
  302   296 
  303   297 	topic.history = make([]Msg, 0)
  304   298 	topic.lastId = 0
  305    -1 	topic.deleteHistory(r.URL.Path)
   -1   299 	topic.deleteHistory()
  306   300 }
  307   301 
  308   302 func handler(w http.ResponseWriter, r *http.Request) {