This commit is contained in:
Rizky 2024-01-24 07:35:44 +07:00
parent 829f1367dc
commit a63cc40afe
6 changed files with 91 additions and 80 deletions

100
app/srv/exports.d.ts vendored
View File

@ -1,3 +1,19 @@
declare module "pkgs/api/_prasi" {
export const _: {
url: string;
api(): Promise<void>;
};
export const getApiEntry: () => any;
}
declare module "pkgs/api/_web" {
export const _: {
url: string;
api(id: string, _: string): Promise<any>;
};
}
declare module "pkgs/utils/dir" {
export const dir: (path: string) => string;
}
declare module "app/db/db" {
}
@ -89,19 +105,6 @@ declare module "pkgs/utils/global" {
};
};
}
declare module "pkgs/server/serve-web" {
export const serveWeb: (url: URL, req: Request) => Promise<false | string[] | Response>;
export const generateIndexHtml: (base_url: string, site_id: string) => string;
}
declare module "pkgs/api/_file" {
export const _: {
url: string;
api(): Promise<Response>;
};
}
declare module "pkgs/utils/dir" {
export const dir: (path: string) => string;
}
declare module "pkgs/server/load-web" {
export const loadWeb: () => Promise<void>;
export const loadWebCache: (site_id: string, ts: number | string) => Promise<void>;
@ -155,12 +158,21 @@ declare module "pkgs/api/_deploy" {
};
export const downloadFile: (url: string, filePath: string, progress?: (rec: number, total: number) => void) => Promise<boolean>;
}
declare module "pkgs/api/_prasi" {
declare module "pkgs/api/_upload" {
export const _: {
url: string;
api(): Promise<void>;
api(body: any): Promise<string>;
};
}
declare module "pkgs/server/serve-web" {
export const serveWeb: (url: URL, req: Request) => Promise<false | string[] | Response>;
export const generateIndexHtml: (base_url: string, site_id: string) => string;
}
declare module "pkgs/api/_file" {
export const _: {
url: string;
api(): Promise<Response>;
};
export const getApiEntry: () => any;
}
declare module "pkgs/api/_notif" {
export const _: {
@ -190,10 +202,10 @@ declare module "pkgs/api/_notif" {
}>;
};
}
declare module "pkgs/api/_web" {
declare module "pkgs/api/_api_frm" {
export const _: {
url: string;
api(id: string, _: string): Promise<any>;
api(): Promise<void>;
};
}
declare module "pkgs/api/_proxy" {
@ -207,31 +219,19 @@ declare module "pkgs/api/_proxy" {
}): Promise<Response>;
};
}
declare module "pkgs/api/_upload" {
export const _: {
url: string;
api(body: any): Promise<string>;
};
}
declare module "pkgs/api/_dbs" {
export const _: {
url: string;
api(dbName: any, action?: string): Promise<void>;
};
}
declare module "pkgs/api/_api_frm" {
export const _: {
url: string;
api(): Promise<void>;
};
}
declare module "app/srv/exports" {
export const _file: {
export const _web: {
name: string;
url: string;
path: string;
args: any[];
handler: Promise<typeof import("pkgs/api/_file")>;
args: string[];
handler: Promise<typeof import("pkgs/api/_web")>;
};
export const _deploy: {
name: string;
@ -240,6 +240,13 @@ declare module "app/srv/exports" {
args: string[];
handler: Promise<typeof import("pkgs/api/_deploy")>;
};
export const _upload: {
name: string;
url: string;
path: string;
args: string[];
handler: Promise<typeof import("pkgs/api/_upload")>;
};
export const _prasi: {
name: string;
url: string;
@ -247,6 +254,13 @@ declare module "app/srv/exports" {
args: any[];
handler: Promise<typeof import("pkgs/api/_prasi")>;
};
export const _file: {
name: string;
url: string;
path: string;
args: any[];
handler: Promise<typeof import("pkgs/api/_file")>;
};
export const _notif: {
name: string;
url: string;
@ -254,12 +268,12 @@ declare module "app/srv/exports" {
args: string[];
handler: Promise<typeof import("pkgs/api/_notif")>;
};
export const _web: {
export const _api_frm: {
name: string;
url: string;
path: string;
args: string[];
handler: Promise<typeof import("pkgs/api/_web")>;
args: any[];
handler: Promise<typeof import("pkgs/api/_api_frm")>;
};
export const _proxy: {
name: string;
@ -268,13 +282,6 @@ declare module "app/srv/exports" {
args: string[];
handler: Promise<typeof import("pkgs/api/_proxy")>;
};
export const _upload: {
name: string;
url: string;
path: string;
args: string[];
handler: Promise<typeof import("pkgs/api/_upload")>;
};
export const _dbs: {
name: string;
url: string;
@ -282,11 +289,4 @@ declare module "app/srv/exports" {
args: string[];
handler: Promise<typeof import("pkgs/api/_dbs")>;
};
export const _api_frm: {
name: string;
url: string;
path: string;
args: any[];
handler: Promise<typeof import("pkgs/api/_api_frm")>;
};
}

View File

@ -1,9 +1,9 @@
export const _file = {
name: "_file",
url: "/_file/**",
path: "app/srv/api/_file.ts",
args: [],
handler: import("../../pkgs/api/_file")
export const _web = {
name: "_web",
url: "/_web/:id/**",
path: "app/srv/api/_web.ts",
args: ["id","_"],
handler: import("../../pkgs/api/_web")
}
export const _deploy = {
name: "_deploy",
@ -12,6 +12,13 @@ export const _deploy = {
args: ["action"],
handler: import("../../pkgs/api/_deploy")
}
export const _upload = {
name: "_upload",
url: "/_upload",
path: "app/srv/api/_upload.ts",
args: ["body"],
handler: import("../../pkgs/api/_upload")
}
export const _prasi = {
name: "_prasi",
url: "/_prasi/**",
@ -19,6 +26,13 @@ export const _prasi = {
args: [],
handler: import("../../pkgs/api/_prasi")
}
export const _file = {
name: "_file",
url: "/_file/**",
path: "app/srv/api/_file.ts",
args: [],
handler: import("../../pkgs/api/_file")
}
export const _notif = {
name: "_notif",
url: "/_notif/:action/:token",
@ -26,12 +40,12 @@ export const _notif = {
args: ["action","data"],
handler: import("../../pkgs/api/_notif")
}
export const _web = {
name: "_web",
url: "/_web/:id/**",
path: "app/srv/api/_web.ts",
args: ["id","_"],
handler: import("../../pkgs/api/_web")
export const _api_frm = {
name: "_api_frm",
url: "/_api_frm",
path: "app/srv/api/_api_frm.ts",
args: [],
handler: import("../../pkgs/api/_api_frm")
}
export const _proxy = {
name: "_proxy",
@ -40,13 +54,6 @@ export const _proxy = {
args: ["arg"],
handler: import("../../pkgs/api/_proxy")
}
export const _upload = {
name: "_upload",
url: "/_upload",
path: "app/srv/api/_upload.ts",
args: ["body"],
handler: import("../../pkgs/api/_upload")
}
export const _dbs = {
name: "_dbs",
url: "/_dbs/:dbName/:action",
@ -54,10 +61,3 @@ export const _dbs = {
args: ["dbName","action"],
handler: import("../../pkgs/api/_dbs")
}
export const _api_frm = {
name: "_api_frm",
url: "/_api_frm",
path: "app/srv/api/_api_frm.ts",
args: [],
handler: import("../../pkgs/api/_api_frm")
}

View File

@ -190,6 +190,7 @@ export const downloadFile = async (
if (_url.hostname === "localhost") {
_url.hostname = "127.0.0.1";
}
g.log.info(`Downloading ${url} to ${filePath}`);
const res = await fetch(_url);
if (res.body) {
const file = Bun.file(filePath);

View File

@ -5,13 +5,20 @@ import { config } from "./utils/config";
import { g } from "./utils/global";
import { createLogger } from "./utils/logger";
import { loadWeb } from "./server/load-web";
import { ensureNotRunning } from "utils/ensure";
import { ensureNotRunning, randomBetween } from "utils/ensure";
import { preparePrisma } from "utils/prisma";
import { startDevWatcher } from "utils/dev-watcher";
g.mode = process.argv.includes("dev") ? "dev" : "prod";
g.datadir = g.mode === "prod" ? "../data" : ".data";
if (!process.env.PORT) {
g.port = randomBetween(5000, 15000);
await Bun.write(".env", `PORT=${g.port}`);
} else {
g.port = parseInt(process.env.PORT);
}
await preparePrisma();
await createLogger();
await ensureNotRunning();

View File

@ -26,7 +26,7 @@ export const ensureNotRunning = async () => {
};
if (!(await checkPort())) {
g.log.warn(`Port ${process.env.PORT} is used, waiting...`);
g.log.warn(`Port ${g.port} is used, waiting...`);
setInterval(async () => {
if (await checkPort()) resolve();
}, 500);
@ -35,3 +35,7 @@ export const ensureNotRunning = async () => {
}
});
};
export function randomBetween(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

View File

@ -13,5 +13,4 @@ export const preparePrisma = async () => {
}
g.dburl = process.env.DATABASE_URL || "";
g.port = parseInt(process.env.PORT || "3000");
};