diff --git a/.env.defaults b/.env.defaults index 04673d8..1967a8d 100644 --- a/.env.defaults +++ b/.env.defaults @@ -1,2 +1,3 @@ PORT=3000 BASE_URL=http://localhost:3000 +DATA_DIR=./data diff --git a/.env.example b/.env.example index 10152d9..f3f83c1 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ PORT= BASE_URL= +DATA_DIR= diff --git a/.gitignore b/.gitignore index 8ff5fd5..8aa1d94 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ yarn-error.log* .env *.db* -data/* +data/ !.tool-versions !.gitkeep diff --git a/data/.gitkeep b/data/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/deno.json b/deno.json index 84806f0..d3d283d 100644 --- a/deno.json +++ b/deno.json @@ -1,8 +1,8 @@ { "tasks": { - "dev": "deno run --reload --watch --unstable-kv --unstable-cron --allow-net --allow-env --allow-write --allow-read ./main.ts", - "start": "deno run --unstable-kv --unstable-cron --allow-net --allow-env --allow-write --allow-read ./main.ts", - "compile": "deno compile --unstable-kv --unstable-cron --allow-net --allow-env --allow-write --allow-read ./main.ts" + "dev": "deno run --reload --watch --unstable-kv --unstable-cron --allow-net --allow-env --allow-write --allow-read main.ts", + "start": "deno run --unstable-kv --unstable-cron --allow-net --allow-env --allow-write --allow-read main.ts", + "compile": "deno compile --unstable-kv --unstable-cron --allow-net --allow-env --allow-write --allow-read main.ts" }, "imports": { "path": "https://deno.land/std@0.224.0/path/mod.ts", diff --git a/deno.lock b/deno.lock index 12b1394..9025fb2 100644 --- a/deno.lock +++ b/deno.lock @@ -3,6 +3,7 @@ "packages": { "specifiers": { "jsr:@deno/cache-dir@0.8": "jsr:@deno/cache-dir@0.8.0", + "jsr:@deno/graph@^0.69.7": "jsr:@deno/graph@0.69.10", "jsr:@oak/commons@0.10": "jsr:@oak/commons@0.10.1", "jsr:@std/assert@0.222": "jsr:@std/assert@0.222.1", "jsr:@std/assert@0.223": "jsr:@std/assert@0.223.0", @@ -33,12 +34,16 @@ "@deno/cache-dir@0.8.0": { "integrity": "e87e80a404958f6350d903e6238b72afb92468378b0b32111f7a1e4916ac7fe7", "dependencies": [ + "jsr:@deno/graph@^0.69.7", "jsr:@std/fmt@^0.218.2", "jsr:@std/fs@^0.218.2", "jsr:@std/io@^0.218.2", "jsr:@std/path@^0.218.2" ] }, + "@deno/graph@0.69.10": { + "integrity": "38fe22ac5686f6ece5daeec5a4df65c6314d7d32adcc33f77917a13cfaffa26f" + }, "@oak/commons@0.10.1": { "integrity": "4775ebf70782b0c5d95958d85b2425b3fbdff06f57654aa4143dbcb23b923cf4", "dependencies": [ @@ -176,6 +181,7 @@ "https://deno.land/std@0.224.0/dotenv/stringify.ts": "275da322c409170160440836342eaa7cf012a1d11a7e700d8ca4e7f2f8aa4615", "https://deno.land/std@0.224.0/fmt/colors.ts": "508563c0659dd7198ba4bbf87e97f654af3c34eb56ba790260f252ad8012e1c5", "https://deno.land/std@0.224.0/fs/exists.ts": "3d38cb7dcbca3cf313be343a7b8af18a87bddb4b5ca1bd2314be12d06533b50f", + "https://deno.land/std@0.224.0/io/types.ts": "acecb3074c730b5ff487ba4fe9ce51e67bd982aa07c95e5f5679b7b2f24ad129", "https://deno.land/std@0.224.0/io/write_all.ts": "24aac2312bb21096ae3ae0b102b22c26164d3249dff96dbac130958aa736f038", "https://deno.land/std@0.224.0/log/_config.ts": "489e11b6d3c917bf5fc954c5e914c095d3480efd924d1e85f2fc576468581c54", "https://deno.land/std@0.224.0/log/_state.ts": "314c0c31ab9c8f4fb33326ad446757d35f75e5bb21746b7720ed4e3f3a939da1", diff --git a/main.ts b/main.ts index efc07aa..63b1ad5 100644 --- a/main.ts +++ b/main.ts @@ -1,16 +1,15 @@ import * as log from "log"; -import { load } from "dotenv"; import { Application } from "oak"; import * as middleware from "./src/middlewares/index.ts"; import * as cron from "./src/cron/index.ts"; import { router } from "./src/router.ts"; +import { LISTEN_PORT } from "./src/constants.ts"; // take care of expired files Deno.cron("Cleanup", "0 0 * * *", cron.cleanup); -await load({ export: true }); -const port = Number(Deno.env.get("PORT")); const app = new Application(); +const port = Number(LISTEN_PORT); app.use(middleware.logs); app.use(middleware.headers); diff --git a/src/constants.ts b/src/constants.ts index cfe7d14..468bfa8 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,8 +1,13 @@ -export const MAX_REQUEST_SIZE = 21 * 1024 * 1024; // in bytes (21mb) -export const FILE_TTL = 7 * 24 * 60 * 60 * 1000; // in milliseconds (7 days) +import { load } from "dotenv"; +await load({ export: true }); -export const DATA_DIR = `${Deno.cwd()}/data`; // directory to database file +export const LISTEN_PORT = `${Deno.env.get("PORT")}`; // port to server listen to +export const BASE_URL = `${Deno.env.get("BASE_URL")}`; // url to print after upload +export const DATA_DIR = `${Deno.env.get("DATA_DIR")}`; // directory to save .db file export const FILES_DIR = `${DATA_DIR}/files`; // directory to upload binaries export const ASSETS_DIR = `${Deno.cwd()}/assets`; // directory to static assets export const VIEWS_DIR = `${Deno.cwd()}/src/views`; // directory contain .ejs templates + +export const MAX_REQUEST_SIZE = 21 * 1024 * 1024; // in bytes (21mb) +export const FILE_TTL = 7 * 24 * 60 * 60 * 1000; // in milliseconds (7 days) diff --git a/src/database.ts b/src/database.ts index 85926f7..305f791 100644 --- a/src/database.ts +++ b/src/database.ts @@ -5,4 +5,5 @@ export type Data = { contentType: string; }; +await Deno.mkdir(DATA_DIR, { recursive: true, mode: 0o755 }); export const kv = await Deno.openKv(`${DATA_DIR}/kv.db`); diff --git a/src/routes/create.ts b/src/routes/create.ts index 427cdf8..65d3b99 100644 --- a/src/routes/create.ts +++ b/src/routes/create.ts @@ -1,12 +1,10 @@ import { Middleware, Status } from "oak"; -import { FILES_DIR, MAX_REQUEST_SIZE } from "../constants.ts"; +import { BASE_URL, FILES_DIR, MAX_REQUEST_SIZE } from "../constants.ts"; import { randomUUID } from "../utils/uuid.ts"; import { encrypt } from "../utils/encrypt.ts"; import { Data, kv } from "../database.ts"; export const create: Middleware = async (ctx) => { - const baseUrl = Deno.env.get("BASE_URL"); - // api will only work with form-data if (ctx.request.body.type() !== "form-data") { return ctx.throw(Status.BadRequest); @@ -54,5 +52,5 @@ export const create: Middleware = async (ctx) => { ]); ctx.response.status = Status.Created; - ctx.response.body = `${baseUrl}/${uuid}#${k}`; + ctx.response.body = `${BASE_URL}/${uuid}#${k}`; };