Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Jul 1, 2023
1 parent b6e283d commit fb61463
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"watch:es": "yarn build:es --watch"
},
"dependencies": {
"@alwatr/logger": "^1.0.1",
"@alwatr/nano-server": "^1.0.1",
"simple-oauth2": "~5.0.0",
"tslib": "^2.6.0"
Expand Down
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { createLogger } from "@alwatr/logger";

const clientId = process.env.OAUTH_GITHUB_CLIENT_ID
const clientSecret = process.env.OAUTH_GITHUB_CLIENT_SECRET

Expand All @@ -23,3 +25,7 @@ export const config = {
port: process.env.PORT != null ? +process.env.PORT : 8000,
},
};

export const logger = createLogger('decap-cms-backend')

logger.logProperty?.('config', config)
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import './route/home.js'
import './route/auth.js'
import './route/callback.js'
import { logger } from './config.js'

logger.logOther?.('..:: Alwatr Decap CMS Backend ::..')
5 changes: 4 additions & 1 deletion src/route/auth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {AuthorizationCode} from 'simple-oauth2';
import {randomBytes} from 'crypto';
import {nanoServer} from '../lib/nano-server.js';
import {config} from '../config.js';
import {config, logger} from '../config.js';

export const randomString = () => randomBytes(4).toString('hex');

nanoServer.route('GET', '/auth', (connection) => {
const host = connection.incomingMessage.headers.host;
const url = new URL(`https://${host}/${connection.url}`);
const provider = url.searchParams.get('provider');
logger.logMethodArgs?.('get-auth', {host, url, provider})

if (provider !== 'github') {
return {
Expand All @@ -29,6 +30,8 @@ nanoServer.route('GET', '/auth', (connection) => {
state: randomString(),
});

logger.logProperty?.('authorizationUri', authorizationUri)

connection.serverResponse.setHeader('Location', authorizationUri);
return {
ok: true,
Expand Down
3 changes: 2 additions & 1 deletion src/route/callback.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {AuthorizationCode} from 'simple-oauth2';
import {config} from '../config.js';
import {config, logger} from '../config.js';
import {nanoServer} from '../lib/nano-server.js'

nanoServer.route('GET', '/callback', async (connection) => {
const host = connection.incomingMessage.headers.host;
const url = new URL(`https://${host}/${connection.url}`);
const provider = url.searchParams.get('provider');
const code = url.searchParams.get('code');
logger.logMethodArgs?.('get-callback', {host, url, provider})

if (provider !== 'github') {
return {
Expand Down

0 comments on commit fb61463

Please sign in to comment.