diff --git a/package.json b/package.json index c00b7ec..fe796d9 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "dependencies": { "ajv": "^8.13.0", "fastify": "^4.27.0", - "typescript-eslint": "^7.8.0" + "typescript-eslint": "^7.8.0", + "yaml": "^2.4.2" }, "devDependencies": { "@types/eslint": "^8.56.10", diff --git a/src/routes/index.ts b/src/routes/index.ts index 3a2cda3..d843250 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,11 +1,13 @@ import { FastifyPluginAsync } from 'fastify'; import deleter from './delete.js'; +import openapi from './openapi.js'; import upload from './upload.js'; import index from './indexpage.js'; import list from './list.js'; import get from './get.js'; export default (async function (fastify) { + await fastify.register(openapi); await fastify.register(deleter); await fastify.register(upload); await fastify.register(index); diff --git a/src/routes/indexpage.ts b/src/routes/indexpage.ts index 13720bf..0fa5f2b 100644 --- a/src/routes/indexpage.ts +++ b/src/routes/indexpage.ts @@ -9,17 +9,18 @@ const page = `

You have reached the backup server!

This is the backup server! If you are a regular user, you wouldn't find this place very interesting and might as well close this page now.

-

If you are a sysadmin, please refer to API docs for more info

+

If you are a sysadmin, please refer to API docs or OpenAPI reference for more info

` .replaceAll('\n', '') .replaceAll(/ +/gm, ' ') - .replaceAll(' <', '<'); + .replaceAll(' <', '<') + .replaceAll(' { - rep.type('text/html; charset=utf8'); + rep.type('text/html; charset=utf-8'); return page; }); fastify.get('/favicon.ico', async (req, rep) => { diff --git a/src/routes/openapi.ts b/src/routes/openapi.ts new file mode 100644 index 0000000..a2e893d --- /dev/null +++ b/src/routes/openapi.ts @@ -0,0 +1,34 @@ +import { type FastifyPluginAsync } from 'fastify'; +import fs from 'fs'; +import yaml from 'yaml'; + +const page = ` + + + Backup server API reference + + + +
+ + + + +` + .replaceAll('\n', '') + .replaceAll(/ +/gm, ' ') + .replaceAll(' <', '<'); + +const openapiYml = yaml.parse(fs.readFileSync('openapi.yml').toString('utf-8')); + +export default (async function (fastify) { + fastify.get('/openapi', async (req, rep) => { + rep.type('text/html; charset=utf-8'); + return page; + }); + + fastify.get('/openapi.json', async (req, rep) => { + rep.type('application/json; charset=utf-8'); + return openapiYml; + }); +} as FastifyPluginAsync); diff --git a/yarn.lock b/yarn.lock index 2347b11..4d953ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1361,6 +1361,11 @@ word-wrap@^1.2.5: resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.5.tgz#d2c45c6dd4fbce621a66f136cbe328afd0410b34" integrity sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA== +yaml@^2.4.2: + version "2.4.2" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-2.4.2.tgz#7a2b30f2243a5fc299e1f14ca58d475ed4bc5362" + integrity sha512-B3VqDZ+JAg1nZpaEmWtTXUlBneoGx6CPM9b0TENK6aoSu5t73dItudwdgmi6tHlIZZId4dZ9skcAQ2UbcyAeVA== + yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"