publishHandler reads the request body with a limit of 8192 bytes and then publishes the received message.
(w http.ResponseWriter, r *http.Request)
| 87 | // publishHandler reads the request body with a limit of 8192 bytes and then publishes |
| 88 | // the received message. |
| 89 | func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) { |
| 90 | if r.Method != "POST" { |
| 91 | http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) |
| 92 | return |
| 93 | } |
| 94 | body := http.MaxBytesReader(w, r.Body, 8192) |
| 95 | msg, err := io.ReadAll(body) |
| 96 | if err != nil { |
| 97 | http.Error(w, http.StatusText(http.StatusRequestEntityTooLarge), http.StatusRequestEntityTooLarge) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | cs.publish(msg) |
| 102 | |
| 103 | w.WriteHeader(http.StatusAccepted) |
| 104 | } |
| 105 | |
| 106 | // subscribe subscribes the given WebSocket to all broadcast messages. |
| 107 | // It creates a subscriber with a buffered msgs chan to give some room to slower |