feat: serve Swagger UI

This commit is contained in:
b1ek 2024-05-11 22:38:15 +10:00
parent d3452a4ac7
commit 8ade73fc23
Signed by: blek
GPG Key ID: 14546221E3595D0C
5 changed files with 47 additions and 4 deletions

View File

@ -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",

View File

@ -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);

View File

@ -9,17 +9,18 @@ const page = `<!DOCTYPE html>
<body>
<h1>You have reached the backup server!</h1>
<p>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.</p>
<p>If you are a sysadmin, please refer to <a href='https://git.blek.codes/blek/backups.git'>API docs</a> for more info</p>
<p>If you are a sysadmin, please refer to <a href='https://git.blek.codes/blek/backups.git'>API docs</a> or <a href='/openapi'>OpenAPI reference</a> for more info</p>
</body>
</html>
`
.replaceAll('\n', '')
.replaceAll(/ +/gm, ' ')
.replaceAll(' <', '<');
.replaceAll(' <', '<')
.replaceAll('<a', ' <a');
export default (async function (fastify) {
fastify.get('/', async (_req, rep) => {
rep.type('text/html; charset=utf8');
rep.type('text/html; charset=utf-8');
return page;
});
fastify.get('/favicon.ico', async (req, rep) => {

34
src/routes/openapi.ts Normal file
View File

@ -0,0 +1,34 @@
import { type FastifyPluginAsync } from 'fastify';
import fs from 'fs';
import yaml from 'yaml';
const page = `<!DOCTYPE html>
<html>
<head>
<title>Backup server API reference</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@5.11.0/swagger-ui-bundle.js" crossorigin></script>
<script>window.onload=()=>window.ui=SwaggerUIBundle({url:'/openapi.json',dom_id:'#swagger-ui'})</script>
</body>
</html>
`
.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);

View File

@ -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"