p3: search API, pinned sources, cache, CLI
POST /v1/search and /v2/search fan in SearXNG, Brave LLM Context, and Tavily from config-fixed endpoints. Public sources refuse private A records; SearXNG is a loopback-only client, not the fetch guard. Cache keys include caller class. Restricted callers search like the fleet; logs omit query, hosts, URLs, and upstream errors. Degraded results are not stored. Tavily sends search_depth=basic and drops answer. Missing keys surface in /healthz. Default-on sources stay off until the bake-off. Query harvest is not in this commit.
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func searchMain() {
|
||||
fs := flag.NewFlagSet("search", flag.ContinueOnError)
|
||||
fs.SetOutput(io.Discard)
|
||||
k := fs.Int("k", 0, "result cap")
|
||||
asJSON := fs.Bool("json", false, "native envelope")
|
||||
args := os.Args[1:]
|
||||
flagArgs, pos, err := splitAroundPositional(args, map[string]bool{"k": true})
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
if err := fs.Parse(flagArgs); err != nil {
|
||||
fmt.Fprintln(os.Stderr, searchUsage)
|
||||
os.Exit(2)
|
||||
}
|
||||
if len(pos) != 1 {
|
||||
fmt.Fprintln(os.Stderr, searchUsage)
|
||||
os.Exit(2)
|
||||
}
|
||||
query := pos[0]
|
||||
base, key, err := loadClient()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(5)
|
||||
}
|
||||
body := map[string]any{"query": query}
|
||||
if *k > 0 {
|
||||
body["k"] = *k
|
||||
}
|
||||
raw, _ := json.Marshal(body)
|
||||
req, err := http.NewRequest(http.MethodPost, base+"/v1/search", bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(5)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+key)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(5)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var env map[string]any
|
||||
if err := json.NewDecoder(resp.Body).Decode(&env); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(5)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
msg, _ := env["error"].(string)
|
||||
fmt.Fprintln(os.Stderr, msg)
|
||||
os.Exit(exitFor(resp.StatusCode, msg))
|
||||
}
|
||||
if *asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
_ = enc.Encode(env)
|
||||
return
|
||||
}
|
||||
results, _ := env["results"].([]any)
|
||||
for i, it := range results {
|
||||
m, _ := it.(map[string]any)
|
||||
if i > 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
fmt.Println(m["title"])
|
||||
fmt.Println(m["url"])
|
||||
if sn, _ := m["snippet"].(string); sn != "" {
|
||||
fmt.Println(sn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const searchUsage = `usage: sift search "<query>" [--k N] [--json]`
|
||||
Reference in New Issue
Block a user