Skip to content

Commit

Permalink
Migrate to hono
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya committed Apr 15, 2024
1 parent 0db442b commit dfcc884
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 953 deletions.
Binary file modified bun.lockb
Binary file not shown.
754 changes: 8 additions & 746 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@
"dependencies": {
"@prisma/client": "5.12.1",
"@types/body-parser": "^1.19.5",
"@types/express": "^4.17.21",
"axios": "^1.6.8",
"body-parser": "^1.20.2",
"croner": "^8.0.2",
"date-fns": "^3.6.0",
"discord.js": "^14.14.1",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"form-data": "^4.0.0",
"gpt3-tokenizer": "^1.1.5",
"helmet": "^7.1.0",
"hono": "^4.2.4",
"lodash": "^4.17.21",
"openai": "^4.33.1",
"pino": "^8.20.0",
Expand All @@ -46,7 +43,6 @@
"tmp-promise": "^3.0.3"
},
"devDependencies": {
"@types/express-serve-static-core": "^4.19.0",
"@types/lodash": "^4.17.0",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^7.6.0",
Expand Down
41 changes: 16 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
import express from 'express';
import { resolve } from 'path';
import bodyParser from 'body-parser';
import helmet from 'helmet';
import { Client } from '@/lib/module-loader';
import { ActivityType, GatewayIntentBits, Partials } from 'discord.js';
import config from '@/config';
import { runFromSrc } from '@/utils/runFromSrc';
import { registerRoutes } from '@/webhooks';
console.log('runFromSrc', runFromSrc);
const app: express.Application = express();
const port: number = parseInt(process.env.PORT || '3000');
import { Hono } from 'hono';
import { logger } from 'hono/logger';

app.use(helmet());
app.use(bodyParser.json());
const app = new Hono();
app.use(logger());
const port: number = parseInt(process.env.PORT || '3000');

const client = new Client({
moduleLoader: {
eventsDir: resolve(__dirname, runFromSrc ? './events' : '../dist/events'),
commandsDir: resolve(
__dirname,
runFromSrc ? './commands' : '../dist/commands',
),
eventsDir: resolve(__dirname, './events'),
commandsDir: resolve(__dirname, './commands'),
validationsDir: resolve(__dirname, './validations'),
},
intents: [
GatewayIntentBits.Guilds,
Expand All @@ -40,30 +34,22 @@ async function initializeClient() {
// Register routes
async function registerWebhookRoutes() {
const router = await registerRoutes(client);
app.use('/webhooks', router);
app.route('/webhooks', router);
}

// Health check endpoint
app.get('/health', (req, res) => {
res.send('OK');
});
app.get('/health', (c) => c.text('OK'));

// Start server
async function startServer() {
try {
await initializeClient();
await registerWebhookRoutes();

const server = app.listen(port, () => {
console.log(`App listening at ${config.bot.base_url}`);
});

process.on('SIGINT', () => {
console.log('\nGracefully shutting down');

server.close(() => {
console.log('Express server closed');
});
client.destroy();

process.exit();
});
Expand All @@ -73,3 +59,8 @@ async function startServer() {
}

startServer();

export default {
port,
fetch: app.fetch,
};
1 change: 1 addition & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export function buildContext(
export function buildThreadContext(
messages: Collection<string, Message>,
userMessage: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
botId: string,
): Array<OpenAI.Chat.ChatCompletionMessageParam> {
if (messages.size === 0) {
Expand Down
Loading

0 comments on commit dfcc884

Please sign in to comment.