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(routing): implemented basic routing with decorators and separate…
…d demo out of core logic for later extraction
- Loading branch information
Showing
19 changed files
with
223 additions
and
89 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,3 @@ | ||
# Notice | ||
This directory need to be extracted out to ubiquits/ubiquits as a proper | ||
framework demo once the build toolchain is worked out. |
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,5 @@ | ||
import { server } from './main'; | ||
|
||
server.start().then(() => { | ||
console.log('Server running at:', server.getEngine().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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {TestController} from './test.controller'; | ||
export {SimpleController} from './simple.controller'; |
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,20 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Server } from '../../../api/servers/abstract.server'; | ||
import { AbstractController } from '../../../api/controllers/abstract.controller'; | ||
import { RouteBase } from '../../../api/controllers/routeBase.decorator'; | ||
import { Action } from '../../../api/controllers/action.decorator'; | ||
|
||
@Injectable() | ||
@RouteBase('simple') | ||
export class SimpleController extends AbstractController { | ||
|
||
constructor(server: Server) { | ||
super(server); | ||
} | ||
|
||
@Action('GET', '/test/{id}') | ||
public test() { | ||
return 'hello world'; | ||
} | ||
|
||
} |
File renamed without changes.
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,16 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Server } from '../../../api/servers/abstract.server'; | ||
import { AbstractController } from '../../../api/controllers/abstract.controller'; | ||
import { RouteBase } from '../../../api/controllers/routeBase.decorator'; | ||
|
||
@Injectable() | ||
@RouteBase('test') | ||
export class TestController extends AbstractController { | ||
|
||
constructor(server: Server) { | ||
super(server); | ||
|
||
console.log('route base is ', this.routeBase); | ||
} | ||
|
||
} |
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,20 @@ | ||
import { coreInjector } from '../../api/main'; | ||
import { ReflectiveInjector, ResolvedReflectiveProvider } from '@angular/core'; | ||
import { Server } from '../../api/servers/abstract.server'; | ||
import * as Controllers from './controllers'; | ||
import * as _ from 'lodash'; | ||
|
||
// console.log('typeof ControllerMap', typeof ControllerMap); | ||
|
||
// const controllers = ControllerMap.values(); | ||
|
||
let resolvedProviders = ReflectiveInjector.resolve(_.values(Controllers)); | ||
|
||
let injector = ReflectiveInjector.fromResolvedProviders(resolvedProviders, coreInjector); | ||
|
||
export const server: Server = injector.get(Server); | ||
|
||
resolvedProviders.forEach((resolvedControllerProvider: ResolvedReflectiveProvider) => { | ||
console.log('initializing', resolvedControllerProvider.key); | ||
injector.instantiateResolved(resolvedControllerProvider); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Server } from '../servers/abstract.server'; | ||
import { Injectable } from '@angular/core'; | ||
import { Request as HapiRequest, IReply, Response } from 'hapi'; | ||
import * as _ from 'lodash'; | ||
import { Cat } from '../../common/models/index'; | ||
import { Action } from './action.decorator'; | ||
|
||
export interface Request extends HapiRequest { | ||
|
||
} | ||
|
||
export interface RouteParam { | ||
key: string; | ||
value: string; | ||
} | ||
|
||
export type ActionType = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; | ||
|
||
export interface MethodDefinition { | ||
method: ActionType; | ||
route: string; | ||
} | ||
|
||
export interface MethodDictionary { | ||
[methodSignature: string]: MethodDefinition; | ||
} | ||
|
||
@Injectable() | ||
export abstract class AbstractController { | ||
|
||
protected __actionMethods: Map<string, MethodDefinition>; | ||
|
||
protected routeBase: string; | ||
|
||
constructor(protected server: Server) { | ||
this.registerRoutes(); | ||
} | ||
|
||
public registerActionMethod(methodSignature: string, method: ActionType, route: string) { | ||
if (!this.__actionMethods) { | ||
this.__actionMethods = new Map<string, MethodDefinition>(); | ||
} | ||
|
||
const methodDefinition: MethodDefinition = { | ||
method, | ||
route | ||
}; | ||
|
||
this.__actionMethods.set(methodSignature, methodDefinition); | ||
} | ||
|
||
@Action('GET', '/{id}') | ||
public getOne(request: Request, ...routeParams: RouteParam[]) { | ||
|
||
const greeting = new Cat().greet(); | ||
|
||
return {id: routeParams[0], greeting}; | ||
} | ||
|
||
public registerRoutes(): void { | ||
|
||
this.__actionMethods.forEach((methodDefinition: MethodDefinition, methodSignature: string) => { | ||
|
||
this.server.register({ | ||
method: methodDefinition.method, | ||
path: `/${this.routeBase}${methodDefinition.route}`, | ||
handler: (request: Request, reply: IReply): Response => { | ||
|
||
let response = this[methodSignature](request, ...request.paramsArray); | ||
|
||
return reply(response); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
} | ||
|
||
} |
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,8 @@ | ||
import { ActionType } from './abstract.controller'; | ||
export function Action(method: ActionType, route: string) { | ||
|
||
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { | ||
|
||
target.registerActionMethod(propertyKey, method, route); | ||
}; | ||
} |
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,12 @@ | ||
export function RouteBase(base: string) { | ||
|
||
console.log('decorating!'); | ||
|
||
return function (target: Function) { | ||
|
||
(<any>target).prototype.routeBase = base; | ||
|
||
// Reflect.defineMetadata('RouteBase', base, target); | ||
|
||
}; | ||
} |
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,13 +1,12 @@ | ||
import 'es6-shim'; | ||
import 'reflect-metadata'; | ||
import { ReflectiveInjector, provide } from '@angular/core'; | ||
import { Server, HapiServer } from './server'; | ||
import { TestController } from './test'; | ||
let injector = ReflectiveInjector.resolveAndCreate([ | ||
TestController, | ||
import { Server } from './servers/abstract.server'; | ||
import { HapiServer } from './servers/hapi.server'; | ||
import { AbstractController } from './controllers/abstract.controller'; | ||
|
||
export const coreInjector = ReflectiveInjector.resolveAndCreate([ | ||
AbstractController, | ||
provide(Server, {useClass: HapiServer}), | ||
]); | ||
|
||
export const server: HapiServer = injector.get(Server); | ||
|
||
injector.get(TestController); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { IRouteConfiguration } from 'hapi'; | ||
|
||
export abstract class Server { | ||
|
||
constructor() { | ||
this.initialize(); | ||
} | ||
|
||
protected server: any; | ||
|
||
public abstract register(config: IRouteConfiguration): void; | ||
|
||
protected abstract initialize(): this; | ||
|
||
public abstract start(): Promise<this>; | ||
|
||
public getEngine(): any { | ||
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,24 @@ | ||
import { Server as Hapi, IRouteConfiguration } from 'hapi'; | ||
import { Server } from './abstract.server'; | ||
|
||
export class HapiServer extends Server { | ||
|
||
protected initialize() { | ||
this.server = new Hapi(); | ||
|
||
this.server.connection({ | ||
host: 'localhost', | ||
port: 3000 | ||
}); | ||
return this; | ||
} | ||
|
||
public register(config: IRouteConfiguration): void { | ||
return this.server.route(config); | ||
} | ||
|
||
public start(): Promise<this> { | ||
return this.server.start().then(() => this); | ||
} | ||
|
||
} |
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
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