Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rainwashed committed Dec 25, 2024
1 parent 44da484 commit fabd757
Show file tree
Hide file tree
Showing 62 changed files with 1,433 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
db/
**/dist/
*.lockb
**/.vscode/
**/node_modules/
180 changes: 180 additions & 0 deletions .gitignore
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
24 changes: 24 additions & 0 deletions Dockerfile
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"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Stupidly Simple Redirect
1 change: 1 addition & 0 deletions db/blank
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.
23 changes: 23 additions & 0 deletions package.json
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"
}
}
101 changes: 101 additions & 0 deletions src/database.ts
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 };
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./server";
import "./database";
Loading

0 comments on commit fabd757

Please sign in to comment.