Complete settings integration

This commit is contained in:
Isaac 2022-09-06 20:46:18 +01:00
parent c101562dcc
commit fdce924b22
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
4 changed files with 24 additions and 33 deletions

View File

@ -3,8 +3,8 @@ DISCORD_TOKEN=
DB_CONNECTION_URL="mysql://test:password@localhost/tickets0" DB_CONNECTION_URL="mysql://test:password@localhost/tickets0"
DB_PROVIDER=mysql DB_PROVIDER=mysql
ENCRYPTION_KEY= ENCRYPTION_KEY=
HTTP_BIND=8080 API_BIND=8080
HTTP_EXTERNAL=http://localhost:8080 API_EXTERNAL=http://localhost:8080
PORTAL=http://localhost:3000 PORTAL=http://localhost:3000
SUPER= SUPER=

View File

@ -2,15 +2,16 @@ const { randomBytes } = require('crypto');
const fs = require('fs'); const fs = require('fs');
const env = { const env = {
API_BIND: 8080,
API_EXTERNAL: 'http://localhost:8080',
DB_CONNECTION_URL: '', DB_CONNECTION_URL: '',
DB_PROVIDER: '', // don't default to sqlite, postinstall checks if empty DB_PROVIDER: '', // don't default to sqlite, postinstall checks if empty
DISCORD_SECRET: '', DISCORD_SECRET: '',
DISCORD_TOKEN: '', DISCORD_TOKEN: '',
ENCRYPTION_KEY: randomBytes(24).toString('hex'), ENCRYPTION_KEY: randomBytes(24).toString('hex'),
HTTP_BIND: 8080,
HTTP_EXTERNAL: 'http://localhost:8080',
PORTAL: '', PORTAL: '',
PUBLIC_BOT: false, PUBLIC_BOT: false,
SETTINGS_BIND: 80,
SUPER: '319467558166069248', SUPER: '319467558166069248',
}; };

View File

@ -1,15 +1,12 @@
const fastify = require('fastify')(); const fastify = require('fastify')();
const oauth = require('@fastify/oauth2'); const oauth = require('@fastify/oauth2');
// const { randomBytes } = require('crypto');
const { short } = require('leeks.js'); const { short } = require('leeks.js');
const { join } = require('path'); const { join } = require('path');
const { files } = require('node-dir'); const { files } = require('node-dir');
process.env.PUBLIC_HOST = process.env.HTTP_EXTERNAL; process.env.PUBLIC_HOST = process.env.API_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST`
async function build(client) {
// await fastify.register(require('@fastify/express'));
module.exports = async client => {
// cors plugin // cors plugin
fastify.register(require('@fastify/cors'), { fastify.register(require('@fastify/cors'), {
credentials: true, credentials: true,
@ -19,7 +16,7 @@ async function build(client) {
// oauth2 plugin // oauth2 plugin
fastify.register(oauth, { fastify.register(oauth, {
callbackUri: `${process.env.HTTP_EXTERNAL}/auth/callback`, callbackUri: `${process.env.API_EXTERNAL}/auth/callback`,
credentials: { credentials: {
auth: oauth.DISCORD_CONFIGURATION, auth: oauth.DISCORD_CONFIGURATION,
client: { client: {
@ -41,7 +38,6 @@ async function build(client) {
cookieName: 'token', cookieName: 'token',
signed: false, signed: false,
}, },
// secret: randomBytes(16).toString('hex'),
secret: process.env.ENCRYPTION_KEY, secret: process.env.ENCRYPTION_KEY,
}); });
@ -49,7 +45,6 @@ async function build(client) {
fastify.decorate('authenticate', async (req, res) => { fastify.decorate('authenticate', async (req, res) => {
try { try {
const data = await req.jwtVerify(); const data = await req.jwtVerify();
// if (data.payload.expiresAt < Date.now()) res.redirect('/auth/login');
if (data.payload.expiresAt < Date.now()) { if (data.payload.expiresAt < Date.now()) {
return res.code(401).send({ return res.code(401).send({
error: 'Unauthorised', error: 'Unauthorised',
@ -121,7 +116,7 @@ async function build(client) {
: responseTime >= 5 : responseTime >= 5
? '&e' ? '&e'
: '&a') + responseTime + 'ms'; : '&a') + responseTime + 'ms';
client.log.info.http(short(`${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`)); client.log.info.http(short(`API ${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`));
done(); done();
}); });
@ -129,6 +124,7 @@ async function build(client) {
// route loading // route loading
const dir = join(__dirname, '/routes'); const dir = join(__dirname, '/routes');
files(dir, { files(dir, {
exclude: /^\./, exclude: /^\./,
match: /.js$/, match: /.js$/,
@ -141,6 +137,7 @@ async function build(client) {
.replace(/\[(\w+)\]/gi, ':$1') // convert [] to : .replace(/\[(\w+)\]/gi, ':$1') // convert [] to :
.replace('/index', '') || '/'; // remove index .replace('/index', '') || '/'; // remove index
const route = require(file); const route = require(file);
Object.keys(route).forEach(method => fastify.route({ Object.keys(route).forEach(method => fastify.route({
config: { client }, config: { client },
method: method.toUpperCase(), method: method.toUpperCase(),
@ -149,31 +146,24 @@ async function build(client) {
})); // register route })); // register route
}); });
// const { handler: app } = await import('@discord-tickets/settings/build/handler.js'); // start server
// fastify.use('/*', app); fastify.listen({ port: process.env.API_BIND }, (err, addr) => {
// fastify.use(app); if (err) client.log.error.http(err);
else client.log.success.http(`API Listening at ${addr}`);
return fastify; });
}
module.exports = async client => {
build(client)
.then(fastify => {
// start server
fastify.listen({ port: process.env.HTTP_BIND }, (err, addr) => {
if (err) client.log.error.http(err);
else client.log.success.http(`Listening at ${addr}`);
});
})
.catch(client.log.error.http);
const express = require('express')(); const express = require('express')();
const { handler } = await import('@discord-tickets/settings/build/handler.js'); const { handler } = await import('@discord-tickets/settings/build/handler.js');
express.get('/api/client', (req, res) => { express.get('/api/client', (req, res) => {
res.end('ok'); res.end('ok');
}); });
express.use((req, res, next) => {
next();
// req.route?.path ?? '*'
client.log.verbose.http(short(`SETTINGS ${req.ip} ${req.method} ${req.path}`)); // verbose because little information & SvelteKit is very spammy (lots of routes)
});
express.use(handler); express.use(handler);
express.listen(3000, () => { express.listen(process.env.SETTINGS_BIND, () => {
console.log('listening on port 3000'); client.log.success.http(`SETTINGS Listening at ${process.env.SETTINGS_BIND}`);
}); });
}; };

View File

@ -1 +1 @@
module.exports.domain = process.env.HTTP_EXTERNAL.match(/http(s?):\/\/(?<domain>[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain; module.exports.domain = process.env.API_EXTERNAL.match(/http(s?):\/\/(?<domain>[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain;