-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Update dev server to work with logger.
- Loading branch information
Showing
29 changed files
with
262 additions
and
99 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,67 @@ | ||
/* | ||
Copyright 2020-2023 Lowdefy, Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
import path from 'path'; | ||
import crypto from 'crypto'; | ||
import { createApiContext } from '@lowdefy/api'; | ||
import { getSecretsFromEnv } from '@lowdefy/node-utils'; | ||
|
||
import config from '../../build/config.json'; | ||
import connections from '../../build/plugins/connections.js'; | ||
import createLogger from './log/createLogger.js'; | ||
import fileCache from './fileCache.js'; | ||
import getServerSession from './auth/getServerSession.js'; | ||
import logRequest from './log/logRequest.js'; | ||
import operators from '../../build/plugins/operators/server.js'; | ||
import getAuthOptions from './auth/getAuthOptions.js'; | ||
|
||
function apiWrapper(handler) { | ||
return async function wrappedHandler(req, res) { | ||
let logger = console; | ||
try { | ||
const rid = crypto.randomUUID(); | ||
logger = createLogger({ rid }); | ||
const authOptions = getAuthOptions({ logger }); | ||
const session = await getServerSession({ authOptions, req, res }); | ||
// Important to give absolute path so Next can trace build files | ||
const context = createApiContext({ | ||
authOptions, | ||
buildDirectory: path.join(process.cwd(), 'build'), | ||
config, | ||
connections, | ||
fileCache, | ||
headers: req.headers, | ||
logger, | ||
operators, | ||
secrets: getSecretsFromEnv(), | ||
session, | ||
req, | ||
}); | ||
logRequest({ context }); | ||
// Await here so that if handler throws it is caught. | ||
const response = await handler({ context, req, res }); | ||
// TODO: Log response time? | ||
return response; | ||
} catch (error) { | ||
// TODO: Improve (logError function) | ||
// TODO: Log cause | ||
logger.error(error); | ||
// TODO: What we do here? | ||
res.status(500).json({ name: error.name, message: error.message }); | ||
} | ||
}; | ||
} | ||
|
||
export default apiWrapper; |
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,35 @@ | ||
/* | ||
Copyright 2020-2023 Lowdefy, Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { getNextAuthConfig } from '@lowdefy/api'; | ||
import { getSecretsFromEnv } from '@lowdefy/node-utils'; | ||
|
||
import adapters from '../../../build/plugins/auth/adapters.js'; | ||
import authJson from '../../../build/auth.json'; | ||
import callbacks from '../../../build/plugins/auth/callbacks.js'; | ||
import events from '../../../build/plugins/auth/events.js'; | ||
import providers from '../../../build/plugins/auth/providers.js'; | ||
|
||
function getAuthOptions({ logger }) { | ||
return getNextAuthConfig({ | ||
authJson, | ||
logger, | ||
plugins: { adapters, callbacks, events, providers }, | ||
secrets: getSecretsFromEnv(), | ||
}); | ||
} | ||
|
||
export default getAuthOptions; |
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
File renamed without changes.
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,29 @@ | ||
/* | ||
Copyright 2020-2023 Lowdefy, Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import pino from 'pino'; | ||
|
||
const logger = pino({ | ||
name: 'lowdefy_server', | ||
level: process.env.LOWDEFY_LOG_LEVEL ?? 'info', | ||
base: { pid: undefined, hostname: undefined }, | ||
}); | ||
|
||
function createLogger(metadata = {}) { | ||
return logger.child(metadata); | ||
} | ||
|
||
export default createLogger; |
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,76 @@ | ||
/* | ||
Copyright 2020-2023 Lowdefy, Inc | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// TODO: Better name needed here maybe? | ||
function logRequest({ context, metadata = {} }) { | ||
const { user = {} } = context; | ||
context.logger.debug({ | ||
// TODO: | ||
// app_name | ||
// app_version | ||
// lowdefy_version | ||
// build_hash | ||
// config_hash | ||
user: { | ||
id: user.id, | ||
roles: user.roles, | ||
sub: user.sub, | ||
session_id: user.session_id, // TODO: Implement session id | ||
}, | ||
url: context.req.url, | ||
method: context.req.method, | ||
resolvedUrl: context.nextContext?.resolvedUrl, | ||
hostname: context.req.hostname, | ||
...metadata, | ||
}); | ||
// TODO: | ||
// Next local? nextContext.locale, nextContext.locales, nextContext.defaultLocale | ||
// console.log('params', nextContext.params); | ||
} | ||
|
||
export default logRequest; | ||
|
||
/* | ||
User ID | ||
Session ID | ||
Event Type/Action | ||
Timestamp | ||
Source IP Address | ||
User Agent | ||
Resource ID/Identifier | ||
Outcome/Status | ||
Reason | ||
Details/Additional Information | ||
Targeted System | ||
Page URL | ||
Referrer URL | ||
IP Address | ||
Response Time | ||
Error Messages | ||
Device Information | ||
Input Data | ||
Behavioral Metrics | ||
Error Level/Severity | ||
Error Message | ||
Error Code | ||
HTTP Method | ||
Stack Trace | ||
Payload/Body | ||
Application Version | ||
Environment Information | ||
*/ |
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
Oops, something went wrong.