♻️ oo-connector: refactored router system to something more coherent (#525)

This commit is contained in:
Eric Doughty-Papassideris
2024-09-04 03:47:59 +02:00
parent afe6562804
commit 9bc9dcafee
10 changed files with 97 additions and 83 deletions
@@ -1,21 +1,16 @@
import TwakeDriveBackendCallbackController from '@/controllers/backend-callbacks.controller';
import { Routes } from '@/interfaces/routes.interface';
import authMiddleware from '@/middlewares/auth.middleware';
import { Router } from 'express';
import type { Router } from 'express';
/**
* These routes are called by Twake Drive backend, for ex. before editing or retreiving a file,
* if it has an editing_session_key still, get the status of that and force a resolution.
*/
export default class TwakeDriveBackendCallbackRoutes implements Routes {
public readonly router = Router();
public readonly path = '/tdriveApi/1';
private readonly controller: TwakeDriveBackendCallbackController;
constructor() {
this.controller = new TwakeDriveBackendCallbackController();
export const TwakeDriveBackendCallbackRoutes = {
mount(router: Router) {
const controller = new TwakeDriveBackendCallbackController();
// Why post ? to garantee it is never cached and always ran
this.router.post('/session/:editing_session_key/check', authMiddleware, this.controller.checkSessionStatus);
this.router.delete('/session/:editing_session_key', authMiddleware, this.controller.deleteSessionKey);
}
}
router.post('/session/:editing_session_key/check', authMiddleware, controller.checkSessionStatus);
router.delete('/session/:editing_session_key', authMiddleware, controller.deleteSessionKey);
},
};
@@ -0,0 +1,16 @@
import BrowserEditorController from '@/controllers/browser-editor.controller';
import authMiddleware from '@/middlewares/auth.middleware';
import requirementsMiddleware from '@/middlewares/requirements.middleware';
import type { Router } from 'express';
/**
* When the user previews or edits a file in Twake Drive, their browser is sent to these routes
* which return a webpage that instantiates the client side JS Only Office component.
*/
export const BrowserEditorRoutes = {
mount(router: Router) {
const controller = new BrowserEditorController();
router.get('/', requirementsMiddleware, authMiddleware, controller.index);
router.get('/editor', requirementsMiddleware, authMiddleware, controller.editor);
},
};
@@ -1,28 +0,0 @@
import IndexController from '@/controllers/index.controller';
import { Routes } from '@/interfaces/routes.interface';
import authMiddleware from '@/middlewares/auth.middleware';
import requirementsMiddleware from '@/middlewares/requirements.middleware';
import { Router } from 'express';
import { SERVER_PREFIX } from '@config';
/**
* When the user previews or edits a file in Twake Drive, their browser is sent to these routes
* which return a webpage that instantiates the client side JS Only Office component.
*/
class IndexRoute implements Routes {
public path = SERVER_PREFIX;
public router = Router();
public indexController: IndexController;
constructor() {
this.indexController = new IndexController();
this.initRoutes();
}
private initRoutes = () => {
this.router.get('/', requirementsMiddleware, authMiddleware, this.indexController.index);
this.router.get('/editor', requirementsMiddleware, authMiddleware, this.indexController.editor);
};
}
export default IndexRoute;
@@ -0,0 +1,43 @@
import { Application, Router } from 'express';
import * as Utils from '@/utils';
import { TwakeDriveBackendCallbackRoutes } from './backend-callbacks.route';
import { BrowserEditorRoutes } from './browser-editor.route';
import { OnlyOfficeRoutes } from './onlyoffice.route';
import { SERVER_ORIGIN, SERVER_PREFIX, SERVER_TDRIVE_API_PREFIX } from '@config';
export function mountRoutes(app: Application) {
// These routes are forwarded through the Twake Drive front, back and here
const proxiedRouter = Router();
BrowserEditorRoutes.mount(proxiedRouter);
OnlyOfficeRoutes.mount(proxiedRouter);
console.log('Mounting at ' + SERVER_PREFIX);
app.use(SERVER_PREFIX, proxiedRouter);
// These endpoints should only be accessible to the Twake Drive backend
const apiRouter = Router();
console.log('Mounting at ' + SERVER_TDRIVE_API_PREFIX);
TwakeDriveBackendCallbackRoutes.mount(apiRouter);
app.use(SERVER_TDRIVE_API_PREFIX, apiRouter);
}
export const makeURLTo = {
rootAbsolute: () => Utils.joinURL([SERVER_ORIGIN, SERVER_PREFIX]),
assets: () => Utils.joinURL([SERVER_PREFIX, 'assets']),
editorAbsolute(params: { token: string; file_id: string; editing_session_key: string; company_id: string; preview: string; office_token: string }) {
return Utils.joinURL([SERVER_ORIGIN ?? '', SERVER_PREFIX, 'editor'], params);
},
};
// export function makeURLToEditor2() {
// const initResponse = await editorService.init(company_id, file_name, file_id, user, preview, drive_file_id || file_id);
// res.render('index', {
// ...initResponse,
// docId: preview ? file_id : editing_session_key,
// server: Utils.joinURL([SERVER_ORIGIN, SERVER_PREFIX]),
// token: inPageToken,
// });
// }
@@ -1,23 +1,15 @@
import OnlyOfficeController from '@/controllers/onlyoffice.controller';
import { Routes } from '@/interfaces/routes.interface';
import requirementsMiddleware from '@/middlewares/requirements.middleware';
import { Router } from 'express';
import { SERVER_PREFIX } from '@config';
import type { Router } from 'express';
class OnlyOfficeRoute implements Routes {
public path = SERVER_PREFIX;
public router = Router();
public onlyOfficeController: OnlyOfficeController;
constructor() {
this.onlyOfficeController = new OnlyOfficeController();
this.initRoutes();
}
private initRoutes = () => {
this.router.get(`:mode/read`, requirementsMiddleware, this.onlyOfficeController.read);
this.router.post(`:mode/callback`, requirementsMiddleware, this.onlyOfficeController.ooCallback);
};
}
export default OnlyOfficeRoute;
/**
* These routes are called by the Only Office server
* when reading a document or updating an editing session
*/
export const OnlyOfficeRoutes = {
mount(router: Router) {
const controller = new OnlyOfficeController();
router.get(`/:mode/read`, requirementsMiddleware, controller.read);
router.post(`/:mode/callback`, requirementsMiddleware, controller.ooCallback);
},
};