Skip to content

Commit

Permalink
idc lets just do this as one commit
Browse files Browse the repository at this point in the history
  • Loading branch information
leonitousconforti committed Nov 19, 2023
1 parent 2c8b58f commit d8eb715
Show file tree
Hide file tree
Showing 260 changed files with 4,235 additions and 5,257 deletions.
2 changes: 1 addition & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// spell-checker: disable
{
"name": "Node.js & TypeScript",
"image": "mcr.microsoft.com/devcontainers/base:bullseye",
Expand Down Expand Up @@ -36,7 +37,6 @@
"Prisma.prisma",
"bradlc.vscode-tailwindcss",
"RushStack.rushstack",
"redhat.ansible",
"william-voyek.vscode-nginx"
],
"settings": {
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/dokku-deploy/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Reusable dokku deploy workflow

inputs:
DOKKU_HOST:
required: true
DOKKU_USER:
required: true
DOKKU_KEY:
required: true
DOKKU_APP:
required: true

runs:
using: "composite"
steps:
- name: "placeholder"
shell: bash
run: echo "placeholder"
121 changes: 121 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"cSpell.words": [
"Apkmirror",
"Apkpure",
"apks",
"apksubmit",
"Apksupport",
"architecting",
"armeabi",
"authproxy",
"autoinstaller",
"autoinstallers",
"azuretools",
"bitbook",
"bitcity",
"bitizen",
"bitizens",
"bitprints",
"Bluestacks",
"bufbuild",
"buildargs",
"Buildx",
"Checkin",
"cloudinit",
"codemetrics",
"commitlint",
"Conforti",
"connectrpc",
"cuda",
"Cydia",
"devcontainer",
"devcontainers",
"devel",
"dockercompose",
"Dockerode",
"DOKKU",
"DYNO",
"emittery",
"execa",
"Fastify",
"Formbody",
"frida",
"gamecenter",
"Genymotion",
"gitleaks",
"glvnd",
"googleapis",
"gpasswd",
"icsharpcode",
"ilspy",
"isort",
"Jeeze",
"Jsep",
"kisstkondoros",
"legotower",
"leonitousconforti",
"libvulkan",
"loadapk",
"localtime",
"Logcat",
"mediapath",
"mediatype",
"MITM",
"mitmdump",
"mitmproxy",
"mitmweb",
"mutantdino",
"nimblebit",
"nocheck",
"nonbrowser",
"noreply",
"npmjs",
"opengl",
"packagejson",
"pino",
"Playstore",
"pocketfrogs",
"pocketplanes",
"pockettrains",
"proto",
"protobuf",
"protoc",
"proxied",
"proxying",
"pulseaudio",
"ratelimit",
"resourcemonitor",
"reuseaddr",
"Roomate",
"rushstack",
"rushx",
"shimataro",
"socat",
"Sooooooo",
"structs",
"swiftshader",
"sysimg",
"Tailscale",
"tailwindcss",
"timestamptz",
"tinyburg",
"tinytower",
"tsdoc",
"turncfg",
"typeclass",
"typeorm",
"tzdata",
"Ummm",
"vercel",
"visualstudioexptteam",
"vscodeintellicode",
"vulkan",
"watchin",
"webrtc",
"Welp",
"Winudf",
"Xauthority",
"xvfb",
"Yayyyyyy"
]
}
12 changes: 12 additions & 0 deletions .vscode/snippets.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Gen Function _": {
"prefix": "gg",
"body": ["Effect.gen(function*(_) {", " $0", "})"],
"description": "Generator Function with a _ parameter"
},
"Gen Yield _": {
"prefix": "yy",
"body": ["yield* _($0)"],
"description": "Yield generator calling _()"
}
}
20 changes: 10 additions & 10 deletions apps/authproxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@fastify/routes-stats": "~3.4.0",
"@fastify/sensible": "~5.3.0",
"@fastify/under-pressure": "~8.3.0",
"@tinyburg/core": "workspace:*",
"@tinyburg/nucleus": "workspace:*",
"close-with-grace": "~1.2.0",
"fastify": "~4.23.2",
"fastify-plugin": "~4.5.0",
Expand All @@ -51,18 +51,18 @@
},
"devDependencies": {
"@rushstack/eslint-config": "3.4.1",
"@rushstack/heft": "0.62.3",
"@rushstack/heft-lint-plugin": "~0.2.9",
"@rushstack/heft-typescript-plugin": "~0.2.9",
"@rushstack/heft": "0.63.2",
"@rushstack/heft-lint-plugin": "0.2.12",
"@rushstack/heft-typescript-plugin": "0.2.12",
"@tinyburg/tsconfig-rig": "workspace:*",
"@types/node": "20.8.8",
"@types/node": "20.9.0",
"@types/uuid": "~9.0.1",
"compile-schemas-to-typescript": "~0.2.0",
"eslint": "8.52.0",
"eslint-config-prettier": "~9.0.0",
"eslint-plugin-prettier": "~5.0.1",
"eslint-plugin-unicorn": "~48.0.1",
"prettier": "~3.0.3",
"eslint": "8.53.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-unicorn": "49.0.0",
"prettier": "3.1.0",
"typescript": "5.2.2"
}
}
16 changes: 4 additions & 12 deletions apps/authproxy/src/auth/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import type { Redis } from "ioredis";
import type { FastifyRequest } from "fastify";
import type { RateLimitPluginOptions } from "@fastify/rate-limit";

