feat: add cluster & bucket management

This commit is contained in:
2024-08-16 01:23:55 +07:00
parent b0e5d53ee0
commit dfb4e30e23
41 changed files with 1394 additions and 67 deletions
+83
View File
@@ -0,0 +1,83 @@
import { config } from "./garage";
type FetchOptions = Omit<RequestInit, "headers" | "body"> & {
params?: Record<string, any>;
headers?: Record<string, string>;
body?: any;
};
const adminPort = config?.admin?.api_bind_addr?.split(":").pop();
const adminAddr =
import.meta.env.API_BASE_URL ||
config?.rpc_public_addr?.split(":")[0] + ":" + adminPort ||
"";
export const API_BASE_URL =
!adminAddr.startsWith("http") && !adminAddr.startsWith("https")
? `http://${adminAddr}`
: adminAddr;
export const API_ADMIN_KEY =
import.meta.env.API_ADMIN_KEY || config?.admin?.admin_token;
const api = {
async fetch<T = any>(url: string, options?: Partial<FetchOptions>) {
const headers: Record<string, string> = {
Authorization: `Bearer ${API_ADMIN_KEY}`,
};
const _url = new URL(API_BASE_URL + url);
if (options?.params) {
Object.entries(options.params).forEach(([key, value]) => {
_url.searchParams.set(key, String(value));
});
}
if (
typeof options?.body === "object" &&
!(options.body instanceof FormData)
) {
options.body = JSON.stringify(options.body);
headers["Content-Type"] = "application/json";
}
const res = await fetch(_url, {
...options,
headers: { ...headers, ...(options?.headers || {}) },
});
if (!res.ok) {
const err = new Error(res.statusText);
(err as any).status = res.status;
throw err;
}
const isJson = res.headers
.get("Content-Type")
?.includes("application/json");
if (isJson) {
const json = (await res.json()) as T;
return json;
}
const text = await res.text();
return text as unknown as T;
},
async get<T = any>(url: string, options?: Partial<FetchOptions>) {
return this.fetch<T>(url, {
...options,
method: "GET",
});
},
async post<T = any>(url: string, options?: Partial<FetchOptions>) {
return this.fetch<T>(url, {
...options,
method: "POST",
});
},
};
export default api;
+4
View File
@@ -0,0 +1,4 @@
import type { Config } from "../types/garage";
import { readTomlFile } from "./utils";
export const config = readTomlFile<Config>(process.env.CONFIG_PATH);
+35
View File
@@ -0,0 +1,35 @@
import type { Context } from "hono";
import { API_ADMIN_KEY, API_BASE_URL } from "./api";
export const proxyApi = async (c: Context) => {
const url = new URL(c.req.url);
const reqUrl = new URL(API_BASE_URL + url.pathname + url.search);
try {
const headers = c.req.raw.headers;
let body: BodyInit | ReadableStream<Uint8Array> | null = c.req.raw.body;
headers.set("authorization", `Bearer ${API_ADMIN_KEY}`);
if (headers.get("content-type")?.includes("application/json")) {
const json = await c.req.json();
body = JSON.stringify(json);
}
const res = await fetch(reqUrl, {
...c.req.raw,
method: c.req.method,
headers,
body,
});
return res;
} catch (err) {
return c.json(
{
success: false,
error: (err as Error)?.message || "Server error",
},
500
);
}
};
+9
View File
@@ -0,0 +1,9 @@
import fs from "node:fs";
import toml from "toml";
export const readTomlFile = <T = any>(path?: string | null) => {
if (!path || !fs.existsSync(path)) {
return undefined;
}
return toml.parse(fs.readFileSync(path, "utf8")) as T;
};