diff --git a/app/srv/exports.d.ts b/app/srv/exports.d.ts index 5421d63..aeea6c5 100644 --- a/app/srv/exports.d.ts +++ b/app/srv/exports.d.ts @@ -1,3 +1,19 @@ +declare module "pkgs/api/_prasi" { + export const _: { + url: string; + api(): Promise; + }; + export const getApiEntry: () => any; +} +declare module "pkgs/api/_web" { + export const _: { + url: string; + api(id: string, _: string): Promise; + }; +} +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; - export const generateIndexHtml: (base_url: string, site_id: string) => string; -} -declare module "pkgs/api/_file" { - export const _: { - url: string; - api(): Promise; - }; -} -declare module "pkgs/utils/dir" { - export const dir: (path: string) => string; -} declare module "pkgs/server/load-web" { export const loadWeb: () => Promise; export const loadWebCache: (site_id: string, ts: number | string) => Promise; @@ -155,12 +158,21 @@ declare module "pkgs/api/_deploy" { }; export const downloadFile: (url: string, filePath: string, progress?: (rec: number, total: number) => void) => Promise; } -declare module "pkgs/api/_prasi" { +declare module "pkgs/api/_upload" { export const _: { url: string; - api(): Promise; + api(body: any): Promise; + }; +} +declare module "pkgs/server/serve-web" { + export const serveWeb: (url: URL, req: Request) => Promise; + export const generateIndexHtml: (base_url: string, site_id: string) => string; +} +declare module "pkgs/api/_file" { + export const _: { + url: string; + api(): Promise; }; - 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; + api(): Promise; }; } declare module "pkgs/api/_proxy" { @@ -207,31 +219,19 @@ declare module "pkgs/api/_proxy" { }): Promise; }; } -declare module "pkgs/api/_upload" { - export const _: { - url: string; - api(body: any): Promise; - }; -} declare module "pkgs/api/_dbs" { export const _: { url: string; api(dbName: any, action?: string): Promise; }; } -declare module "pkgs/api/_api_frm" { - export const _: { - url: string; - api(): Promise; - }; -} declare module "app/srv/exports" { - export const _file: { + export const _web: { name: string; url: string; path: string; - args: any[]; - handler: Promise; + args: string[]; + handler: Promise; }; export const _deploy: { name: string; @@ -240,6 +240,13 @@ declare module "app/srv/exports" { args: string[]; handler: Promise; }; + export const _upload: { + name: string; + url: string; + path: string; + args: string[]; + handler: Promise; + }; export const _prasi: { name: string; url: string; @@ -247,6 +254,13 @@ declare module "app/srv/exports" { args: any[]; handler: Promise; }; + export const _file: { + name: string; + url: string; + path: string; + args: any[]; + handler: Promise; + }; export const _notif: { name: string; url: string; @@ -254,12 +268,12 @@ declare module "app/srv/exports" { args: string[]; handler: Promise; }; - export const _web: { + export const _api_frm: { name: string; url: string; path: string; - args: string[]; - handler: Promise; + args: any[]; + handler: Promise; }; export const _proxy: { name: string; @@ -268,13 +282,6 @@ declare module "app/srv/exports" { args: string[]; handler: Promise; }; - export const _upload: { - name: string; - url: string; - path: string; - args: string[]; - handler: Promise; - }; export const _dbs: { name: string; url: string; @@ -282,11 +289,4 @@ declare module "app/srv/exports" { args: string[]; handler: Promise; }; - export const _api_frm: { - name: string; - url: string; - path: string; - args: any[]; - handler: Promise; - }; } diff --git a/app/srv/exports.ts b/app/srv/exports.ts index 416e921..4c1c2ba 100644 --- a/app/srv/exports.ts +++ b/app/srv/exports.ts @@ -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,24 +54,10 @@ 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", path: "app/srv/api/_dbs.ts", 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") } \ No newline at end of file diff --git a/pkgs/api/_deploy.ts b/pkgs/api/_deploy.ts index cf3c0a3..c5d2b10 100644 --- a/pkgs/api/_deploy.ts +++ b/pkgs/api/_deploy.ts @@ -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); diff --git a/pkgs/index.ts b/pkgs/index.ts index 036ba18..284f0d9 100644 --- a/pkgs/index.ts +++ b/pkgs/index.ts @@ -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(); diff --git a/pkgs/utils/ensure.ts b/pkgs/utils/ensure.ts index fb6e111..f9ba6f7 100644 --- a/pkgs/utils/ensure.ts +++ b/pkgs/utils/ensure.ts @@ -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); +} diff --git a/pkgs/utils/prisma.ts b/pkgs/utils/prisma.ts index 7043015..6407efe 100644 --- a/pkgs/utils/prisma.ts +++ b/pkgs/utils/prisma.ts @@ -11,7 +11,6 @@ export const preparePrisma = async () => { const { PrismaClient } = await import("../../app/db/db"); g.db = new PrismaClient(); } - + g.dburl = process.env.DATABASE_URL || ""; - g.port = parseInt(process.env.PORT || "3000"); };