declare module "@fastify/rate-limit" {
// eslint-disable-next-line @typescript-eslint/naming-convention
interface errorResponseBuilderContext {
ban: boolean;
after: string;
max: number;
ttl: number;
}
}

export const buildRateLimitConfig = (redis: Redis): RateLimitPluginOptions => ({
// Apply the rate limit setting to all routes within the encapsulation scope
global: true,
Expand Down Expand Up @@ -49,8 +40,7 @@ export const buildRateLimitConfig = (redis: Redis): RateLimitPluginOptions => ({
keyGenerator: (request) => request.apiKey?.id || request.ip,

// The maximum number of requests a single client can perform inside a time window.
// eslint-disable-next-line @typescript-eslint/typedef
async max(request) {
async max(request: FastifyRequest) {
const rl = request.apiKey?.rateLimitPerWindow || 5;
request.log.debug(`This request is rate-limited to ${rl} requests per 2 minute window`);

Expand Down Expand Up @@ -81,3 +71,5 @@ export const buildRateLimitConfig = (redis: Redis): RateLimitPluginOptions => ({
};
},
});

export default buildRateLimitConfig;
1 change: 0 additions & 1 deletion apps/authproxy/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// eslint-disable-next-line dot-notation
export const secretSalt: string = process.env["SECRET_SALT"] || "";
6 changes: 4 additions & 2 deletions apps/authproxy/src/entity/api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "reflect-metadata";

import type { Scope } from "../auth/scope-permission.js";

import { v4 as uuidv4 } from "uuid";
import { v4 as uuidV4 } from "uuid";
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, Generated, CreateDateColumn } from "typeorm";

@Entity()
Expand Down Expand Up @@ -33,9 +33,11 @@ export class ApiKey extends BaseEntity {
public apiKey: string;

public async roll(): Promise<string> {
this.apiKey = uuidv4();
this.apiKey = uuidV4();
this.lastUsed = undefined;
await this.save();
return this.apiKey;
}
}

export default ApiKey;
14 changes: 8 additions & 6 deletions apps/authproxy/src/logger-options.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import type { FastifyLoggerOptions } from "fastify";
import type { ResSerializerReply } from "fastify/types/logger.js";
import type { FastifyLoggerOptions, FastifyReply, FastifyRequest, RawServerDefault } from "fastify";

const loggerOptions: FastifyLoggerOptions & { redact: string[] } = {
export const loggerOptions: FastifyLoggerOptions & { redact: string[] } = {
level: "trace",
redact: ["req.headers.authorization"],
serializers: {
// eslint-disable-next-line @typescript-eslint/typedef
res(reply) {
res(
reply: ResSerializerReply<RawServerDefault, FastifyReply>
): ResSerializerReply<RawServerDefault, FastifyReply> {
return {
statusCode: reply.statusCode,
};
},
// eslint-disable-next-line @typescript-eslint/typedef
req(request) {

req(request: FastifyRequest) {
return {
method: request.method,
url: request.url,
Expand Down
18 changes: 0 additions & 18 deletions apps/authproxy/src/plugins/bad-request.ts

This file was deleted.

32 changes: 16 additions & 16 deletions apps/authproxy/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@ import { Redis } from "ioredis";
import { fastify } from "fastify";
import { DataSource } from "typeorm";
import closeWithGrace from "close-with-grace";
import fastifyRateLimit from "@fastify/rate-limit";
import { fastifyRateLimit } from "@fastify/rate-limit";
import { fastifyFormbody } from "@fastify/formbody";
import { fastifyUnderPressure } from "@fastify/under-pressure";

import api_v1 from "./routes/v1/api.js";
import version from "./routes/version.js";
import { ApiKey } from "./entity/api-key.js";
import ApiKey from "./entity/api-key.js";
import loggerOptions from "./logger-options.js";
import badRequest from "./plugins/bad-request.js";
import { buildRateLimitConfig } from "./auth/rate-limit.js";
import { buildUnderPressureConfig } from "./plugins/under-pressure.js";
import buildRateLimitConfig from "./auth/rate-limit.js";
import buildUnderPressureConfig from "./under-pressure.js";

// Fastify app and redis
const app = fastify({ logger: loggerOptions, trustProxy: true, ignoreTrailingSlash: true });
const redis = new Redis(process.env["REDIS_URL"]!, { connectTimeout: 500, maxRetriesPerRequest: 1 });

// Fastify plugins
await app.register(fastifyFormbody);
await app.register(fastifyRateLimit, buildRateLimitConfig(redis));
await app.register(fastifyUnderPressure, buildUnderPressureConfig());
await app.register(fastifyRateLimit.default, buildRateLimitConfig(redis));

// My plugins
await app.register(badRequest);

// API routes
await app.register(version);
await app.register(api_v1, { url: "/" });
await app.register(api_v1, { url: "/v1" });

// Version route
app.get(
"/version",
() => `Running in NODE_ENV -> ${process.env["NODE_ENV"]}\nRunning from GIT_SHA -> ${process.env["GIT_SHA"]}`
);

// Create the database connection and bind the app to the port,
// then register the graceful shutdown handler
const postgres = await new DataSource({
Expand All @@ -41,12 +41,12 @@ const postgres = await new DataSource({
entities: [ApiKey],
}).initialize();

await app.listen({ port: Number.parseInt(process.env["PORT"] || "5000", 10), host: "0.0.0.0" });
// Start the fastify server
await app.listen({ port: Number.parseInt(process.env["PORT"] || "5000"), host: "0.0.0.0" });
await app.after();

closeWithGrace({ delay: 500 }, async ({ err }) => {
if (err) {
console.error(err);
}
// Close the postgres connection when the server shuts down
closeWithGrace({ delay: 2000 }, async ({ err }) => {
if (err) console.error(err);
await postgres.destroy();
});
2 changes: 1 addition & 1 deletion apps/authproxy/src/routes/v1/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { QuerystringSchema as QuerystringSchemaInterface } from "../types/q

// Fastify routing for api v1
// eslint-disable-next-line @rushstack/typedef-var
const api_v1 = fp<{ url: string }>(
export const api_v1 = fp<{ url: string }>(
async (fastify: FastifyInstance, options: { url: string | undefined }): Promise<void> => {
fastify.route<{
Querystring: QuerystringSchemaInterface;
Expand Down
Loading

0 comments on commit d8eb715

Please sign in to comment.