diff --git a/.eslintrc.json b/.eslintrc.json index c247600..e472619 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -124,7 +124,7 @@ "error" ], "multiline-comment-style": [ - "warn" + "off" ], "no-console": [ "off" diff --git a/README.md b/README.md index ff369df..1cb051d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ CONFIG_PATH=./user/config.yml DISCORD_TOKEN= DB_ENCRYPTION_KEY= -DB_CONNECTION_URL="" \ No newline at end of file +DB_CONNECTION_URL="" +HTTP_BIND=3000 \ No newline at end of file diff --git a/package.json b/package.json index 33d8694..c9013cb 100644 --- a/package.json +++ b/package.json @@ -35,9 +35,11 @@ "@prisma/client": "^3.13.0", "discord.js": "^13.6.0", "dotenv": "^16.0.0", + "fastify": "^3.29.0", "figlet": "^1.5.2", "leeks.js": "^0.2.4", "leekslazylogger": "^4.1.7", + "node-dir": "^0.1.17", "semver": "^7.3.7", "terminal-link": "^2.1.1", "yaml": "^1.10.2" diff --git a/src/http.js b/src/http.js new file mode 100644 index 0000000..387d5f3 --- /dev/null +++ b/src/http.js @@ -0,0 +1,37 @@ +const fastify = require('fastify')(); +const { readFiles } = require('node-dir'); +const { join } = require('path'); + +module.exports = client => { + const dir = join(__dirname, '/routes'); + + readFiles(dir, + { + exclude: /^\./, + match: /.js$/, + }, + (err, content, next) => next(), + (err, files) => { + if (err) throw err; + + for (const file of files) { + const path = file + .substring(0, file.length - 3) // remove `.js` + .substring(dir.length) // remove higher directories + .replace(/\[(\w+)\]/gi, ':$1') // convert [] to : + .replace('/index', '') || '/'; // remove index + const route = require(file); + + Object.keys(route).forEach(method => fastify[method](path, { + config: { client }, + ...route[method], + })); // register route + } + + fastify.listen(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/index.js b/src/index.js index 4571a7c..a5bd741 100644 --- a/src/index.js +++ b/src/index.js @@ -30,6 +30,7 @@ const logger = require('./lib/logger'); const banner = require('./lib/banner'); const YAML = require('yaml'); const Client = require('./client'); +const http = require('./http'); process.env.NODE_ENV ??= 'development'; // make sure NODE_ENV is set require('dotenv').config(); // load env file @@ -74,4 +75,6 @@ process.on('unhandledRejection', error => { const client = new Client(); client.config = config; client.log = log; -client.login(); \ No newline at end of file +client.login().then(() => { + http(client); +}); \ No newline at end of file diff --git a/src/routes/api/admin/guilds/[guild]/categories/[category].js b/src/routes/api/admin/guilds/[guild]/categories/[category].js new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/api/admin/guilds/[guild]/categories/index.js b/src/routes/api/admin/guilds/[guild]/categories/index.js new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/api/admin/guilds/[guild]/index.js b/src/routes/api/admin/guilds/[guild]/index.js new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/api/admin/guilds/index.js b/src/routes/api/admin/guilds/index.js new file mode 100644 index 0000000..e69de29 diff --git a/src/routes/index.js b/src/routes/index.js new file mode 100644 index 0000000..64cdf38 --- /dev/null +++ b/src/routes/index.js @@ -0,0 +1,6 @@ +module.exports.get = { + handler: (req, res) => { + const { client } = res.context.config; + return `Hello, I am ${client.user.username}!`; + }, +}; \ No newline at end of file