diff --git a/package.json b/package.json index a974768..9ece98f 100644 --- a/package.json +++ b/package.json @@ -34,12 +34,12 @@ "node": ">=18.0" }, "dependencies": { - "@discord-tickets/settings": "^1.0.0", "@eartharoid/dbf": "^0.3.3", "@eartharoid/dtf": "^2.0.1", "@eartharoid/i18n": "^1.0.4", "@fastify/cookie": "^6.0.0", "@fastify/cors": "^8.1.0", + "@fastify/http-proxy": "^8.2.2", "@fastify/jwt": "^5.0.1", "@fastify/oauth2": "^5.1.0", "@prisma/client": "^4.3.1", diff --git a/scripts/postinstall.js b/scripts/postinstall.js index bd0cdc5..3b541d1 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -2,7 +2,6 @@ require('dotenv').config(); const fs = require('fs-extra'); const { spawnSync } = require('child_process'); - const providers = ['mysql', 'postgresql', 'sqlite']; const provider = process.env.DB_PROVIDER; diff --git a/scripts/preinstall.js b/scripts/preinstall.js index fb82431..10fd114 100644 --- a/scripts/preinstall.js +++ b/scripts/preinstall.js @@ -2,16 +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', PORTAL: '', PUBLIC_BOT: false, - SETTINGS_BIND: 80, + SETTINGS_BIND: 8888, SUPER: '319467558166069248', }; diff --git a/src/http.js b/src/http.js index 953714d..30355b8 100644 --- a/src/http.js +++ b/src/http.js @@ -4,7 +4,7 @@ const { short } = require('leeks.js'); const { join } = require('path'); const { files } = require('node-dir'); -process.env.PUBLIC_HOST = process.env.API_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST` +process.env.PUBLIC_HOST = process.env.HTTP_EXTERNAL; // the SvelteKit app expects `PUBLIC_HOST` module.exports = async client => { // cors plugin @@ -16,7 +16,7 @@ module.exports = async client => { // oauth2 plugin fastify.register(oauth, { - callbackUri: `${process.env.API_EXTERNAL}/auth/callback`, + callbackUri: `${process.env.HTTP_EXTERNAL}/auth/callback`, credentials: { auth: oauth.DISCORD_CONFIGURATION, client: { @@ -41,6 +41,14 @@ module.exports = async client => { secret: process.env.ENCRYPTION_KEY, }); + // proxy /settings to express + fastify.register(require('@fastify/http-proxy'), { + http2: false, + prefix: '/settings', + rewritePrefix: '/settings', + upstream: `http://127.0.0.1:${process.env.SETTINGS_BIND}`, + }); + // auth fastify.decorate('authenticate', async (req, res) => { try { @@ -54,6 +62,8 @@ module.exports = async client => { }); } } catch (err) { + console.log(req); + console.log(req.cookies); res.send(err); } }); @@ -116,15 +126,15 @@ module.exports = async client => { : responseTime >= 5 ? '&e' : '&a') + responseTime + 'ms'; - client.log.info.http(short(`API ${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`)); + client.log.info.http(short(`${req.id} ${req.ip} ${req.method} ${req.routerPath ?? '*'} &m-+>&r ${status}&b in ${responseTime}`)); + if (!req.routerPath) client.log.verbose.http(`${req.id} ${req.method} ${req.url}`); done(); }); - fastify.addHook('onError', async (req, res, err) => client.log.error.http(err)); + fastify.addHook('onError', async (req, res, err) => client.log.error.http(req.id, err)); // route loading const dir = join(__dirname, '/routes'); - files(dir, { exclude: /^\./, match: /.js$/, @@ -146,24 +156,21 @@ module.exports = async client => { })); // register route }); - // 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}`); - }); - + // express server for settings 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) + client.log.verbose.http(short(`Express ${req.ip} ${req.method} ${req.route?.path ?? req.path}`)); }); - express.use(handler); - express.listen(process.env.SETTINGS_BIND, () => { - client.log.success.http(`SETTINGS Listening at ${process.env.SETTINGS_BIND}`); + express.use(handler); // let SvelteKit handle everything + express.listen(process.env.SETTINGS_BIND, () => { // start the express server + client.log.verbose.http(`Express listening on port ${process.env.SETTINGS_BIND}`); + }); + + // start the fastify 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}`); }); }; \ No newline at end of file diff --git a/src/lib/http.js b/src/lib/http.js index aee58e9..9dbcdf2 100644 --- a/src/lib/http.js +++ b/src/lib/http.js @@ -1 +1 @@ -module.exports.domain = process.env.API_EXTERNAL.match(/http(s?):\/\/(?[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain; \ No newline at end of file +module.exports.domain = process.env.HTTP_EXTERNAL.match(/http(s?):\/\/(?[a-zA-Z0-9\-_.]+)(:\d+)?/).groups.domain; \ No newline at end of file diff --git a/src/lib/logger.js b/src/lib/logger.js index 9ba59e7..e30e15e 100644 --- a/src/lib/logger.js +++ b/src/lib/logger.js @@ -10,6 +10,7 @@ const colours = { info: ['&3', '&b'], notice: ['&!6&0', '&!6&0'], success: ['&2', '&a'], + verbose: ['&7', '&f'], warn: ['&6', '&e'], }; diff --git a/src/routes/auth/callback.js b/src/routes/auth/callback.js index 400fcb3..328d3a8 100644 --- a/src/routes/auth/callback.js +++ b/src/routes/auth/callback.js @@ -23,6 +23,6 @@ module.exports.get = () => ({ sameSite: true, secure: false, }) - .redirect('/'); + .redirect('/settings'); }, }); \ No newline at end of file diff --git a/src/routes/index.js b/src/routes/index.js index 0a10ed6..203bc5b 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -2,5 +2,6 @@ module.exports.get = () => ({ handler: (req, res) => { const { client } = res.context.config; return `Hello, I am ${client.user.username}!`; + // res.redirect(process.env.SETTINGS_EXTERNAL); }, }); \ No newline at end of file diff --git a/src/stdin/settings.js b/src/stdin/settings.js new file mode 100644 index 0000000..187f972 --- /dev/null +++ b/src/stdin/settings.js @@ -0,0 +1,14 @@ +const { StdinCommand } = require('@eartharoid/dbf'); + +module.exports = class extends StdinCommand { + constructor(client, options) { + super(client, { + ...options, + id: 'settings', + }); + } + + async run() { + this.client.log.info.settings(process.env.HTTP_EXTERNAL + '/settings'); + } +}; \ No newline at end of file