Skip to content

Commit

Permalink
rewrite: use bun
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Dec 11, 2023
1 parent 815befb commit 51a845c
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 2,258 deletions.
25 changes: 0 additions & 25 deletions .eslintrc.js

This file was deleted.

40 changes: 0 additions & 40 deletions .github/workflows/ci.yml

This file was deleted.

10 changes: 0 additions & 10 deletions Dockerfile

This file was deleted.

1 change: 0 additions & 1 deletion _config.yml

This file was deleted.

Binary file added bun.lockb
Binary file not shown.
6 changes: 0 additions & 6 deletions docker-compose.dev.yml

This file was deleted.

19 changes: 0 additions & 19 deletions docker-compose.yml

This file was deleted.

6 changes: 0 additions & 6 deletions nodemon.json

This file was deleted.

31 changes: 4 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,17 @@
"name": "guilded-shields",
"version": "1.0.0",
"description": "SVG shields for providing metadata about Guilded Servers.",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"dev": "yarn build && node dist/index.js",
"start:dev": "npx nodemon",
"format": "prettier --write **/*.{ts,js,json,yml,yaml}",
"lint": "npx eslint src/",
"lint:fix": "npx eslint src/ --fix",
"test": "echo \"pass through\" && exit 0"
"format": "prettier --write **/*.{ts,json}"
},
"author": "Zaid \"Nico\"",
"license": "MIT",
"devDependencies": {
"@sapphire/eslint-config": "^4.3.0",
"@types/node": "^15.0.2",
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-config-typescript": "^3.0.0",
"eslint-plugin-prettier": "^4.0.0",
"nodemon": "^2.0.14",
"prettier": "^2.3.0",
"rimraf": "^3.0.2",
"typescript": "^4.3.2"
"prettier": "^3.1.1"
},
"dependencies": {
"@redis/client": "^1.2.0",
"badge-maker": "^3.3.1",
"dotenv": "^8.2.0",
"fastify": "^3.15.1",
"fastify-rate-limit": "^5.5.0",
"node-fetch": "^2.6.7",
"redis": "^4.0.3"
"elysia": "^0.7.30",
"elysia-rate-limit": "^2.0.1"
}
}
36 changes: 0 additions & 36 deletions src/colors.ts

This file was deleted.

81 changes: 0 additions & 81 deletions src/controllers/shield.ts

This file was deleted.

97 changes: 65 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,72 @@
import "dotenv/config";
import fastify, { FastifyInstance } from "fastify";
import { getServerShield } from "./routes/shields";
import type { Server, IncomingMessage, ServerResponse } from "http";
import { createClient, RedisClientType, RedisModules } from "@redis/client";
import { BadgeGetReq } from "./types";
const server: FastifyInstance = fastify<
Server,
IncomingMessage,
ServerResponse
>();
const redis = createClient({
url: process.env.REDIS_URL ?? undefined,
}) as RedisClientType<RedisModules>;

// eslint-disable-next-line @typescript-eslint/no-var-requires
server.register(require("fastify-rate-limit"), {
max: 100,
timeWindow: "1 minute",
});
import { Elysia, t } from "elysia";
import { rateLimit } from "elysia-rate-limit";
import { generateSvg, getMemberCount } from "./shields";

const app = new Elysia();
app.use(rateLimit());

app.get(
"/shields/:type/:inviteId",
async (ctx) => {
const { type, inviteId } = ctx.params;
const { color, style } = ctx.query;

server.get("/shields/:type/:inviteId", (req: BadgeGetReq, res) =>
getServerShield(redis, req, res).catch(console.error),
const memberCount = await getMemberCount(inviteId, type);
const svg = await generateSvg({
message: `${memberCount} members`,
style,
color: color ?? "black",
});

ctx.set = {
headers: {
"Content-Type": "image/svg+xml",
},
};
return svg;
},
{
query: t.Object({
color: t.Optional(t.String()),
style: t.Optional(t.String()),
}),
params: t.Object({
type: t.Union([
t.Literal("i"),
t.Literal("r"),
t.Literal("vanity"),
]),
inviteId: t.String(),
}),
},
);

server.all("*", {}, (_req, res) => {
res.status(404).send({
app.onError((ctx) => {
if (ctx.error.message === "NOT_FOUND") {
return {
failed: true,
status: "NOT_FOUND",
error: { message: "404: Route not found." },
};
}

if (ctx.code == "VALIDATION") {
return {
failed: true,
status: "INVALID_REQUEST",
error: {
message: ctx.error.message,
},
};
}

return {
failed: true,
status: "NOT_FOUND",
error: { message: "404: Route not found." },
});
return void 0;
status: "INTERNAL_ERROR",
error: { message: "There was an internal error." },
};
});

server.listen(process.env.PORT ?? 80, "0.0.0.0", async (e) => {
if (e) throw e;
await redis.connect();
return console.log("Server started!");
app.listen(7777, () => {
console.log("Server running!");
});
Loading

0 comments on commit 51a845c

Please sign in to comment.