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_PROVIDER=mysql
ENCRYPTION_KEY=
HTTP_BIND=8080
HTTP_EXTERNAL=http://localhost:8080
API_BIND=8080
API_EXTERNAL=http://localhost:8080
PORTAL=http://localhost:3000
SUPER=

View File

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

View File

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