-
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.
- Loading branch information
1 parent
44da484
commit fabd757
Showing
62 changed files
with
1,433 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
db/ | ||
**/dist/ | ||
*.lockb | ||
**/.vscode/ | ||
**/node_modules/ |
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,180 @@ | ||
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore | ||
|
||
# Logs | ||
|
||
logs | ||
_.log | ||
npm-debug.log_ | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
.pnpm-debug.log* | ||
|
||
# Caches | ||
|
||
.cache | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
|
||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
||
# Runtime data | ||
|
||
pids | ||
_.pid | ||
_.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
|
||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
|
||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
|
||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
|
||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
|
||
bower_components | ||
|
||
# node-waf configuration | ||
|
||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
|
||
build/Release | ||
|
||
# Dependency directories | ||
|
||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Snowpack dependency directory (https://snowpack.dev/) | ||
|
||
web_modules/ | ||
|
||
# TypeScript cache | ||
|
||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
|
||
.npm | ||
|
||
# Optional eslint cache | ||
|
||
.eslintcache | ||
|
||
# Optional stylelint cache | ||
|
||
.stylelintcache | ||
|
||
# Microbundle cache | ||
|
||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
|
||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
|
||
*.tgz | ||
|
||
# Yarn Integrity file | ||
|
||
.yarn-integrity | ||
|
||
# dotenv environment variable files | ||
|
||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
|
||
.parcel-cache | ||
|
||
# Next.js build output | ||
|
||
.next | ||
out | ||
|
||
# Nuxt.js build / generate output | ||
|
||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
|
||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
|
||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
|
||
# public | ||
|
||
# vuepress build output | ||
|
||
.vuepress/dist | ||
|
||
# vuepress v2.x temp and cache directory | ||
|
||
.temp | ||
|
||
# Docusaurus cache and generated files | ||
|
||
.docusaurus | ||
|
||
# Serverless directories | ||
|
||
.serverless/ | ||
|
||
# FuseBox cache | ||
|
||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
|
||
.dynamodb/ | ||
|
||
# TernJS port file | ||
|
||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
|
||
.vscode-test | ||
|
||
# yarn v2 | ||
|
||
.yarn/cache | ||
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
# IntelliJ based IDEs | ||
.idea | ||
|
||
# Finder (MacOS) folder config | ||
.DS_Store | ||
|
||
db/*.db | ||
db/*.sqlite | ||
**/.vscode/ | ||
*.lockb |
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,24 @@ | ||
FROM oven/bun:latest | ||
|
||
WORKDIR /app | ||
|
||
COPY . ./ | ||
COPY ./view ./view | ||
|
||
RUN mkdir ./db | ||
VOLUME ./db | ||
|
||
RUN bun install | ||
RUN cd view && bun install && bun run build && cd .. | ||
|
||
WORKDIR /app | ||
|
||
RUN bun run build | ||
|
||
ENV PORT=3002 | ||
ENV ADMIN_USER=admin | ||
ENV ADMIN_PASSWORD=admin | ||
|
||
EXPOSE 3002 | ||
|
||
CMD ["bun", "run", "start"] |
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 @@ | ||
# Stupidly Simple Redirect |
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 @@ | ||
This is necessary to have this directory be open as Bun SQLite requires that the folder exist in order to create the database files. |
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,23 @@ | ||
{ | ||
"name": "ssr", | ||
"module": "src/index.ts", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "cd view; bun run dev & cd ..; bun --watch src/index.ts", | ||
"build": "cd view; bun run build; cd ..; bun build src/index.ts --target bun --outdir ./dist", | ||
"start": "NODE_ENV=production bun dist/index.js", | ||
"test": "bun test" | ||
}, | ||
"devDependencies": { | ||
"@types/bun": "^1.1.14" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"@elysiajs/cors": "^1.1.1", | ||
"@elysiajs/html": "^1.1.1", | ||
"@elysiajs/static": "^1.1.2", | ||
"elysia": "^1.1.26" | ||
} | ||
} |
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,101 @@ | ||
import { Database } from "bun:sqlite"; | ||
import { randomBytes } from "node:crypto"; | ||
|
||
const adminDb = new Database("db/admin.sqlite", { create: true }); | ||
const redirectsDb = new Database("db/redirects.sqlite", { create: true }); | ||
const memoryCache = new Map(); | ||
// declare hashing function with salt | ||
const hashFunction = (toBeHashed: string, salt: string) => { | ||
return new Bun.CryptoHasher("sha256").update(toBeHashed + salt).digest("hex"); | ||
}; | ||
// generate salt function | ||
const generateSalt = () => randomBytes(12).toString("hex"); | ||
|
||
function initDatabases() { | ||
if (!process.env["ADMIN_USER"] || !process.env["ADMIN_PASSWORD"]) { | ||
console.log( | ||
"Could not detect ADMIN_USER or ADMIN_PASSWORD in the .env file or as runtime environment variable." | ||
); | ||
|
||
process.exit(1); | ||
} | ||
|
||
memoryCache.set("redirectAmount", -1); | ||
|
||
adminDb.run(` | ||
CREATE TABLE IF NOT EXISTS admins ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
username TEXT UNIQUE NOT NULL, | ||
hash TEXT NOT NULL, | ||
salt TEXT NOT NULL | ||
); | ||
`); | ||
redirectsDb.run(` | ||
CREATE TABLE IF NOT EXISTS redirects ( | ||
target_url TEXT NOT NULL, | ||
cut TEXT PRIMARY KEY UNIQUE NOT NULL, | ||
created_at TEXT NOT NULL DEFAULT current_timestamp | ||
); | ||
`); | ||
|
||
// determine amount of elements in admin table, because if it is empty, create the first and root level account. | ||
let adminAccounts = adminDb.query("SELECT * FROM admins"); | ||
if (adminAccounts.all().length === 0) { | ||
console.log("Creating root level account..."); | ||
let salt = generateSalt(); | ||
let hash = hashFunction(process.env["ADMIN_PASSWORD"], salt); | ||
|
||
console.log({ salt, hash }); | ||
|
||
adminDb.run(` | ||
INSERT INTO admins (id, username, hash, salt) VALUES (0, "${process.env["ADMIN_USER"]}", "${hash}", "${salt}") | ||
`); | ||
} | ||
} | ||
|
||
function copyRequiredDbValuesToCache() { | ||
|
||
} | ||
|
||
function deleteDatabases() { | ||
adminDb.run("DROP TABLE admins"); | ||
redirectsDb.run("DROP TABLE redirects"); | ||
} | ||
|
||
function returnTargetFromCut(cut: string): string { | ||
let returnedQuery = redirectsDb | ||
.query(`SELECT target_url FROM redirects WHERE cut = $cut`) | ||
.get({ $cut: cut }); | ||
|
||
if (!returnedQuery) return ""; | ||
let targetUrl = (returnedQuery as { target_url: string }).target_url; | ||
|
||
return targetUrl; | ||
} | ||
|
||
function insertNewRedirect(cut: string, target: string): string { | ||
if (returnTargetFromCut(cut) !== "") return ""; | ||
|
||
redirectsDb.run( | ||
`INSERT INTO redirects VALUES ("${target}", "${cut}", current_timestamp)` | ||
); | ||
|
||
return cut; | ||
} | ||
|
||
initDatabases(); | ||
copyRequiredDbValuesToCache(); | ||
|
||
function countAmountOfRedirects(): number { | ||
let amountOfQuery = redirectsDb.query(`SELECT COUNT(*) FROM redirects`).get() as {"COUNT(*)": number}; | ||
|
||
|
||
if (!amountOfQuery || !amountOfQuery["COUNT(*)"]) return -1; | ||
|
||
let count = amountOfQuery["COUNT(*)"]; | ||
|
||
return count; | ||
} | ||
|
||
export { adminDb, redirectsDb }; | ||
export { returnTargetFromCut, insertNewRedirect, countAmountOfRedirects }; |
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,2 @@ | ||
import "./server"; | ||
import "./database"; |
Oops, something went wrong.