oneuptime/ApiReference/Index.ts

104 lines
3.5 KiB
TypeScript
Raw Normal View History

2022-05-31 13:57:15 +00:00
import Express, {
ExpressApplication,
2022-03-21 22:26:54 +00:00
ExpressRequest,
ExpressResponse,
2022-03-22 11:19:12 +00:00
ExpressStatic,
2022-04-10 21:57:14 +00:00
} from 'CommonServer/Utils/Express';
2023-02-10 11:08:15 +00:00
import logger from 'CommonServer/Utils/Logger';
import App from 'CommonServer/Utils/StartServer';
2022-02-27 12:24:02 +00:00
import path from 'path';
2023-03-18 21:37:34 +00:00
import ResourceUtil, { ModelDocumentation } from './Utils/Resources';
2023-03-18 21:48:21 +00:00
import IntroductionServiceHandler from './Service/Introduction';
import ErrorServiceHandler from './Service/Errors';
import PermissionServiceHandler from './Service/Permissions';
import AuthenticationServiceHandler from './Service/Authentication';
import PageNotFoundServiceHandler from './Service/PageNotFound';
2023-03-18 21:37:34 +00:00
import ModelServiceHandler from './Service/Model';
import PaginationServiceHandler from './Service/Pagination';
2023-04-05 10:08:25 +00:00
import StatusServiceHandler from './Service/Status';
2023-04-05 12:11:00 +00:00
import DataTypeServiceHandler from './Service/DataType';
2023-03-18 21:48:21 +00:00
import Dictionary from 'Common/Types/Dictionary';
2023-03-17 21:09:55 +00:00
2023-03-18 21:48:21 +00:00
const ResourceDictionary: Dictionary<ModelDocumentation> =
2023-07-24 16:06:55 +00:00
ResourceUtil.getResourceDictionaryByPath();
2023-03-17 21:09:55 +00:00
2023-04-05 10:08:25 +00:00
const APP_NAME: string = 'reference';
2023-02-10 11:08:15 +00:00
2022-05-31 13:57:15 +00:00
const app: ExpressApplication = Express.getExpressApp();
2022-04-15 22:14:01 +00:00
// Set the view engine to ejs
2020-01-16 20:30:32 +00:00
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
2019-08-02 12:56:16 +00:00
2022-04-15 22:14:01 +00:00
// Public static files
2022-03-22 11:19:12 +00:00
app.use(ExpressStatic(path.join(__dirname, 'public'), { maxAge: 2592000 }));
2020-01-16 20:30:32 +00:00
2020-03-22 14:22:56 +00:00
app.use(
2023-04-05 10:08:25 +00:00
'/reference',
2022-03-22 11:19:12 +00:00
ExpressStatic(path.join(__dirname, 'public'), { maxAge: 2592000 })
2020-03-22 14:22:56 +00:00
);
2020-03-22 09:27:39 +00:00
2022-04-15 22:14:01 +00:00
// Index page
2023-04-05 10:08:25 +00:00
app.get(['/reference'], (_req: ExpressRequest, res: ExpressResponse) => {
return res.redirect('/reference/introduction');
2023-02-20 18:42:44 +00:00
});
2023-03-18 21:48:21 +00:00
app.get(
2023-04-05 10:08:25 +00:00
['/reference/page-not-found'],
2023-03-18 21:48:21 +00:00
(req: ExpressRequest, res: ExpressResponse) => {
return PageNotFoundServiceHandler.executeResponse(req, res);
}
);
2023-03-17 18:07:41 +00:00
2023-03-18 21:48:21 +00:00
// All Pages
2023-04-05 10:08:25 +00:00
app.get(['/reference/:page'], (req: ExpressRequest, res: ExpressResponse) => {
2023-03-18 21:48:21 +00:00
const page: string | undefined = req.params['page'];
2023-03-17 21:09:55 +00:00
2023-03-18 21:37:34 +00:00
if (!page) {
return PageNotFoundServiceHandler.executeResponse(req, res);
2023-03-17 18:07:41 +00:00
}
2023-03-18 21:48:21 +00:00
const currentResource: ModelDocumentation | undefined =
ResourceDictionary[page];
2023-03-18 21:37:34 +00:00
2023-03-18 21:48:21 +00:00
if (req.params['page'] === 'permissions') {
2023-03-18 21:37:34 +00:00
return PermissionServiceHandler.executeResponse(req, res);
2023-03-18 21:48:21 +00:00
} else if (req.params['page'] === 'authentication') {
2023-03-18 21:37:34 +00:00
return AuthenticationServiceHandler.executeResponse(req, res);
2023-03-18 21:48:21 +00:00
} else if (req.params['page'] === 'pagination') {
2023-03-18 21:37:34 +00:00
return PaginationServiceHandler.executeResponse(req, res);
2023-03-18 21:48:21 +00:00
} else if (req.params['page'] === 'errors') {
2023-03-18 21:37:34 +00:00
return ErrorServiceHandler.executeResponse(req, res);
2023-03-18 21:48:21 +00:00
} else if (req.params['page'] === 'introduction') {
2023-03-18 21:37:34 +00:00
return IntroductionServiceHandler.executeResponse(req, res);
2023-04-05 10:08:25 +00:00
} else if (req.params['page'] === 'status') {
return StatusServiceHandler.executeResponse(req, res);
2023-04-05 12:11:00 +00:00
} else if (req.params['page'] === 'data-types') {
return DataTypeServiceHandler.executeResponse(req, res);
2023-04-05 12:22:52 +00:00
} else if (currentResource) {
2023-03-18 21:37:34 +00:00
return ModelServiceHandler.executeResponse(req, res);
2023-03-17 18:07:41 +00:00
}
2023-03-18 21:48:21 +00:00
// page not found
return PageNotFoundServiceHandler.executeResponse(req, res);
2023-03-18 21:37:34 +00:00
});
app.get('/*', (req: ExpressRequest, res: ExpressResponse) => {
return PageNotFoundServiceHandler.executeResponse(req, res);
2019-08-02 12:56:16 +00:00
});
2023-08-10 17:25:15 +00:00
const init: () => Promise<void> = async (): Promise<void> => {
2023-02-10 11:08:15 +00:00
try {
// init the app
await App(APP_NAME);
} catch (err) {
logger.error('App Init Failed:');
logger.error(err);
}
};
2023-08-10 18:00:27 +00:00
init().catch((err: Error) => {
logger.error(err);
});
2023-02-10 11:08:15 +00:00
2022-02-25 13:22:20 +00:00
export default app;