Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
feat(di): Implemented dependency injection with angular Injector
Browse files Browse the repository at this point in the history
Separated bootstrapping from main so localhost can start server independently
  • Loading branch information
zakhenry committed May 19, 2016
1 parent 8a97705 commit e0f6a33
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 96 deletions.
9 changes: 9 additions & 0 deletions api/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {server} from "./main";

server.start((err:any) => {

if (err) {
throw err;
}
console.log('Server running at:', server.getHapi().info.uri);
});
57 changes: 31 additions & 26 deletions api/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
/// <reference path="../typings/index.d.ts" />
import * as Hapi from 'hapi';
import {Request} from "hapi";
import {IReply} from "hapi";
// import * as Hapi from 'hapi';
// import {Request} from "hapi";
// import {IReply} from "hapi";
//
// import {Cat} from "../common/models/index";
//
// export const server = new Hapi.Server();
// server.connection({host: 'localhost', port: 3000});
//
// server.route({
// method: 'GET',
// path: '/',
// handler: (request:Request, reply:IReply) => {
//
// const greeting = new Cat().greet();
//
// return reply(greeting);
// }
// });
//
import 'es6-shim';
import 'reflect-metadata';
import {ReflectiveInjector, provide} from '@angular/core';

import {Cat} from "../common/models/index";
import {Server, HapiServer} from "./server";
import {TestController} from "./test";
let injector = ReflectiveInjector.resolveAndCreate([
TestController,
provide(Server, {useClass: HapiServer}),
]);

export const server:HapiServer = injector.get(Server);

const server = new Hapi.Server();
server.connection({host:'localhost', port: 3000});

server.route({
method: 'GET',
path: '/',
handler: (request:Request, reply:IReply) => {

const greeting = new Cat().greet();

return reply(greeting);
}
});

server.start((err:any) => {

if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
injector.get(TestController);
34 changes: 34 additions & 0 deletions api/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {Server as BaseServer, IRouteConfiguration} from 'hapi';
import {IPromise} from "hapi";

export abstract class Server {
public abstract register(config:IRouteConfiguration):void;
public abstract start(callback?: (err: any) => void):IPromise<void>;
}

export class HapiServer implements Server {

private server:BaseServer;

constructor() {
this.server = new BaseServer();

this.server.connection({
host: 'localhost',
port: 3000
});
}

public register(config:IRouteConfiguration):void {
return this.server.route(config);
}

public start(callback?: (err: any) => void):IPromise<void> {
return this.server.start(callback);
}

public getHapi():BaseServer {
return this.server;
}

}
29 changes: 29 additions & 0 deletions api/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Injectable} from '@angular/core';
import {Cat} from "../common/models/index";
import {Request} from "hapi";
import {IReply} from "hapi";
import {Server} from "./server";

@Injectable()
export class TestController {

constructor(private Server:Server) {
this.registerRoute();
}

private registerRoute():void {

this.Server.register({
method: 'GET',
path: '/cat',
handler: (request:Request, reply:IReply) => {

const greeting = new Cat().greet();

return reply(greeting);
}
});

}

}
109 changes: 56 additions & 53 deletions browser/config/webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,65 @@ var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
entry: {
'polyfills': './browser/polyfills.ts',
'vendor': './browser/vendor.ts',
'app': './browser/main.ts',
'api': './api/main.ts'
},
entry: {
'polyfills': './browser/polyfills.ts',
'vendor': './browser/vendor.ts',
'app': './browser/main.ts',
},

resolve: {
extensions: ['', '.js', '.ts']
},
resolve: {
extensions: ['', '.js', '.ts']
},

module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts'
},
{
test: /\.html$/,
loader: 'html'
},
{
test: /\.json/,
loader: 'raw'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file?name=assets/[name].[hash].[ext]'
},
{
test: /\.css$/,
exclude: helpers.root('browser', 'app'),
loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
},
{
test: /\.css$/,
include: helpers.root('browser', 'app'),
loader: 'raw'
}
]
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts'
},
{
test: /\.html$/,
loader: 'html'
},
{
test: /\.json/,
loader: 'raw'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file?name=assets/[name].[hash].[ext]'
},
{
test: /\.css$/,
exclude: helpers.root('browser', 'app'),
loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
},
{
test: /\.css$/,
include: helpers.root('browser', 'app'),
loader: 'raw'
}
]
},

plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['api', 'app', 'vendor', 'polyfills']
}),
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: ['app', 'vendor', 'polyfills']
}),

new HtmlWebpackPlugin({
template: 'browser/index.html'
})
],
new HtmlWebpackPlugin({
template: 'browser/index.html'
})
],

//list used core node modules so webpack doesn't complain
node: {
fs: "empty",
net: "empty",
dns: "empty"
}
ts: {
configFileName: 'tsconfig.browser.json'
},

//list used core node modules so webpack doesn't complain
node: {
fs: "empty",
net: "empty",
dns: "empty"
}
};
1 change: 0 additions & 1 deletion browser/webpack.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let tsProject = tsc.createProject('./tsconfig.api.json');
let sourceFiles = ['./api/**/*.ts', './common/**/*.ts'];
let testFiles = ['./api/**/*.spec.ts'];
let outDir = require('./tsconfig.api.json').compilerOptions.outDir;
let entryPoint = './build/api/api/main.js';
let entryPoint = './localhost.js';

/**
* Remove build directory.
Expand Down
37 changes: 23 additions & 14 deletions localhost.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
const WebpackDevServer = require("webpack-dev-server");
const API = require('./build/api/api/main.js');
const server = API.server;

const webpack = require("webpack");
const config = require('./config/webpack.dev.js');
const compiler = webpack(config);
// const WebpackDevServer = require("webpack-dev-server");
//
// const webpack = require("webpack");
// const config = require('./browser/config/webpack.dev.js');
// const compiler = webpack(config);
//
// const webpackDevServer = new WebpackDevServer(compiler, {
// // webpack-dev-server options
// contentBase: "/path/to/directory",
// historyApiFallback: true,
//
// // webpack-dev-middleware options
// stats: 'minimal'
// });
//
// webpackDevServer.listen(8080, "localhost", function() {});

const server = new WebpackDevServer(compiler, {
// webpack-dev-server options
contentBase: "/path/to/directory",
historyApiFallback: true,
server.start((err) => {

// webpack-dev-middleware options
stats: 'minimal'
if (err) {
throw err;
}
console.log('Server running at:', server.getHapi().info.uri);
});

server.listen(8080, "localhost", function() {});

console.log(server);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "ubiquits core",
"main": "index.js",
"scripts": {
"start": "node localhost.js",
"start": "gulp nodemon",
"test": "karma start",
"build": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail",
"postinstall": "typings install"
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./browser/config/webpack.dev.js');

0 comments on commit e0f6a33

Please sign in to comment.