Split services into core and request
This commit is contained in:
25
express/request/index.ts
Normal file
25
express/request/index.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { AuthService } from "../auth";
|
||||
import { getCurrentUser } from "../context";
|
||||
import { PostgresAuthStore } from "../database";
|
||||
import type { MaybeUser } from "../user";
|
||||
import { html, redirect, render } from "./util";
|
||||
|
||||
const util = { html, redirect, render };
|
||||
|
||||
const session = {
|
||||
getUser: (): MaybeUser => {
|
||||
return getCurrentUser();
|
||||
},
|
||||
};
|
||||
|
||||
// Initialize auth with PostgreSQL store
|
||||
const authStore = new PostgresAuthStore();
|
||||
const auth = new AuthService(authStore);
|
||||
|
||||
const request = {
|
||||
auth,
|
||||
session,
|
||||
util,
|
||||
};
|
||||
|
||||
export { request };
|
||||
@@ -1,24 +1,23 @@
|
||||
import { contentTypes } from "../content-types";
|
||||
import { core } from "../core";
|
||||
import { executionContext } from "../execution-context";
|
||||
import { httpCodes } from "../http-codes";
|
||||
import type { RedirectResult, Result } from "../types";
|
||||
import {services} from '../services'
|
||||
import { loadFile } from "../util";
|
||||
import { request } from "./index";
|
||||
|
||||
import{loadFile}from'../util'
|
||||
|
||||
|
||||
type NoUser={
|
||||
type NoUser = {
|
||||
[key: string]: unknown;
|
||||
} & {
|
||||
user?: never;
|
||||
}
|
||||
};
|
||||
|
||||
const render = async (path: string, ctx?: NoUser): Promise<string> => {
|
||||
const fullPath = `${executionContext.diachron_root}/templates/${path}.html.njk`;
|
||||
const template = await loadFile(fullPath);
|
||||
const user = services.session.getUser();
|
||||
const context = {user, ...ctx}
|
||||
const engine = services.conf.templateEngine()
|
||||
const user = request.session.getUser();
|
||||
const context = { user, ...ctx };
|
||||
const engine = core.conf.templateEngine();
|
||||
const retval = engine.renderTemplate(template, context);
|
||||
|
||||
return retval;
|
||||
@@ -43,4 +42,4 @@ const redirect = (location: string): RedirectResult => {
|
||||
};
|
||||
};
|
||||
|
||||
export { render, html, redirect };
|
||||
export { html, redirect, render };
|
||||
|
||||
Reference in New Issue
Block a user