─── Host handlers ────────────────────────────────────────────────────────────
(w http.ResponseWriter, r *http.Request)
| 181 | // ─── Host handlers ──────────────────────────────────────────────────────────── |
| 182 | |
| 183 | func (a *inboundAPI) handleHosts(w http.ResponseWriter, r *http.Request) { |
| 184 | switch r.Method { |
| 185 | case http.MethodGet: |
| 186 | inboundID := r.URL.Query().Get("inbound_id") |
| 187 | var items []inbounds.Host |
| 188 | var err error |
| 189 | if inboundID != "" { |
| 190 | items, err = a.store.ListHostsByInbound(inboundID) |
| 191 | } else { |
| 192 | items, err = a.store.ListHosts() |
| 193 | } |
| 194 | if err != nil { |
| 195 | internalError(w, r, err) |
| 196 | return |
| 197 | } |
| 198 | writeJSON(w, http.StatusOK, map[string]any{"hosts": items}) |
| 199 | case http.MethodPost: |
| 200 | var req inbounds.Host |
| 201 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 202 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "invalid json body"}) |
| 203 | return |
| 204 | } |
| 205 | if req.InboundID == "" || req.Address == "" { |
| 206 | writeJSON(w, http.StatusBadRequest, map[string]any{"error": "inbound_id and address are required"}) |
| 207 | return |
| 208 | } |
| 209 | if _, err := a.store.GetInbound(req.InboundID); err != nil { |
| 210 | writeInboundError(w, err) |
| 211 | return |
| 212 | } |
| 213 | if req.ID == "" { |
| 214 | req.ID = idgen.NextString() |
| 215 | } |
| 216 | if req.Security == "" { |
| 217 | req.Security = "none" |
| 218 | } |
| 219 | trimHost(&req) |
| 220 | item, err := a.store.UpsertHost(req) |
| 221 | if err != nil { |
| 222 | internalError(w, r, err) |
| 223 | return |
| 224 | } |
| 225 | a.triggerHostNodeSync(item) |
| 226 | writeJSON(w, http.StatusOK, item) |
| 227 | default: |
| 228 | writeMethodNotAllowed(w, http.MethodGet+", "+http.MethodPost) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | func (a *inboundAPI) handleHostRoutes(w http.ResponseWriter, r *http.Request) { |
| 233 | id := strings.TrimPrefix(r.URL.Path, "/v1/hosts/") |
nothing calls this directly
no test coverage detected