chore: first build

This commit is contained in:
2024-08-17 04:55:01 +07:00
parent 61a07068d5
commit e1d2274bfb
37 changed files with 537 additions and 59 deletions
+2
View File
@@ -0,0 +1,2 @@
//
export const __PROD = import.meta.env.NODE_ENV === "production";
+13 -1
View File
@@ -1,4 +1,16 @@
import type { Config } from "../types/garage";
import { readTomlFile } from "./utils";
export const config = readTomlFile<Config>(process.env.CONFIG_PATH);
const CONFIG_PATH = process.env.CONFIG_PATH || "/etc/garage.toml";
export const config = await readTomlFile<Config>(CONFIG_PATH);
if (!config?.rpc_public_addr) {
throw new Error(
"Cannot load garage config! Missing `rpc_public_addr` in config file."
);
}
if (!config?.admin?.admin_token) {
throw new Error("Missing `admin.admin_token` in config.");
}
+2 -1
View File
@@ -3,7 +3,8 @@ 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);
const pathname = url.pathname.replace(/\/api\//, "/");
const reqUrl = new URL(API_BASE_URL + pathname + url.search);
try {
const headers = c.req.raw.headers;
+9 -4
View File
@@ -1,9 +1,14 @@
import fs from "node:fs";
import toml from "toml";
export const readTomlFile = <T = any>(path?: string | null) => {
if (!path || !fs.existsSync(path)) {
export const readTomlFile = async <T = any>(path?: string | null) => {
if (!path) {
return undefined;
}
return toml.parse(fs.readFileSync(path, "utf8")) as T;
const file = Bun.file(path);
if (!(await file.exists())) {
return undefined;
}
return toml.parse(await file.text()) as T;
};
+18 -4
View File
@@ -1,20 +1,34 @@
import { Hono } from "hono";
import { logger } from "hono/logger";
import { serveStatic } from "hono/bun";
import router from "./routes";
import { proxyApi } from "./lib/proxy-api";
import { __PROD } from "./lib/consts";
const HOST = import.meta.env.HOST || "0.0.0.0";
const PORT = Number(import.meta.env.PORT) || 3909;
const DIST_ROOT = import.meta.env.DIST_ROOT || "./dist";
const app = new Hono();
app.use(logger());
// API router
app.route("/", router);
app.route("/api", router);
// Proxy to garage admin API
app.all("*", proxyApi);
if (__PROD) {
// Serve client dist
app.use(serveStatic({ root: DIST_ROOT }));
app.use(async (c, next) => {
try {
const file = Bun.file(DIST_ROOT + "/index.html");
return c.html(await file.text());
} catch (err) {
next();
}
});
console.log(`Listening on http://${HOST}:${PORT}`);
}
export default {
fetch: app.fetch,
+3 -1
View File
@@ -3,7 +3,9 @@
"module": "main.ts",
"type": "module",
"scripts": {
"dev": "bun --watch main.ts"
"dev": "bun --watch main.ts",
"build": "bun build main.ts --minify --sourcemap --outdir ./dist",
"start": "NODE_ENV=production bun run ./dist/main.js"
},
"devDependencies": {
"@types/bun": "latest"
+10
View File
@@ -14,6 +14,9 @@ importers:
toml:
specifier: ^3.0.0
version: 3.0.0
typescript:
specifier: ^5.0.0
version: 5.5.4
devDependencies:
'@types/bun':
specifier: latest
@@ -40,6 +43,11 @@ packages:
[email protected]:
resolution: {integrity: sha512-y/mWCZinnvxjTKYhJ+pYxwD0mRLVvOtdS2Awbgxln6iEnt4rk0yBxeSBHkGJcPucRiG0e55mwWp+g/05rsrd6w==}
[email protected]:
resolution: {integrity: sha512-Mtq29sKDAEYP7aljRgtPOpTvOfbwRWlS6dPRzwjdE+C0R4brX/GUyhHSecbHMFLNBLcJIPt9nl9yG5TZ1weH+Q==}
engines: {node: '>=14.17'}
hasBin: true
[email protected]:
resolution: {integrity: sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==}
@@ -66,4 +74,6 @@ snapshots:
[email protected]: {}
[email protected]: {}
[email protected]: {}
+4 -1
View File
@@ -1,10 +1,13 @@
import { Hono } from "hono";
import { buckets } from "./buckets";
import { configRoute } from "./config";
import { proxyApi } from "../lib/proxy-api";
const router = new Hono()
//
.route("/config", configRoute)
.route("/buckets", buckets);
.route("/buckets", buckets)
.all("*", proxyApi);
export default router;