Skip to content

Commit

Permalink
[wip] trying to fix bugs in read request body
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Nov 6, 2018
1 parent 76bbf8f commit 455eb59
Show file tree
Hide file tree
Showing 11 changed files with 379 additions and 46 deletions.
238 changes: 223 additions & 15 deletions discovery/package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions discovery/package.json
Expand Up @@ -15,18 +15,22 @@
"@aws/dynamodb-data-mapper-annotations": "^0.7.3",
"@koa/cors": "^2.2.2",
"@types/koa": "^2.0.46",
"@types/koa-bodyparser": "^5.0.1",
"@types/koa-helmet": "^3.1.2",
"@types/koa-router": "^7.0.32",
"@types/koa__cors": "^2.2.3",
"aws-sdk": "^2.348.0",
"dotenv": "^6.1.0",
"guid-typescript": "^1.0.9",
"koa": "^2.6.1",
"koa-bodyparser": "^4.2.1",
"koa-bodyparser-ts": "^0.3.1",
"koa-helmet": "^4.0.0",
"koa-router": "^7.4.0"
"koa-router": "^7.4.0",
"winston": "^3.1.0"
},
"devDependencies": {
"@types/dotenv": "^4.0.3",
"@types/koa-bodyparser": "^5.0.1",
"@types/node": "^10.12.2",
"nodemon": "^1.18.5",
"ts-node": "^7.0.1",
Expand Down
19 changes: 19 additions & 0 deletions discovery/src/config.ts
@@ -0,0 +1,19 @@
import * as dotenv from 'dotenv';

dotenv.config({ path: '.env' });

export interface IConfig {
port: number;
debugLogging: boolean;
dbsslconn: boolean;
jwtSecret: string;
}

const config: IConfig = {
port: +process.env.PORT || 3000,
debugLogging: process.env.NODE_ENV == 'development',
dbsslconn: process.env.NODE_ENV != 'development',
jwtSecret: process.env.JWT_SECRET || 'your-secret-whatever',
};

export { config };
6 changes: 6 additions & 0 deletions discovery/src/controller/entity.ts
@@ -0,0 +1,6 @@
interface importPackageRequest {
linkedTo: string
}
export {
importPackageRequest
}
15 changes: 9 additions & 6 deletions discovery/src/controller/package.ts
@@ -1,12 +1,15 @@
import { BaseContext } from 'koa';
import { Package } from '../dynamo/model'

import { Context } from 'koa';
import { Package } from '../dynamo/entity';
import { importPackageRequest } from './entity'
export default class PackageController {
public static async importPackage (ctx: BaseContext) {
public static async importPackage (ctx: Context) {
const toImportPackage : Package = new Package();
console.log(ctx.body)
console.log(typeof(ctx.request.body));

let ipr:importPackageRequest;
Object.assign(ipr, ctx.request.body);
toImportPackage.isSymbol = true;
toImportPackage.linkedTo = ctx.body.linkedTo;
toImportPackage.linkedTo = ipr.linkedTo;
console.log(toImportPackage)
}
}
6 changes: 3 additions & 3 deletions discovery/src/dynamo/action.ts
@@ -1,8 +1,8 @@
import mapper from './dynamo'
import { Package } from './model'
import { Package } from './entity'
import { Guid } from 'guid-typescript';
/**
* Following will be object manipulations
* Following will be package manipulations
*/
function putPackage (isSymbol: boolean, linkedTo: string) {
const toPutPackage = Object.assign(new Package, {
Expand All @@ -13,4 +13,4 @@ function putPackage (isSymbol: boolean, linkedTo: string) {
mapper.put(toPutPackage).then(objectSaved => {
console.log(objectSaved)
})
}
}
File renamed without changes.
44 changes: 44 additions & 0 deletions discovery/src/logging.ts
@@ -0,0 +1,44 @@
import * as Koa from 'koa';
import { config } from './config';
import * as winston from 'winston';

export function logger(winstonInstance: any) {
return async(ctx: Koa.Context, next: () => Promise<any>) => {

const start = new Date().getMilliseconds();

await next();

const ms = new Date().getMilliseconds() - start;

let logLevel: string;
if (ctx.status >= 500) {
logLevel = 'error';
}
if (ctx.status >= 400) {
logLevel = 'warn';
}
if (ctx.status >= 100) {
logLevel = 'info';
}

const msg: string = `${ctx.method} ${ctx.originalUrl} ${ctx.status} ${ms}ms`;

winstonInstance.configure({
level: config.debugLogging ? 'debug' : 'info',
transports: [
//
// - Write all logs error (and below) to `error.log`.
new winston.transports.File({ filename: 'error.log', level: 'error' }),
//
// - Write to all logs with specified level to console.
new winston.transports.Console({ format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
) })
]
});

winstonInstance.log(logLevel, msg);
};
}
8 changes: 6 additions & 2 deletions discovery/src/server.ts
Expand Up @@ -2,15 +2,19 @@ import * as cors from '@koa/cors';
import * as Koa from 'koa';
import * as bodyParser from 'koa-bodyparser';
import * as helmet from 'koa-helmet';
import * as winston from 'winston';

import { config } from './config';
import { router } from './routes';
import { logger } from './logging';

const app = new Koa();

app.use(cors());
app.use(helmet());
app.use(logger(winston))
app.use(bodyParser());
app.use(router.routes()).use(router.allowedMethods());

app.listen(3000);
console.log('Server is Running on 3000');
app.listen(config.port);
console.log(`Server running on port ${config.port}`);
16 changes: 15 additions & 1 deletion discovery/tsconfig.json
@@ -1,12 +1,26 @@
{
"compileOnSave": false,
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"noImplicitAny": true,
"outDir": "./dist",
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"moduleResolution": "node",
"typeRoots": [
"node_modules/**/*",
"node_modules/@types",
"node_modules/koa-bodyparser"
],
"lib": [
"es2017",
"es6"
],
"types": [
"node"
],
},
"include": [
"./src/**/*"
Expand Down
65 changes: 48 additions & 17 deletions discovery/tslint.json
@@ -1,29 +1,60 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"quotemark": [
"class-name": true,
"comment-format": [
true,
"single"
"check-space"
],
"no-console": [
false
"indent": [
true,
"spaces"
],
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"object-literal-sort-keys": [
false
"no-var-keyword": true,
"quotemark": [
true,
"single",
"avoid-escape"
],
"semicolon": [
true,
"always",
"ignore-bound-class-methods"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-separator",
"check-type"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
},
{
"call-signature": "onespace",
"index-signature": "onespace",
"parameter": "onespace",
"property-declaration": "onespace",
"variable-declaration": "onespace"
}
],
"no-string-literal": false,
"triple-equals": [
false
]
},
"rulesDirectory": []
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-null-keyword": true,
"prefer-const": true,
"jsdoc-format": true
}
}

0 comments on commit 455eb59

Please sign in to comment.