package main import ( "fmt" "log" "net/http" "strings" "context" "strconv" "encoding/json" "github.com/jackc/pgx/v4" ) func enableCors(w *http.ResponseWriter) { (*w).Header().Set("Access-Control-Allow-Origin", "*") } func search(w http.ResponseWriter, r *http.Request) { enableCors(&w) searchkeys, exists_search := r.URL.Query()["s"] offsetkeys, exists_offset := r.URL.Query()["o"] if strings.Contains(r.Header.Get("Accept"), "application/json") { log.Print("Treat as an Json") } else { log.Println("Treat as HTML") } var err error var rows pgx.Rows var searchKey string if exists_search { searchKey = searchkeys[0] } var offsetKey int if exists_offset { offsetKey, _ = strconv.Atoi(offsetkeys[0]) } else { offsetKey = -1 } if exists_search && searchKey != "" { if offsetKey == -1 { rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.identifiedat DESC LIMIT 10", searchKey) } else { rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' WHERE activities.normalized_tsvector @@ plainto_tsquery($1) ORDER BY activities.identifiedat DESC LIMIT 10 OFFSET $2", searchKey, offsetKey) } } else { if offsetKey == -1 { rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.identifiedat DESC LIMIT 10") } else { rows, err = pool.Query(context.Background(), "SELECT activities.id, activities.document, actors.document FROM activities as activities INNER JOIN actors as actors ON activities.document->>'actor' = actors.document->>'id' ORDER BY activities.identifiedat DESC OFFSET $2 LIMIT 10 FETCH FIRST", offsetKey) } } if err != nil { panic(err) } defer rows.Close() var lastid int lastid = 0 var activitiesJson []map[string]json.RawMessage for rows.Next() { var id int var activityRaw string var actorRaw string var activityJson map[string]json.RawMessage err = rows.Scan(&id, &activityRaw, &actorRaw) if err != nil { panic(err) } err := json.Unmarshal([]byte(activityRaw), &activityJson) if err != nil { fmt.Println(err) } if lastid == 0 { lastid = id } else if lastid > id { lastid = id } activityJson["actor"] = json.RawMessage(actorRaw) activitiesJson = append(activitiesJson, activityJson) } requestData := make(map[string]int) requestData["lastid"] = lastid requestData["total_results"] = 9999 totalJson := make(map[string]interface{} ) totalJson["requestdata"] = requestData totalJson["activities"] = activitiesJson data, err := json.Marshal(totalJson) if err != nil { log.Fatal("error marshaling combined activity: %v", err) } fmt.Fprintf(w, "%s", data) } func main() { pool = getDbPool() http.HandleFunc("/search", search) log.Print("Starting HTTP inbox on port 6432") log.Fatal(http.ListenAndServe("0.0.0.0:6432", nil)) }