-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaces wren with hono Upgrades standard library packages to their latest versions Adds PORT env variable Uses JSR as main source of packages Signed-off-by: Amy <amy@amogus.cloud>
- Loading branch information
1 parent
3b605a1
commit 28260b9
Showing
15 changed files
with
279 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
{ | ||
"importMap": "./import_map.json", | ||
"tasks": { | ||
"start": "deno run -A -r main.ts" | ||
} | ||
"tasks": { | ||
"start": "deno run -A -r main.ts" | ||
}, | ||
"imports": { | ||
"http": "jsr:@std/http@^1.0.8", | ||
"ulid": "jsr:@std/ulid@^1.0.0", | ||
"path": "jsr:@std/path@^1.0.6", | ||
"dotenv": "jsr:@std/dotenv@^0.225.2", | ||
"fs": "jsr:@std/fs@^1.0.4", | ||
"eta": "jsr:@eta-dev/eta@^3.5.0", | ||
"hono": "jsr:@hono/hono@^4.6.5" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { load } from "dotenv/mod.ts" | ||
import { load } from "dotenv"; | ||
|
||
export const env = await load(); | ||
|
||
export const env = await load() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Eta } from "eta/src/index.ts"; | ||
import { resolve } from "path/mod.ts"; | ||
import { Eta } from "eta"; | ||
import { resolve } from "path"; | ||
|
||
const eta = new Eta({ | ||
views: resolve("./templates"), | ||
cache: true, | ||
useWith: true, | ||
}) | ||
views: resolve("./templates"), | ||
cache: true, | ||
useWith: true, | ||
}); | ||
|
||
export { eta }; | ||
|
||
export { eta } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
import { serve } from 'wren/mod.ts'; | ||
import CreateObjectRoute from "./routes/create_object.ts" | ||
import GetObjectRoute from "./routes/get_object.ts"; | ||
import { Hono } from "hono"; | ||
|
||
import { StorageSingleton } from "./storage.ts"; | ||
import IndexRoute from "./routes/index_route.ts"; | ||
import UICreateObjectRoute from "./routes/ui_create_object_route.ts"; | ||
|
||
const storage = StorageSingleton.getInstance() | ||
import ApiRoutes from "./routes/api/index.ts"; | ||
import Views from "./routes/views/index.ts"; | ||
import { env } from "./env.ts"; | ||
|
||
const app = new Hono(); | ||
|
||
const storage = StorageSingleton.getInstance(); | ||
|
||
const routes = [ | ||
CreateObjectRoute, | ||
GetObjectRoute, | ||
IndexRoute, | ||
UICreateObjectRoute | ||
]; | ||
await storage.removeLeftovers(); | ||
|
||
await storage.removeLeftovers() | ||
app.route("/", Views); | ||
app.route("/api", ApiRoutes); | ||
|
||
setInterval(() => { | ||
storage.manager.purgeExpired() | ||
}, 1000) | ||
storage.manager.purgeExpired(); | ||
}, 1000); | ||
|
||
serve(routes); | ||
Deno.serve( | ||
{ | ||
port: parseInt(env["PORT"]) || 5544, | ||
hostname: env["DOMAIN"] || "127.0.0.1", | ||
}, | ||
app.fetch, | ||
); | ||
|
||
// Run on different port | ||
// serve(routes, { port: 3000 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Hono, type Context } from "hono"; | ||
|
||
import { StorageSingleton } from "../../storage.ts"; | ||
|
||
const api = new Hono(); | ||
|
||
api.post("/object", async (c: Context) => { | ||
const form = await c.req.formData(); | ||
const file = form.get("file") ?? 1; | ||
const until: unknown = form.get("until"); | ||
const storage = StorageSingleton.getInstance(); | ||
|
||
if (until && (until as number) > 5) { | ||
return c.json( | ||
{ | ||
message: "You can't store files for more than five hours!", | ||
}, | ||
400, | ||
); | ||
} | ||
|
||
if (!file) { | ||
return c.json({ | ||
message: "Missing file", | ||
}); | ||
} else if (file instanceof File) { | ||
try { | ||
const { id, filename, stored_until } = await storage.storeFile( | ||
file, | ||
(until as number | undefined) ?? 1, | ||
); | ||
|
||
console.log(`[CREATE] File ${filename} was created`); | ||
return c.json( | ||
{ | ||
id, | ||
filename, | ||
message: `Saved ${filename} until ${new Date(stored_until)}`, | ||
}, | ||
201, | ||
); | ||
} catch { | ||
return c.json({ message: "Error whilst creating object" }, 500); | ||
} | ||
} else { | ||
return c.json({}, 406); | ||
} | ||
}); | ||
|
||
api.get("/object/:id", async (c: Context) => { | ||
if (!c.req.param()) return c.text("", 400); | ||
|
||
const objectId = c.req.param("id"); | ||
const storage = StorageSingleton.getInstance(); | ||
const object = storage.getFile(objectId); | ||
|
||
if (!object) return c.json({ message: `Could not find object ${objectId}` }); | ||
|
||
console.log(`[GET] Found ${object.filename}, serving...`); | ||
|
||
try { | ||
const file = await object.openFile(); | ||
|
||
if (!file) throw new Error(`Could not open file ${object.filename}`); | ||
|
||
c.header("Content-Type", object.mimeType); | ||
c.status(200); | ||
|
||
return c.body(file, { "Content-type": object.mimeType }); | ||
} catch (error: unknown) { | ||
return c.json( | ||
{ | ||
message: "Error whilst opening file", | ||
error: (error as Error).message, | ||
}, | ||
500, | ||
); | ||
} | ||
}); | ||
|
||
export default api; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.