Skip to content

Commit

Permalink
Start moving code of a specific “page” to a separate place in the bac…
Browse files Browse the repository at this point in the history
…kend folder
  • Loading branch information
smarr committed Jun 13, 2023
1 parent fe284f4 commit 1255783
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
9 changes: 9 additions & 0 deletions src/backend/db/db-instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { cacheInvalidationDelay, dbConfig, statsConfig } from '../../util.js';
import { DatabaseWithPool } from '../../db.js';

export const db = new DatabaseWithPool(
dbConfig,
statsConfig.numberOfBootstrapSamples,
true,
cacheInvalidationDelay
);
File renamed without changes.
13 changes: 13 additions & 0 deletions src/backend/main/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ParameterizedContext } from 'koa';
import { db } from '../db/db-instance.js';
import { isReBenchDotDev } from '../../util.js';
import { processTemplate } from '../../templates.js';

export async function renderMainPage(ctx: ParameterizedContext): Promise<void> {
const projects = await db.getAllProjects();
ctx.body = processTemplate('../backend/main/index.html', {
projects,
isReBenchDotDev: isReBenchDotDev()
});
ctx.type = 'html';
}
27 changes: 4 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Koa from 'koa';
import { koaBody } from 'koa-body';
import Router from 'koa-router';

import { DatabaseWithPool } from './db.js';
import { BenchmarkData, BenchmarkCompletion, TimelineRequest } from './api.js';
import { createValidator } from './api-validator.js';
import * as dataFormatters from './data-format.js';
Expand All @@ -31,17 +30,12 @@ import {
dashMeasurements
} from './dashboard.js';
import { prepareTemplate, processTemplate } from './templates.js';
import {
cacheInvalidationDelay,
dbConfig,
isReBenchDotDev,
robustPath,
siteConfig,
statsConfig
} from './util.js';
import { dbConfig, robustPath, siteConfig } from './util.js';
import { createGitHubClient } from './github.js';
import { getDirname } from './util.js';
import { log } from './logging.js';
import { renderMainPage } from './backend/main/main.js';
import { db } from './backend/db/db-instance.js';

const __dirname = getDirname(import.meta.url);

Expand All @@ -61,21 +55,8 @@ const refreshSecret =

const app = new Koa();
const router = new Router();
const db = new DatabaseWithPool(
dbConfig,
statsConfig.numberOfBootstrapSamples,
true,
cacheInvalidationDelay
);

router.get('/', async (ctx) => {
const projects = await db.getAllProjects();
ctx.body = processTemplate('index.html', {
projects,
isReBenchDotDev: isReBenchDotDev()
});
ctx.type = 'html';
});
router.get('/', renderMainPage);

const projectHtml = prepareTemplate('project.html');

Expand Down
1 change: 1 addition & 0 deletions src/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { compile, TemplateFunction, Options } from 'ejs';

const headerHtml = readFileSync(robustPath('views/header.html')).toString();

// TODO: remove this function
export function processTemplate(filename: string, variables: any = {}): string {
const fileContent = readFileSync(robustPath(`views/${filename}`)).toString();

Expand Down

0 comments on commit 1255783

Please sign in to comment.