This repository has been archived by the owner on Oct 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
64 changed files
with
1,526 additions
and
360 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require(`dotenv`).config(); | ||
|
||
module.exports = { | ||
serverUrl: process.env.ELASTIC_APM_SERVER_URL, // E.g. https://my-deployment-name.apm.us-west2.gcp.elastic-cloud.com | ||
secretToken: process.env.ELASTIC_APM_SECRET_TOKEN, | ||
serviceName: process.env.ELASTIC_APM_SERVICE_NAME, | ||
active: process.env.ELASTIC_APM_ACTIVE === "true", | ||
logLevel: process.env.ELASTIC_APM_LOG_LEVEL | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const UNAUTHORIZED = `Unauthorized`; | ||
export const INVALID_JWT_TOKEN = `Invalid JWT Token`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export function isValidUrl(string) { | ||
try { | ||
new URL(string); | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import {LogAccess} from "./log-access"; | ||
import withCors from "./withCors"; | ||
import WithJwt from "./withJwt"; | ||
|
||
const withProtected = (handler) => withCors(WithJwt(handler)) | ||
export {withCors, WithJwt, withProtected} | ||
const RouteMiddleware = (handler) => LogAccess(withCors(WithJwt(handler))); | ||
|
||
export {withCors, WithJwt, withProtected, RouteMiddleware}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {NextApiHandler, NextApiRequest, NextApiResponse} from "next"; | ||
|
||
import {debug, log, Logger} from "../services/logging"; | ||
|
||
export const LogAccess = (handler: NextApiHandler) => { | ||
return async (req: NextApiRequest, res: NextApiResponse) => { | ||
const {url, method} = req as any; | ||
const _query = Object.fromEntries(new URLSearchParams(url.split('?')[1])); | ||
const query = Object.keys(_query).length ? _query : null; | ||
const body = req?.body || null; | ||
|
||
const pathname = url.split('/api')[1].replace(/\?.+/g, ''); | ||
|
||
const rest = (query || body) ? ({ ... query ? {query} : {}, ... body ? {body} : {}}) : ''; | ||
|
||
log(`${method} access`, pathname); | ||
if (rest) | ||
debug(`${method} access-payload`, pathname, rest); | ||
|
||
try { | ||
await handler(req, res); | ||
|
||
if (res.statusCode >= 400) | ||
Logger.warn(`Answered with ${res.statusCode}`, res.statusMessage) | ||
|
||
debug(`${method} access-end`, pathname) | ||
} catch (e) { | ||
Logger.error(e, `${method}`, pathname, e?.toString(), rest); | ||
} | ||
Logger.changeActionName(``); // clean action just in case; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,27 @@ | ||
import Cors from 'cors' | ||
import getConfig from "next/config"; | ||
|
||
import { info, error } from 'services/logging'; | ||
|
||
const { publicRuntimeConfig } = getConfig(); | ||
|
||
const cors = Cors({ | ||
methods: ['GET', 'PUT', 'POST'], | ||
origin: [publicRuntimeConfig?.urls?.home || 'http://localhost:3000'], | ||
}) | ||
|
||
const ignorePaths = ['health', 'ip']; | ||
|
||
function runMiddleware(req, res, fn) { | ||
return new Promise((resolve, reject) => { | ||
fn(req, res, (result) => { | ||
if (result instanceof Error) { | ||
return reject(result) | ||
const WithCors = (handler) => | ||
(req, res) => | ||
new Promise((resolve, reject) => { | ||
|
||
const next = (e) => { | ||
if (e instanceof Error) | ||
reject(e); | ||
resolve(null) | ||
} | ||
|
||
return resolve(result) | ||
}) | ||
}) | ||
} | ||
|
||
function runLogger(req, e = null) { | ||
const {url, method} = req as any; | ||
const search = Object(new URLSearchParams(url.split('?')[1])); | ||
const pathname = url.split('/api')[1].replace(/\?.+/g, ''); | ||
|
||
if (!ignorePaths.some(k => pathname.includes(k))) | ||
info('Access', {method, pathname, search,}); | ||
|
||
if (e) | ||
error(e?.message); | ||
} | ||
|
||
const withCors = (handler) => { | ||
return async (req, res) => { | ||
runLogger(req); | ||
runMiddleware(req, res, cors) | ||
.then(()=>{ | ||
return handler(req, res); | ||
}).catch((e)=>{ | ||
runLogger(req, e?.message || e.toString()); | ||
return res.status(401).write('Unautorized'); | ||
}) | ||
}; | ||
}; | ||
|
||
export default withCors; | ||
cors(req, res, next); | ||
|
||
}).then(() => handler(req, res)) | ||
|
||
|
||
export default WithCors; |
Oops, something went wrong.