This commit is contained in:
Rizky 2024-07-18 22:48:10 +07:00
parent e8fc0363c8
commit b7ff711617
6 changed files with 58 additions and 11 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -20,6 +20,7 @@
},
"dependencies": {
"brotli-wasm": "^2.0.1",
"bun-sqlite-key-value": "^1.4.5",
"exit-hook": "^4.0.0",
"firebase-admin": "^12.2.0",
"prisma": "^5.17.0"

View File

@ -1,17 +1,12 @@
import { $ } from "execa";
import * as fs from "fs";
import {
dirAsync,
readAsync,
removeAsync,
writeAsync
} from "fs-jetpack";
import { dirAsync, readAsync, removeAsync, writeAsync } from "fs-jetpack";
import { apiContext } from "service-srv";
import { deploy } from "utils/deploy";
import { dir } from "utils/dir";
import { g } from "utils/global";
import { genEnv, parseEnv } from "utils/parse-env";
import { restartServer } from "utils/restart";
import { $ } from "bun";
export const _ = {
url: "/_deploy",
@ -84,7 +79,7 @@ export const _ = {
return "ok";
case "db-gen":
{
await $({ cwd: dir("app/db") })`bun prisma generate`;
await $`bun prisma generate`.cwd(dir("app/db"));
res.send("ok");
setTimeout(() => {
@ -118,9 +113,9 @@ export const _ = {
dir("app/db/.env"),
`DATABASE_URL=${ENV.DATABASE_URL}`
);
await $({ cwd: dir("app/db") })`bun install`;
await $({ cwd: dir("app/db") })`bun prisma db pull --force`;
await $({ cwd: dir("app/db") })`bun prisma generate`;
await $`bun install`.cwd(dir("app/db"));
await $`bun prisma db pull --force`.cwd(dir("app/db"));
await $`bun prisma generate`.cwd(dir("app/db"));
await Bun.write(
dir(`${g.datadir}/db-ver`),
Date.now().toString()

View File

@ -11,6 +11,7 @@ import {
removeAsync,
renameAsync,
} from "fs-jetpack";
export const _ = {
url: "/_file/**",
async api() {

48
pkgs/api/_kv.ts Normal file
View File

@ -0,0 +1,48 @@
import { BunSqliteKeyValue } from "bun-sqlite-key-value";
import { apiContext } from "service-srv";
import { dir } from "utils/dir";
import { g } from "utils/global";
export const _ = {
url: "/_kv/**",
raw: true,
async api() {
const { req } = apiContext(this);
if (!g.kv) {
g.kv = new BunSqliteKeyValue(dir(`${g.datadir}/db-kv.sqlite`));
}
try {
const parts = req.params._.split("/");
switch (parts[0]) {
case "set": {
const body = await req.json();
if (typeof parts[1] === "string" && typeof body !== "undefined") {
g.kv.set(parts[1], body);
return new Response(JSON.stringify({ status: "ok" }), {
headers: { "content-type": "application/json" },
});
}
return new Response(
JSON.stringify({ status: "failed", reason: "no key or body" }),
{
headers: { "content-type": "application/json" },
}
);
}
case "get": {
return new Response(JSON.stringify(g.kv.get(parts[1])), {
headers: { "content-type": "application/json" },
});
}
}
} catch (e) {}
return new Response(JSON.stringify({ status: "failed" }), {
headers: { "content-type": "application/json" },
});
},
};

View File

@ -6,6 +6,7 @@ import { PrismaClient } from "../../app/db/db";
import admin from "firebase-admin";
import { Database } from "bun:sqlite";
import { prodIndex } from "./prod-index";
import { BunSqliteKeyValue } from "bun-sqlite-key-value";
type SingleRoute = {
url: string;
@ -39,6 +40,7 @@ type PrasiServer = {
export const g = global as unknown as {
db: PrismaClient;
kv: BunSqliteKeyValue;
dburl: string;
datadir: string;
mode: "dev" | "prod";