Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove Base package #117

Merged
merged 1 commit into from
May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 80 additions & 21 deletions src/packages/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,104 @@ import Promise from 'bluebird';
import cluster from 'cluster';
import { singularize } from 'inflection';

import Base from '../base';
import Server from '../server';
import Router from '../router';
import Database from '../database';

import loader from '../loader';

const { defineProperties } = Object;

const { env: { PWD, PORT } } = process;
const { isMaster } = cluster;

class Application extends Base {
class Application {
path;
port;
store;
domain;
logger;
router;

constructor({
path = PWD,
port = PORT || 4000,
domain = 'http://localhost',
logger,
sessionKey,
sessionSecret
} = {}) {
const router = new Router();

const server = new Server({
router,
logger,
sessionKey,
sessionSecret
});

router = Router.create();

constructor({ path = process.env.PWD, ...props }) {
super({
...props,
path
const store = new Database({
path,
logger,
config: require(`${path}/config/database`).default
});

this.setProps({
server: Server.create({
router: this.router,
logger: this.logger,
application: this
})
defineProperties(this, {
path: {
value: path,
writable: false,
enumerable: true,
configurable: false
},

port: {
value: port,
writable: false,
enumerable: true,
configurable: false
},

store: {
value: store,
writable: false,
enumerable: true,
configurable: false
},

domain: {
value: domain,
writable: false,
enumerable: true,
configurable: false
},

router: {
value: router,
writable: false,
enumerable: true,
configurable: false
},

logger: {
value: logger,
writable: false,
enumerable: true,
configurable: false
},

server: {
value: server,
writable: false,
enumerable: false,
configurable: false
}
});

return this;
}

async boot() {
const { router, logger, domain, server, port, path } = this;

const store = new Database({
path,
logger,
config: require(`${path}/config/database`).default
});
const { router, domain, server, port, path, store } = this;

let [
routes,
Expand Down
22 changes: 0 additions & 22 deletions src/packages/base/index.js

This file was deleted.

82 changes: 65 additions & 17 deletions src/packages/route/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Base from '../base';

import getStaticPath from './utils/get-static-path';
import getDynamicSegments from './utils/get-dynamic-segments';

class Route extends Base {
const { defineProperties } = Object;

class Route {
path;
method;
action;
method;
resource;
handlers;
controller;
staticPath;
dynamicSegments;
Expand All @@ -16,24 +17,71 @@ class Route extends Base {
const resource = path.replace(/^(.+)\/.+$/ig, '$1');
const controller = controllers.get(resource);
const dynamicSegments = getDynamicSegments(path);
let handlers;

if (action && controller) {
props = {
...props,
handlers: controller[action]()
};
handlers = controller[action]();
}

return super({
...props,
path,
action,
resource,
controller,
dynamicSegments,
method: method.toUpperCase(),
staticPath: getStaticPath(path, dynamicSegments)
defineProperties(this, {
path: {
value: path,
writable: false,
enumerable: true,
configurable: false
},

action: {
value: action,
writable: false,
enumerable: true,
configurable: false
},

method: {
value: method.toUpperCase(),
writable: false,
enumerable: true,
configurable: false
},

resource: {
value: resource,
writable: false,
enumerable: false,
configurable: false
},

handlers: {
value: handlers,
writable: false,
enumerable: false,
configurable: false
},

controller: {
value: controller,
writable: false,
enumerable: false,
configurable: false
},

dynamicSegments: {
value: dynamicSegments,
writable: false,
enumerable: false,
configurable: false
},

staticPath: {
value: getStaticPath(path, dynamicSegments),
writable: false,
enumerable: false,
configurable: false
}
});

return this;
}
}

Expand Down
34 changes: 28 additions & 6 deletions src/packages/router/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
import Base from '../base';
import Route from '../route';
import Serializer from '../serializer';

import tryCatch from '../../utils/try-catch';

import bound from '../../decorators/bound';

const { defineProperties } = Object;

const routesKey = Symbol('routes');

class Router extends Base {
serializer = new Serializer();
controllers = new Map();
class Router {
serializer;
controllers;

constructor() {
return super({
[routesKey]: new Map()
defineProperties(this, {
[routesKey]: {
value: new Map(),
writable: false,
enumerable: false,
configurable: false
},

controllers: {
value: new Map(),
writable: true,
enumerable: false,
configurable: false
},

serializer: {
value: new Serializer(),
writable: false,
enumerable: false,
configurable: false
}
});

return this;
}

@bound
Expand Down
67 changes: 40 additions & 27 deletions src/packages/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,59 @@ import { parse as parseURL } from 'url';

import chalk, { cyan } from 'chalk';

import Base from '../base';
import Session from '../session';
import { line } from '../logger';

import formatParams from './utils/format-params';

class Server extends Base {
constructor(props) {
const { router, logger, application } = props;
const { sessionKey, sessionSecret } = application;
const resolver = router.createResolver();
const { defineProperties } = Object;

class Server {
logger;
instance;

super({
logger: logger,
constructor({ logger, router, sessionKey, sessionSecret } = {}) {
const resolver = router.createResolver();
const instance = http.createServer(async (req, res) => {
const { headers } = req;
const methodOverride = headers['X-HTTP-Method-Override'];

instance: http.createServer(async (req, res) => {
const { headers } = req;
const methodOverride = headers['X-HTTP-Method-Override'];
this.logRequest(req, res);

this.logRequest(req, res);
req.setEncoding('utf8');

req.setEncoding('utf8');
res.setHeader('Content-Type', 'application/vnd.api+json');

res.setHeader('Content-Type', 'application/vnd.api+json');
if (methodOverride) {
req.method = methodOverride;
}

if (methodOverride) {
req.method = methodOverride;
}
req.url = parseURL(req.url, true);
req.params = await formatParams(req);
req.session = new Session({
cookie: headers.cookie,
logger,
sessionKey,
sessionSecret
});

req.url = parseURL(req.url, true);
req.params = await formatParams(req);
req.session = Session.create({
cookie: headers.cookie,
logger,
sessionKey,
sessionSecret
});
resolver.next().value(req, res);
});

resolver.next().value(req, res);
})
defineProperties(this, {
logger: {
value: logger,
writable: false,
enumerable: false,
configurable: false
},

instance: {
value: instance,
writable: false,
enumerable: false,
configurable: false
}
});

return this;
Expand Down
Loading