OOKNET                             [ /  search the index  ]  
──────────────────────────────────────────────────────────────────────────────────────
══════════════════════════════════════════════════════════════════════════════════════
OOKNET   [ /  search  ]  
────────────────────────────────────────────────
════════════════════════════════════════════════
 
 
master @ 62 LINES
 
[ HISTORY ]  [ UP ]
 

import type { APIRoute } from "astro";
import { buildStaticSearchIndexPath } from "../../../lib/utils";

export const GET: APIRoute = async ({ url }) => {
  try {
    console.log("[INFO] Player search index API called!");
    const urlParams = url.searchParams;
    const shard = parseInt(urlParams.get("shard") || "1"); // Default to shard 1

    console.log(`[INFO] Request URL: ${url.toString()}`);
    console.log(`[INFO] Parameters: shard=${shard}`);

    // Load from static search index files (generated by Go)
    const staticPath = buildStaticSearchIndexPath(shard);
    const response = await fetch(`${url.origin}${staticPath}`);

    if (!response.ok) {
      if (response.status === 404) {
        return new Response(
          JSON.stringify({
            error: "Search shard not found",
            available_shards:
              "Check /api/search/players-001.json, players-002.json, etc.",
          }),
          {
            status: 404,
            headers: { "Content-Type": "application/json" },
          },
        );
      } else {
        throw new Error(`Failed to load search shard: ${response.statusText}`);
      }
    }

    const data = await response.json();

    console.log(
      `[INFO] Successfully loaded search index shard ${shard} with ${data.players?
.length || 0} players`,
    );

    return new Response(JSON.stringify(data), {
      status: 200,
      headers: {
        "Content-Type": "application/json",
        "Cache-Control": "public, max-age=7200, stale-while-revalidate=3600", // C
ache 2 hours, stale 1 hour
      },
    });
  } catch (error) {
    console.error("Player search index API error:", error);
    return new Response(
      JSON.stringify({
        error: "Internal server error",
        message: error instanceof Error ? error.message : "Unknown error",
      }),
      {
        status: 500,
        headers: { "Content-Type": "application/json" },
      },
    );
  }
};

import type { APIRoute } from "astro";
import { buildStaticSearchIndexPath } from "
../../../lib/utils";

export const GET: APIRoute = async ({ url })
 => {
  try {
    console.log("[INFO] Player search index 
API called!");
    const urlParams = url.searchParams;
    const shard = parseInt(urlParams.get("sh
ard") || "1"); // Default to shard 1

    console.log(`[INFO] Request URL: ${url.t
oString()}`);
    console.log(`[INFO] Parameters: shard=${
shard}`);

    // Load from static search index files (
generated by Go)
    const staticPath = buildStaticSearchInde
xPath(shard);
    const response = await fetch(`${url.orig
in}${staticPath}`);

    if (!response.ok) {
      if (response.status === 404) {
        return new Response(
          JSON.stringify({
            error: "Search shard not found",
            available_shards:
              "Check /api/search/players-001
.json, players-002.json, etc.",
          }),
          {
            status: 404,
            headers: { "Content-Type": "appl
ication/json" },
          },
        );
      } else {
        throw new Error(`Failed to load sear
ch shard: ${response.statusText}`);
      }
    }

    const data = await response.json();

    console.log(
      `[INFO] Successfully loaded search ind
ex shard ${shard} with ${data.players?.lengt
h || 0} players`,
    );

    return new Response(JSON.stringify(data)
, {
      status: 200,
      headers: {
        "Content-Type": "application/json",
        "Cache-Control": "public, max-age=72
00, stale-while-revalidate=3600", // Cache 2
 hours, stale 1 hour
      },
    });
  } catch (error) {
    console.error("Player search index API e
rror:", error);
    return new Response(
      JSON.stringify({
        error: "Internal server error",
        message: error instanceof Error ? er
ror.message : "Unknown error",
      }),
      {
        status: 500,
        headers: { "Content-Type": "applicat
ion/json" },
      },
    );
  }
};
 
──────────────────────────────────────────────────────────────────────────────────────
OOKNET
────────────────────────────────────────────────
OOKNET