Skip to content

Commit

Permalink
fixies
Browse files Browse the repository at this point in the history
  • Loading branch information
akpi816218 committed May 3, 2024
1 parent 9468d48 commit d96973a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 49 deletions.
33 changes: 12 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"@deno/kv": "^0.7.0",
"@discordjs/collection": "^2.0.0",
"@napi-rs/canvas": "^0.1.52",
"cors": "^2.8.5",
"discord.js": "^14.14.1",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"helmet": "^7.1.0",
"jsoning": "^1.0.1",
"node-fetch": "^3.3.2",
"node-schedule": "^2.1.1",
Expand Down Expand Up @@ -52,4 +52,4 @@
"up": "ncu -u && npm i",
"prepare": "husky"
}
}
}
32 changes: 18 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { logger } from './logger';
import { readdirSync } from 'fs';
import { scheduleJob } from 'node-schedule';
import { RecurrenceSpecObjLit, scheduleJob } from 'node-schedule';
import { openKv } from '@deno/kv';
import { Jsoning } from 'jsoning';
import { BirthdayData } from './struct/database';
Expand Down Expand Up @@ -81,10 +81,15 @@ const server = createServer(
route: '/invite'
},
{
handler: (_req, res) => res.sendStatus(client.isReady() ? 200 : 503),
handler: (_req, res) => res.redirect('/status'),
method: Methods.GET,
route: '/'
},
{
handler: (_req, res) => res.sendStatus(client.isReady() ? 200 : 503),
method: Methods.GET,
route: '/status'
},
{
handler: (req, res) => {
if (
Expand All @@ -94,10 +99,6 @@ const server = createServer(
res.status(415).end();
else if (client.isReady())
res
.header({
'Access-Control-Allow-Origin': 'https://discog.opensourceforce.net',
Vary: 'Origin'
})
.status(200)
.contentType('application/json')
.send({
Expand All @@ -113,7 +114,7 @@ const server = createServer(
else res.status(503).end();
},
method: Methods.GET,
route: '/api/bot'
route: '/bot'
},
{
handler: (req, res) => {
Expand All @@ -124,10 +125,6 @@ const server = createServer(
res.status(415).end();
else if (client.isReady())
res
.header({
'Access-Control-Allow-Origin': 'https://discog.opensourceforce.net',
Vary: 'Origin'
})
.status(200)
.contentType('application/json')
.send({
Expand All @@ -141,7 +138,7 @@ const server = createServer(
else res.status(503).end();
},
method: Methods.GET,
route: '/api/commands'
route: '/commands'
}
);
logger.debug('Created server instance.');
Expand Down Expand Up @@ -307,8 +304,15 @@ process.on('SIGINT', () => {
process.exit(0);
});

// Schedule the bdayInterval function to run every day at 12:00 AM PST for a server running 7 hours ahead of PST
scheduleJob('0 7 * * *', () => bdayInterval().catch(e => logger.error(e)));
// Schedule the bdayInterval function to run every day at 12:00 AM PST
scheduleJob(
{
tz: 'America/Los_Angeles',
hour: 0,
minute: 0
} satisfies RecurrenceSpecObjLit,
() => bdayInterval().catch(e => logger.error(e))
);
logger.debug('Scheduled birthday interval.');

server.listen(process.env.PORT ?? PORT);
Expand Down
17 changes: 5 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import express, { Request, Response } from 'express';
import cors from 'cors';
import helmet from 'helmet';

export enum Methods {
DELETE = 'delete',
GET = 'get',
/** @public */
HEAD = 'head',
PATCH = 'patch',
/** @public */
POST = 'post',
/** @public */
PUT = 'put'
}

Expand All @@ -26,14 +23,10 @@ export function createServer(...routes: Route[]) {
}
// cors
app.use(
cors({
origin: 'https://discog.opensourceforce.net/',
methods: [Methods.DELETE, Methods.GET, Methods.PATCH].map(e =>
e.toUpperCase()
),
maxAge: 86400,
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true
helmet({
crossOriginResourcePolicy: {
policy: 'same-site'
}
})
);
return app;
Expand Down

0 comments on commit d96973a

Please sign in to comment.