MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / handleHosts

Method handleHosts

internal/serverapi/inbounds.go:183–230  ·  view source on GitHub ↗

─── Host handlers ────────────────────────────────────────────────────────────

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

181// ─── Host handlers ────────────────────────────────────────────────────────────
182
183func (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
232func (a *inboundAPI) handleHostRoutes(w http.ResponseWriter, r *http.Request) {
233 id := strings.TrimPrefix(r.URL.Path, "/v1/hosts/")

Callers

nothing calls this directly

Calls 13

triggerHostNodeSyncMethod · 0.95
NextStringFunction · 0.92
internalErrorFunction · 0.85
writeInboundErrorFunction · 0.85
trimHostFunction · 0.85
writeMethodNotAllowedFunction · 0.85
writeJSONFunction · 0.70
GetMethod · 0.65
QueryMethod · 0.65
ListHostsByInboundMethod · 0.65
ListHostsMethod · 0.65
GetInboundMethod · 0.65

Tested by

no test coverage detected