Skip to content

Commit

Permalink
feat: middleware options
Browse files Browse the repository at this point in the history
  • Loading branch information
fanSte8 committed Feb 8, 2022
1 parent 7d0bf98 commit 6020470
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
9 changes: 5 additions & 4 deletions api/src/controllers/get-jobs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextFunction, Request, Response } from 'express';
import { getAgendaInstance } from 'src/agenda-instance';
import { getOptions } from 'src/options';
import { StatusType } from 'src/types';
import { buildGetJobsQuery } from 'src/utils/build-get-jobs-query';
import { ITEMS_PER_PAGE } from 'src/constants';

interface ReqQuery {
sortBy: 'lastRunAt' | 'nextRunAt';
Expand All @@ -21,6 +21,7 @@ export const getJobs = async (
) => {
const page = req.query.page || 1;
const query = buildGetJobsQuery(req.query);
const { itemsPerPage } = getOptions();

const data = await getAgendaInstance()
._collection.aggregate([
Expand All @@ -32,15 +33,15 @@ export const getJobs = async (
{
$project: {
pagesCount: {
$ceil: { $divide: ['$itemsCount', ITEMS_PER_PAGE] },
$ceil: { $divide: ['$itemsCount', itemsPerPage] },
},
itemsCount: '$itemsCount',
},
},
],
jobs: [
{ $skip: ITEMS_PER_PAGE * (page - 1) },
{ $limit: ITEMS_PER_PAGE },
{ $skip: itemsPerPage * (page - 1) },
{ $limit: itemsPerPage },
],
},
},
Expand Down
4 changes: 4 additions & 0 deletions api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import { URL } from 'url';
import { Express } from 'express';
import { setAgendaInstance } from 'src/agenda-instance';
import app from 'src/app';
import { OptionsType, setOptions } from 'src/options';

export const mountAgendaAdmin = async ({
publicUrl,
expressApp,
agenda,
options,
}: {
publicUrl: string;
expressApp: Express;
agenda: Agenda;
options?: OptionsType;
}) => {
const parsedUrl = new URL(publicUrl);

Expand Down Expand Up @@ -55,5 +58,6 @@ export const mountAgendaAdmin = async ({
);

setAgendaInstance(agenda);
setOptions(options);
expressApp.use(parsedUrl.pathname, app);
};
8 changes: 4 additions & 4 deletions api/src/middleware/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { NextFunction, Request, Response } from 'express';
import basicAuth from 'express-basic-auth';
import { getOptions } from 'src/options';

export const authentication = (
req: Request,
res: Response,
next: NextFunction
) => {
if (process.env.NODE_ENV === 'testing' || !process.env.AGENDA_PASSWORD) {
const { username, password } = getOptions();

if (process.env.NODE_ENV === 'testing' || !password) {
return next();
}

const username = process.env.AGENDA_USERNAME || 'admin';
const password = process.env.AGENDA_PASSWORD || '';

basicAuth({
users: {
[username]: password,
Expand Down
21 changes: 21 additions & 0 deletions api/src/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ITEMS_PER_PAGE } from './constants';

export type OptionsType =
| {
itemsPerPage?: number;
username?: string;
password?: string;
}
| undefined;

let options: OptionsType;

export const setOptions = (newOptions: OptionsType) => {
options = newOptions;
};

export const getOptions = () => ({
itemsPerPage: options?.itemsPerPage || ITEMS_PER_PAGE,
username: options?.username || 'admin',
password: options?.password || '',
});
5 changes: 1 addition & 4 deletions client/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
REACT_APP_REFRESH_INTERVAL=
REACT_APP_API_URL=
REACT_APP_ITEMS_PER_PAGE=
REACT_APP_LOGO_URL=
PUBLIC_URL=

0 comments on commit 6020470

Please sign in to comment.