fetch tags
This commit is contained in:
parent
0517e7c2e2
commit
f6ce166b11
@ -3,6 +3,8 @@ import { serveDir, serveFile } from "@std/http/file-server";
|
|||||||
import { BearMetalStore } from "@bearmetal/store";
|
import { BearMetalStore } from "@bearmetal/store";
|
||||||
import { ensureDir, ensureFile, exists } from "@std/fs";
|
import { ensureDir, ensureFile, exists } from "@std/fs";
|
||||||
import { Router } from "./router.ts";
|
import { Router } from "./router.ts";
|
||||||
|
import { getPackVersion } from "./util/packVersion.ts";
|
||||||
|
import { createTagRoutes } from "./tags/routes.ts";
|
||||||
|
|
||||||
const installPath = Deno.env.get("BMP_INSTALL_DIR") || "./";
|
const installPath = Deno.env.get("BMP_INSTALL_DIR") || "./";
|
||||||
|
|
||||||
@ -14,6 +16,19 @@ sockpuppet.addHandler((req: Request) => {
|
|||||||
return serveFile(req, url.searchParams.get("location") as string);
|
return serveFile(req, url.searchParams.get("location") as string);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// sockpuppet.addHandler((req: Request) => {
|
||||||
|
// const method = req.method;
|
||||||
|
// if (method === "OPTIONS") {
|
||||||
|
// return new Response(null, {
|
||||||
|
// status: 200,
|
||||||
|
// headers: {
|
||||||
|
// "Access-Control-Allow-Origin": "*",
|
||||||
|
// "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
router.route("/api/dir")
|
router.route("/api/dir")
|
||||||
.get(async () => {
|
.get(async () => {
|
||||||
@ -171,11 +186,8 @@ router.route("/api/packs")
|
|||||||
router.route("/api/pack/version")
|
router.route("/api/pack/version")
|
||||||
.get(() => {
|
.get(() => {
|
||||||
using store = new BearMetalStore();
|
using store = new BearMetalStore();
|
||||||
const packMeta = Deno.readTextFileSync(
|
const version = getPackVersion(store);
|
||||||
store.get("packlocation") + "/pack.mcmeta",
|
return new Response(version.toString(), {
|
||||||
);
|
|
||||||
const packMetaJson = JSON.parse(packMeta);
|
|
||||||
return new Response(packMetaJson.pack.pack_format.toString(), {
|
|
||||||
status: 200,
|
status: 200,
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@ -210,6 +222,8 @@ router.route("/api/versions")
|
|||||||
return new Response(JSON.stringify(versions), { status: 200 });
|
return new Response(JSON.stringify(versions), { status: 200 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
createTagRoutes(router);
|
||||||
|
|
||||||
sockpuppet.addHandler((req: Request) => {
|
sockpuppet.addHandler((req: Request) => {
|
||||||
if (new URL(req.url).pathname.startsWith("/api")) return;
|
if (new URL(req.url).pathname.startsWith("/api")) return;
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
export class Router {
|
export class Router {
|
||||||
private routes: Record<string, Handler[]> = {};
|
private routes: Map<string, Handler[]> = new Map();
|
||||||
|
|
||||||
public route(route: string) {
|
public route(route: string) {
|
||||||
const methods: Record<string, Handler> = {
|
const methods: Record<string, Handler> = {
|
||||||
@ -8,8 +8,8 @@ export class Router {
|
|||||||
put: () => undefined,
|
put: () => undefined,
|
||||||
delete: () => undefined,
|
delete: () => undefined,
|
||||||
};
|
};
|
||||||
this.routes[route] = this.routes[route] || [];
|
this.routes.set(route, this.routes.get(route) || []);
|
||||||
this.routes[route].push((r, c) => {
|
this.routes.get(route)?.push((r, c) => {
|
||||||
switch (r.method) {
|
switch (r.method) {
|
||||||
case "GET":
|
case "GET":
|
||||||
return methods.get?.(r, c);
|
return methods.get?.(r, c);
|
||||||
@ -45,16 +45,20 @@ export class Router {
|
|||||||
|
|
||||||
public handle = async (req: Request): Promise<Response> => {
|
public handle = async (req: Request): Promise<Response> => {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
const route = url.pathname;
|
for (const [route, handlers] of this.routes.entries()) {
|
||||||
if (route in this.routes) {
|
const pattern = new URLPattern({ pathname: route });
|
||||||
let res;
|
const match = pattern.exec(req.url);
|
||||||
for (const handler of this.routes[route]) {
|
if (match) {
|
||||||
res = await handler(req, {
|
let res;
|
||||||
url,
|
for (const handler of handlers) {
|
||||||
state: {},
|
res = await handler(req, {
|
||||||
});
|
url,
|
||||||
if (res) {
|
state: {},
|
||||||
return res;
|
params: match.pathname.groups,
|
||||||
|
});
|
||||||
|
if (res) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
14
server/tags/getTagDir.ts
Normal file
14
server/tags/getTagDir.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import type { BearMetalStore } from "@bearmetal/store";
|
||||||
|
import { getDirName } from "../util/packVersion.ts";
|
||||||
|
|
||||||
|
export async function getTagDir(
|
||||||
|
store: BearMetalStore,
|
||||||
|
namespace: string,
|
||||||
|
version: number,
|
||||||
|
) {
|
||||||
|
// const versionData = JSON.parse(Deno.readTextFileSync(installPath + "pack_versions/" + version + ".json"));
|
||||||
|
|
||||||
|
const tagDir = await getDirName(version, "tags");
|
||||||
|
|
||||||
|
return `${store.get("packlocation")}/${namespace}/${tagDir}`;
|
||||||
|
}
|
31
server/tags/routes.ts
Normal file
31
server/tags/routes.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import { BearMetalStore } from "@bearmetal/store";
|
||||||
|
import { getPackVersion } from "../util/packVersion.ts";
|
||||||
|
import type { Router } from "../router.ts";
|
||||||
|
import { getTagDir } from "./getTagDir.ts";
|
||||||
|
|
||||||
|
export const createTagRoutes = (router: Router) => {
|
||||||
|
router.route("/api/pack/:namespace/tags")
|
||||||
|
.get(async (_, ctx) => {
|
||||||
|
if (!ctx.params.namespace) {
|
||||||
|
return new Response("somehow hit the tags endpoint without namespace", {
|
||||||
|
status: 500,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
using store = new BearMetalStore();
|
||||||
|
|
||||||
|
const version = getPackVersion(store);
|
||||||
|
|
||||||
|
const tagDir = await getTagDir(store, ctx.params.namespace, version);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const tags = Array.from(
|
||||||
|
Deno.readDirSync(
|
||||||
|
tagDir,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return new Response(JSON.stringify(tags), { status: 200 });
|
||||||
|
} catch {
|
||||||
|
return new Response("no tags found", { status: 404 });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
34
server/util/packVersion.ts
Normal file
34
server/util/packVersion.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import type { BearMetalStore } from "@bearmetal/store";
|
||||||
|
|
||||||
|
export function getPackVersion(store: BearMetalStore) {
|
||||||
|
const packMeta = Deno.readTextFileSync(
|
||||||
|
store.get("packlocation") + "/pack.mcmeta",
|
||||||
|
);
|
||||||
|
const packMetaJson = JSON.parse(packMeta);
|
||||||
|
return packMetaJson.pack.pack_format as number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getDirName(version: number, path: string) {
|
||||||
|
const { default: versionData } = await import(
|
||||||
|
"../../pack_versions/" + version + ".json",
|
||||||
|
{
|
||||||
|
with: { type: "json" },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const singular = makeSingular(path);
|
||||||
|
const plural = makePlural(path);
|
||||||
|
|
||||||
|
console.log("versionData", versionData);
|
||||||
|
|
||||||
|
return versionData && versionData.schema.data["<namespace>"][singular] ||
|
||||||
|
versionData.schema.data["<namespace>"][plural];
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeSingular(word: string) {
|
||||||
|
return word.replace(/s$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
function makePlural(word: string) {
|
||||||
|
return makeSingular(word) + "s";
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user