This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(di): Implemented dependency injection with angular Injector
Separated bootstrapping from main so localhost can start server independently
- Loading branch information
Showing
10 changed files
with
185 additions
and
96 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
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); | ||
}); |
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 |
---|---|---|
@@ -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); |
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,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; | ||
} | ||
|
||
} |
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 @@ | ||
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); | ||
} | ||
}); | ||
|
||
} | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
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
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); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./browser/config/webpack.dev.js'); |