refactor(api): move certificate enpoint to app directory
This commit is contained in:
parent
fb9cde68c1
commit
f431aaf3b1
2 changed files with 30 additions and 39 deletions
30
src/app/api/certificate/route.ts
Normal file
30
src/app/api/certificate/route.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { getUserFromCookie } from '@/server/common/session.helpers';
|
||||
import { getConfig } from '@/server/core/TipiConfig/TipiConfig';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const user = await getUserFromCookie();
|
||||
|
||||
if (user?.operator) {
|
||||
const filePath = `${getConfig().rootFolder}/traefik/tls/cert.pem`;
|
||||
|
||||
if (await fs.pathExists(filePath)) {
|
||||
const file = await fs.promises.readFile(filePath);
|
||||
|
||||
return new Response(file, {
|
||||
headers: {
|
||||
'content-type': 'application/x-pem-file',
|
||||
'content-disposition': 'attachment; filename=cert.pem',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return new Response('File not found', { status: 404 });
|
||||
}
|
||||
|
||||
return new Response('Forbidden', { status: 403 });
|
||||
} catch (error) {
|
||||
return new Response('Error', { status: 500 });
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
import { getConfig } from '@/server/core/TipiConfig/TipiConfig';
|
||||
import { TipiCache } from '@/server/core/TipiCache/TipiCache';
|
||||
import { AuthQueries } from '@/server/queries/auth/auth.queries';
|
||||
import { db } from '@/server/db';
|
||||
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
/**
|
||||
* API endpoint to get the self-signed certificate
|
||||
*
|
||||
* @param {NextApiRequest} req - The request
|
||||
* @param {NextApiResponse} res - The response
|
||||
*/
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const authService = new AuthQueries(db);
|
||||
|
||||
const sessionId = req.headers['x-session-id'];
|
||||
const cache = new TipiCache('certificate');
|
||||
const userId = await cache.get(`session:${sessionId}`);
|
||||
await cache.close();
|
||||
const user = await authService.getUserById(Number(userId));
|
||||
|
||||
if (user?.operator) {
|
||||
const filePath = `${getConfig().rootFolder}/traefik/tls/cert.pem`;
|
||||
|
||||
if (await fs.pathExists(filePath)) {
|
||||
const file = await fs.promises.readFile(filePath);
|
||||
|
||||
res.setHeader('Content-Type', 'application/x-pem-file');
|
||||
res.setHeader('Content-Dispositon', 'attachment; filename=cert.pem');
|
||||
return res.send(file);
|
||||
}
|
||||
|
||||
res.status(404).send('File not found');
|
||||
}
|
||||
|
||||
return res.status(403).send('Forbidden');
|
||||
}
|
Loading…
Add table
Reference in a new issue