Skip to content

Commit

Permalink
add get endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Jul 31, 2022
1 parent 1be064d commit 6970687
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
13 changes: 13 additions & 0 deletions backend/src/api/get-endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response } from "express";
import { GetEndpointsService } from "../../services/get-endpoints";
import { GetEndpointParams } from "../../types";

export const getEndpointsHandler = async (req: Request, res: Response) => {
const getEndpointParams: GetEndpointParams = req.query;
try {
const endpoints = await GetEndpointsService.getEndpoints(getEndpointParams);
res.send(200).send(endpoints)
} catch {
res.sendStatus(500)
}
}
18 changes: 13 additions & 5 deletions backend/src/api/log-request/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { Request, Response } from "express";
import { LogRequestService } from "../../services/log-request";
import { TraceParams } from "types";
import { TraceParams } from "../../types";

export const logRequestSingleHandler = async (req: Request, res: Response) => {
const traceParams: TraceParams = req.body;
await LogRequestService.logRequest(traceParams);
res.sendStatus(200);
try {
await LogRequestService.logRequest(traceParams);
res.sendStatus(200)
} catch {
res.sendStatus(500)
}
}

export const logRequestBatchHandler = async (req: Request, res: Response) => {
const traceParamsBatch: TraceParams[] = req.body;
await LogRequestService.logRequestBatch(traceParamsBatch);
res.sendStatus(200);
try {
await LogRequestService.logRequestBatch(traceParamsBatch);
res.sendStatus(200);
} catch {
res.sendStatus(500);
}
}
46 changes: 46 additions & 0 deletions backend/src/services/get-endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FindManyOptions, FindOptionsWhere } from "typeorm";
import { GetEndpointParams } from "../../types";
import { AppDataSource } from "../../data-source"
import { ApiEndpoint } from "../../../models";

export class GetEndpointsService {
static async getEndpoints(getEndpointParams: GetEndpointParams): Promise<ApiEndpoint[]> {
try {
const apiEndpointRepository = AppDataSource.getRepository(ApiEndpoint);
let whereConditions: FindOptionsWhere<ApiEndpoint> = {}
let paginationParams: FindManyOptions<ApiEndpoint> = {}
if (getEndpointParams?.environment) {
whereConditions = {
...whereConditions,
environment: getEndpointParams.environment
}
}
if (getEndpointParams?.host) {
whereConditions = {
...whereConditions,
host: getEndpointParams.host
}
}
if (getEndpointParams?.offset) {
paginationParams = {
...paginationParams,
skip: getEndpointParams.offset
}
}
if (getEndpointParams?.limit) {
paginationParams = {
...paginationParams,
take: getEndpointParams.limit
}
}

const endpoints = await apiEndpointRepository.find({ where: whereConditions, ...paginationParams })

// TODO: Calculate risk score for endpoints and if risk score param present, only return those that meet

return endpoints
} catch (err) {
console.error(`Error in Get Endpoints service: ${err}`)
}
}
}
12 changes: 1 addition & 11 deletions backend/src/services/log-request/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { Repository } from "typeorm";
import { TraceParams } from "../../types";
import { ApiTrace } from "../../../models";
import { AppDataSource } from "../../data-source";
import { ScannerService } from "../scanner/scan";

let API_TRACE_REPOSITORY = AppDataSource.getRepository(ApiTrace);

export class LogRequestService {
static getApiTraceRepository(): Repository<ApiTrace> {
if (!API_TRACE_REPOSITORY) {
API_TRACE_REPOSITORY = AppDataSource.getRepository(ApiTrace);
}
return API_TRACE_REPOSITORY;
}

static async logRequest(traceParams: TraceParams) {
try {
/** Log Request in ApiTrace table */
const apiTraceRepository = this.getApiTraceRepository();
const apiTraceRepository = AppDataSource.getRepository(ApiTrace);
const path = traceParams?.request?.url?.path;
const method = traceParams?.request?.method;
const environment = traceParams?.meta?.environment;
Expand Down
8 changes: 8 additions & 0 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ export interface TraceParams {
response: Response
meta: Meta
}

export interface GetEndpointParams {
environment?: string
host?: string
riskScore?: string
offset?: number
limit?: number
}

0 comments on commit 6970687

Please sign in to comment.