From fdce924b22cc51b68c5bcf24d34cdec1a8b64a13 Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 6 Sep 2022 20:46:18 +0100 Subject: [PATCH] Complete settings integration --- README.md | 4 ++-- scripts/preinstall.js | 5 +++-- src/http.js | 46 +++++++++++++++++-------------------------- src/lib/http.js | 2 +- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 897d855..18e5312 100644 --- a/README.md +++ b/README.md @@ -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= diff --git a/scripts/preinstall.js b/scripts/preinstall.js index 5085603..fb82431 100644 --- a/scripts/preinstall.js +++ b/scripts/preinstall.js @@ -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', }; diff --git a/src/http.js b/src/http.js index 9466ba7..953714d 100644 --- a/src/http.js +++ b/src/http.js @@ -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}`); }); }; \ No newline at end of file diff --git a/src/lib/http.js b/src/lib/http.js index 9dbcdf2..aee58e9 100644 --- a/src/lib/http.js +++ b/src/lib/http.js @@ -1 +1 @@ -module.exports.domain = process.env.HTTP_EXTERNAL.match(/http(s?):\/\/(?[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain; \ No newline at end of file +module.exports.domain = process.env.API_EXTERNAL.match(/http(s?):\/\/(?[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain; \ No newline at end of file