tangled
alpha
login
or
join now
thevoid.cafe
/
voidy
0
fork
atom
A powerful and extendable Discord bot, with it's own module system :3
thevoid.cafe/projects/voidy
0
fork
atom
overview
issues
pulls
pipelines
✨🚧 Testing some very dubious separation of bot/api, EXPERIMENTAL
Jo
2 months ago
2be84c91
de3ac557
+121
7 changed files
expand all
collapse all
unified
split
packages
api
.env.example
Dockerfile
index.ts
middlewares
isAuthenticated.ts
package.json
routes
api
v1
currency.ts
index.ts
+2
packages/api/.env.example
···
1
1
+
DB_URI: #Your MongoDB connection URI - REQUIRED
2
2
+
ACCESS_TOKEN: # Your Bearer token
+12
packages/api/Dockerfile
···
1
1
+
FROM oven/bun:1.1
2
2
+
3
3
+
WORKDIR /app
4
4
+
5
5
+
COPY . .
6
6
+
7
7
+
RUN bun install
8
8
+
9
9
+
WORKDIR /app/packages/api
10
10
+
11
11
+
EXPOSE 3000
12
12
+
CMD ["bun", "run", "dev"]
+25
packages/api/index.ts
···
1
1
+
import { Hono } from "hono";
2
2
+
import { v1 } from "./routes/api/v1";
3
3
+
import { connect } from "mongoose";
4
4
+
5
5
+
// Instantiate Hono
6
6
+
const app = new Hono();
7
7
+
8
8
+
// Database URI validation and connection check
9
9
+
if (!Bun.env.DB_URI) throw new Error("[Voidy] Missing database URI");
10
10
+
await connect(Bun.env.DB_URI)
11
11
+
.then(() => {
12
12
+
console.log("Connected to database");
13
13
+
})
14
14
+
.catch((error) => {
15
15
+
console.error("Failed to connect to database:", error);
16
16
+
});
17
17
+
18
18
+
// Define routes
19
19
+
app.route("/api/v1", v1);
20
20
+
21
21
+
// Export app configuration
22
22
+
export default {
23
23
+
fetch: app.fetch,
24
24
+
port: Bun.env.PORT || 4300,
25
25
+
}
+22
packages/api/middlewares/isAuthenticated.ts
···
1
1
+
import type { MiddlewareHandler } from "hono";
2
2
+
3
3
+
export const isAuthenticated: MiddlewareHandler = async (c, next) => {
4
4
+
const auth = c.req.header("authorization");
5
5
+
6
6
+
if (!auth || !auth.startsWith("Bearer ")) {
7
7
+
return c.json({ error: "Unauthorized" }, 401);
8
8
+
}
9
9
+
10
10
+
const token = auth.slice("Bearer ".length);
11
11
+
12
12
+
if (!Bun.env.ACCESS_TOKEN) {
13
13
+
console.error("ACCESS_TOKEN environment variable is not set");
14
14
+
return c.json({ error: "Internal server error" }, 500);
15
15
+
}
16
16
+
17
17
+
if (token !== Bun.env.ACCESS_TOKEN) {
18
18
+
return c.json({ error: "Invalid token" }, 401);
19
19
+
}
20
20
+
21
21
+
await next();
22
22
+
};
+21
packages/api/package.json
···
1
1
+
{
2
2
+
"name": "@voidy/api",
3
3
+
"version": "0.1.0",
4
4
+
"module": "src/index.ts",
5
5
+
"type": "module",
6
6
+
"private": true,
7
7
+
"scripts": {
8
8
+
"dev": "bun --watch ."
9
9
+
},
10
10
+
"devDependencies": {
11
11
+
"@types/bun": "latest"
12
12
+
},
13
13
+
"peerDependencies": {
14
14
+
"typescript": "^5.9.2"
15
15
+
},
16
16
+
"dependencies": {
17
17
+
"@voidy/bot": "workspace:*",
18
18
+
"hono": "^4.11.1",
19
19
+
"mongoose": "^9.0.1"
20
20
+
}
21
21
+
}
+33
packages/api/routes/api/v1/currency.ts
···
1
1
+
import { Hono } from "hono";
2
2
+
import { UserCurrency, UserCurrencyType, UserIntegration } from "@voidy/bot/db"
3
3
+
import { isAuthenticated } from "../../../middlewares/isAuthenticated";
4
4
+
5
5
+
export const currency = new Hono();
6
6
+
7
7
+
async function findUserId(serviceType: string, serviceId: string) {
8
8
+
const integration = await UserIntegration.findOne({
9
9
+
service: {
10
10
+
type: serviceType,
11
11
+
id: serviceId,
12
12
+
}
13
13
+
});
14
14
+
15
15
+
if (!integration) return null;
16
16
+
17
17
+
return integration.userId;
18
18
+
}
19
19
+
20
20
+
currency.get("/", isAuthenticated, async (c) => {
21
21
+
const serviceType = c.req.param("serviceType");
22
22
+
const serviceId = c.req.param("serviceId");
23
23
+
24
24
+
if (!serviceId || !serviceType) return c.json({ error: "Missing serviceId or serviceType" }, 400);
25
25
+
26
26
+
const userId = await findUserId(serviceType, serviceId);
27
27
+
if (!userId) return c.json({ error: "User not found" }, 404);
28
28
+
29
29
+
const userCurrency = await UserCurrency.findOne({ userId, type: UserCurrencyType.BITS });
30
30
+
if (!userCurrency) return c.json({ error: "User currency not found" }, 404);
31
31
+
32
32
+
return c.json(userCurrency);
33
33
+
});
+6
packages/api/routes/api/v1/index.ts
···
1
1
+
import { Hono } from "hono";
2
2
+
import { currency } from "./currency";
3
3
+
4
4
+
export const v1 = new Hono();
5
5
+
6
6
+
v1.route("/currency", currency);