- commit
- fbe0fc4902b9b08ee230509ee72ea44820c72d69
- parent
- c63eace8a2e871dddfa40a3d1da23dfa69b698ff
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2025-06-13 13:45
refactor: switch position of get and post in code
Diffstat
| M | via.go | 66 | ++++++++++++++++++++++++++++++------------------------------ |
1 files changed, 33 insertions, 33 deletions
diff --git a/via.go b/via.go
@@ -181,39 +181,6 @@ func popChannel(key string, ch chan Msg) {
181 181 }
182 182 }
183 183
184 -1 func post(w http.ResponseWriter, r *http.Request) {
185 -1 body, err := io.ReadAll(r.Body)
186 -1 if err != nil {
187 -1 log.Println("error reading request body:", err)
188 -1 http.Error(w, "Internal Server Error", http.StatusInternalServerError)
189 -1 return
190 -1 }
191 -1
192 -1 mux.Lock()
193 -1 topic, exists := topics[r.URL.Path]
194 -1 mux.Unlock()
195 -1
196 -1 response := make(map[string]int)
197 -1 defer func() {
198 -1 w.Header().Set("Content-Type", "application/json")
199 -1 json.NewEncoder(w).Encode(response)
200 -1 }()
201 -1
202 -1 if !exists {
203 -1 return
204 -1 }
205 -1
206 -1 topic.Lock()
207 -1 defer topic.Unlock()
208 -1
209 -1 topic.post(body)
210 -1
211 -1 if topic.hasHistory {
212 -1 topic.storeHistory()
213 -1 response["historyRemaining"] = maxHistorySize - len(topic.history)
214 -1 }
215 -1 }
216 -1
217 184 func get(w http.ResponseWriter, r *http.Request) {
218 185 lastId, err := strconv.Atoi(r.Header.Get("Last-Event-ID"))
219 186 if err != nil {
@@ -255,6 +222,39 @@ func get(w http.ResponseWriter, r *http.Request) {
255 222 }
256 223 }
257 224
-1 225 func post(w http.ResponseWriter, r *http.Request) {
-1 226 body, err := io.ReadAll(r.Body)
-1 227 if err != nil {
-1 228 log.Println("error reading request body:", err)
-1 229 http.Error(w, "Internal Server Error", http.StatusInternalServerError)
-1 230 return
-1 231 }
-1 232
-1 233 mux.Lock()
-1 234 topic, exists := topics[r.URL.Path]
-1 235 mux.Unlock()
-1 236
-1 237 response := make(map[string]int)
-1 238 defer func() {
-1 239 w.Header().Set("Content-Type", "application/json")
-1 240 json.NewEncoder(w).Encode(response)
-1 241 }()
-1 242
-1 243 if !exists {
-1 244 return
-1 245 }
-1 246
-1 247 topic.Lock()
-1 248 defer topic.Unlock()
-1 249
-1 250 topic.post(body)
-1 251
-1 252 if topic.hasHistory {
-1 253 topic.storeHistory()
-1 254 response["historyRemaining"] = maxHistorySize - len(topic.history)
-1 255 }
-1 256 }
-1 257
258 258 func put(w http.ResponseWriter, r *http.Request) {
259 259 if !hasHistory(r.URL.Path) {
260 260 http.Error(w, "No history", http.StatusBadRequest)