-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
289 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
|
||
export default async function Administration( | ||
fastify: FastifyInstance | ||
) { | ||
const { log, storage: { root }, scheduler } = fastify; | ||
|
||
fastify.delete( | ||
'/admin/storage/root', | ||
{ | ||
onRequest: [fastify.auth] | ||
}, | ||
async (request, reply) => { | ||
log.warn( | ||
'Clearing root database %s %j', | ||
request.ip, | ||
request.headers | ||
); | ||
await root.clear(); | ||
reply.send(); | ||
} | ||
); | ||
|
||
fastify.get('/admin/scheduled',{ | ||
onRequest: [fastify.auth], | ||
schema: { | ||
hide: true, | ||
response: { | ||
200: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
}, async (_, reply) => { | ||
reply.send(await scheduler.allTaskTimes()); | ||
}); | ||
|
||
fastify.get<{ | ||
Params: { | ||
id: string | ||
} | ||
}>('/admin/scheduled/:id',{ | ||
onRequest: [fastify.auth], | ||
schema: { | ||
hide: true | ||
} | ||
}, async (request, reply) => { | ||
reply.send( | ||
await scheduler.getById(request.params.id) | ||
); | ||
}); | ||
|
||
fastify.delete<{ | ||
Params: { | ||
id: string | ||
} | ||
}>('/admin/scheduled/:id',{ | ||
onRequest: [fastify.auth], | ||
schema: { | ||
hide: true, | ||
response: { | ||
200: { | ||
type: 'null' | ||
} | ||
} | ||
} | ||
}, async (request, reply) => { | ||
await scheduler.remove(request.params.id); | ||
reply.send(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify'; | ||
import fp from 'fastify-plugin'; | ||
import jwt from '@fastify/jwt'; | ||
|
||
import { environment } from '../environment.js'; | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
auth: ( | ||
request: FastifyRequest, reply: FastifyReply | ||
) => Promise<void> | ||
} | ||
} | ||
|
||
const authPlugin: FastifyPluginAsync | ||
= async fastify => { | ||
if (environment !== 'development' && !process.env.XCMON_SECRET) { | ||
fastify.log.warn('!! Default XCMON_SECRET configured !!'); | ||
} | ||
|
||
fastify.register(jwt, { | ||
secret: process.env.XCMON_SECRET || 'IAOAbraxasSabaoth' | ||
}); | ||
fastify.decorate('auth', | ||
async function ( | ||
request: FastifyRequest, reply: FastifyReply | ||
) : Promise<void> { | ||
try { | ||
await request.jwtVerify(); | ||
} catch (err) { | ||
reply.status(401).send(err); | ||
} | ||
} | ||
); | ||
}; | ||
|
||
export default fp(authPlugin); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.