parent
a6995c6a85
commit
a27618d5a3
@ -0,0 +1,53 @@
|
|||||||
|
import Module from '../Module.ts'
|
||||||
|
import Kernel from '../Kernel.ts'
|
||||||
|
import PrepareRequest from './PrepareRequest.ts'
|
||||||
|
import Routing from '../../../unit/Routing.ts'
|
||||||
|
import {Logging} from '../../../service/logging/Logging.ts'
|
||||||
|
import {Request} from '../../Request.ts'
|
||||||
|
import Config from '../../../unit/Config.ts'
|
||||||
|
import ActivatedRoute from '../../routing/ActivatedRoute.ts'
|
||||||
|
import {Injectable} from '../../../../../di/src/decorator/Injection.ts'
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export default class MountActivatedRoute extends Module {
|
||||||
|
public static register(kernel: Kernel) {
|
||||||
|
kernel.register(this).after(PrepareRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected readonly routing: Routing,
|
||||||
|
protected readonly config: Config,
|
||||||
|
protected readonly logger: Logging,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async apply(request: Request): Promise<Request> {
|
||||||
|
let incoming = this.routing.resolve([request.path])
|
||||||
|
this.logger.info(`${request.method} ${incoming}`)
|
||||||
|
|
||||||
|
const prefix: string | undefined = this.config.get('server.prefix')
|
||||||
|
const allow_mount_without_prefix: boolean = this.config.get('server.allow_mount_without_prefix')
|
||||||
|
|
||||||
|
if ( prefix && this.routing.resolve([prefix]) !== '/' ) {
|
||||||
|
if ( incoming.startsWith(prefix) ) {
|
||||||
|
incoming = this.routing.resolve([incoming.replace(prefix, '')])
|
||||||
|
this.logger.verbose(`Resolved prefix: ${incoming}`)
|
||||||
|
} else if ( !allow_mount_without_prefix ) {
|
||||||
|
this.logger.warn(`Will not attempt to mount route with missing prefix: ${incoming}`)
|
||||||
|
this.logger.debug('To allow this, set the config server.allow_mount_without_prefix = true.')
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const activated_route: ActivatedRoute | undefined = this.routing.build(incoming)
|
||||||
|
if ( activated_route ) {
|
||||||
|
this.logger.verbose(`Resolved activated route: ${activated_route.route.route}`)
|
||||||
|
request.route = activated_route
|
||||||
|
} else {
|
||||||
|
this.logger.verbose(`Unable to resolve route to activated route. No matching route was found.`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
import ResponseFactory from './ResponseFactory.ts'
|
||||||
|
import {Rehydratable} from '../../support/Rehydratable.ts'
|
||||||
|
import {Request} from '../Request.ts'
|
||||||
|
|
||||||
|
export default class DehydratedStateResponseFactory extends ResponseFactory {
|
||||||
|
constructor(
|
||||||
|
public readonly rehydratable: Rehydratable
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async write(request: Request): Promise<Request> {
|
||||||
|
request.response.body = JSON.stringify(this.rehydratable.dehydrate())
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
import ResponseFactory from './ResponseFactory.ts'
|
||||||
|
import {Request} from '../Request.ts'
|
||||||
|
|
||||||
|
export default class JSONResponseFactory extends ResponseFactory {
|
||||||
|
constructor(
|
||||||
|
public readonly value: any
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async write(request: Request): Promise<Request> {
|
||||||
|
request.response.body = JSON.stringify(this.value)
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
import AppClass from '../../lifecycle/AppClass.ts'
|
||||||
|
import {Request} from '../Request.ts'
|
||||||
|
|
||||||
|
export default abstract class ResponseFactory extends AppClass {
|
||||||
|
public abstract async write(request: Request): Promise<Request>
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
import ResponseFactory from './ResponseFactory.ts'
|
||||||
|
import {Request} from '../Request.ts'
|
||||||
|
|
||||||
|
export default class StringResponseFactory extends ResponseFactory {
|
||||||
|
constructor(
|
||||||
|
public readonly value: string,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async write(request: Request): Promise<Request> {
|
||||||
|
request.response.body = this.value
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
import AppClass from '../../lifecycle/AppClass.ts'
|
||||||
|
import {Route, RouteParameters} from './Route.ts'
|
||||||
|
|
||||||
|
export default class ActivatedRoute extends AppClass {
|
||||||
|
public readonly params: RouteParameters
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public readonly incoming: string,
|
||||||
|
public readonly route: Route,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
this.params = route.build_parameters(incoming)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
import {Route, RouteParameters, RouteSegment} from './Route.ts'
|
||||||
|
import Utility from '../../service/utility/Utility.ts'
|
||||||
|
import {make} from '../../../../di/src/global.ts'
|
||||||
|
|
||||||
|
export class ComplexRoute extends Route {
|
||||||
|
public match(incoming: string): boolean {
|
||||||
|
const base_length = this.split(this.base).length
|
||||||
|
const incoming_length = this.split(incoming).length
|
||||||
|
return base_length === incoming_length // FIXME match!
|
||||||
|
}
|
||||||
|
|
||||||
|
public build_parameters(incoming: string): RouteParameters {
|
||||||
|
const utility = make(Utility)
|
||||||
|
const params: RouteParameters = {}
|
||||||
|
let current_wildcard: number = 1
|
||||||
|
|
||||||
|
this.zip(incoming).forEach((segment: RouteSegment) => {
|
||||||
|
if ( segment.base.indexOf('*') >= 0 ) {
|
||||||
|
params[`$${current_wildcard}`] = utility.infer(segment.match)
|
||||||
|
current_wildcard += 1
|
||||||
|
} else if ( segment.base.startsWith(':') && segment.base.length > 1 ) {
|
||||||
|
const name = segment.base.substr(1)
|
||||||
|
params[name] = utility.infer(segment.match)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return params
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
import {logger} from "../../service/logging/global.ts";
|
||||||
|
|
||||||
|
export type RouteParameters = { [key: string]: string }
|
||||||
|
export type RouteSegment = { base: string, match: string | undefined }
|
||||||
|
export type ZippedRouteSegments = RouteSegment[]
|
||||||
|
|
||||||
|
export abstract class Route {
|
||||||
|
constructor(
|
||||||
|
protected base: string
|
||||||
|
) { }
|
||||||
|
|
||||||
|
public abstract match(incoming: string): boolean
|
||||||
|
public abstract build_parameters(incoming: string): RouteParameters
|
||||||
|
|
||||||
|
public get route() {
|
||||||
|
return this.base
|
||||||
|
}
|
||||||
|
|
||||||
|
public split(incoming: string) {
|
||||||
|
return incoming.toLowerCase().split('/')
|
||||||
|
}
|
||||||
|
|
||||||
|
public zip(incoming: string) {
|
||||||
|
const incoming_parts: string[] = this.split(incoming)
|
||||||
|
const base_parts: string[] = this.split(this.base)
|
||||||
|
const zipped_parts: ZippedRouteSegments = []
|
||||||
|
|
||||||
|
if ( incoming_parts.length !== base_parts.length ) {
|
||||||
|
logger.warn(`Zipping routes with different part lengths! (Base: ${this.base}, Incoming: ${incoming}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
base_parts.forEach((part, index) => {
|
||||||
|
zipped_parts.push({
|
||||||
|
base: part,
|
||||||
|
match: (index >= incoming_parts.length ? undefined : incoming_parts[index]),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return zipped_parts
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
import {Route, RouteParameters} from './Route.ts'
|
||||||
|
|
||||||
|
export class SimpleRoute extends Route {
|
||||||
|
public match(incoming: string): boolean {
|
||||||
|
return incoming === this.base
|
||||||
|
}
|
||||||
|
|
||||||
|
public build_parameters(incoming: string): RouteParameters {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
export type JSONState = { [key: string]: string | boolean | number | JSONState | Array<string | boolean | number | JSONState> }
|
||||||
|
|
||||||
|
export function isJSONState(what: any): what is JSONState {
|
||||||
|
try {
|
||||||
|
JSON.stringify(what)
|
||||||
|
return true
|
||||||
|
} catch (e) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Rehydratable {
|
||||||
|
dehydrate(): Promise<JSONState>
|
||||||
|
rehydrate(state: JSONState): void | Promise<void>
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
import LifecycleUnit from '../lifecycle/Unit.ts'
|
||||||
|
import {Unit} from '../lifecycle/decorators.ts'
|
||||||
|
import Routes from './Routes.ts'
|
||||||
|
import {RouterDefinition} from '../http/type/RouterDefinition.ts'
|
||||||
|
import {Collection} from '../collection/Collection.ts'
|
||||||
|
import {Route} from '../http/routing/Route.ts'
|
||||||
|
import {SimpleRoute} from "../http/routing/SimpleRoute.ts";
|
||||||
|
import {ComplexRoute} from "../http/routing/ComplexRoute.ts";
|
||||||
|
import ActivatedRoute from "../http/routing/ActivatedRoute.ts";
|
||||||
|
|
||||||
|
export type RouteHandler = () => any
|
||||||
|
export interface RouteDefinition {
|
||||||
|
get?: RouteHandler,
|
||||||
|
post?: RouteHandler,
|
||||||
|
patch?: RouteHandler,
|
||||||
|
delete?: RouteHandler,
|
||||||
|
head?: RouteHandler,
|
||||||
|
put?: RouteHandler,
|
||||||
|
connect?: RouteHandler,
|
||||||
|
options?: RouteHandler,
|
||||||
|
trace?: RouteHandler,
|
||||||
|
}
|
||||||
|
|
||||||
|
const verbs = ['get', 'post', 'patch', 'delete', 'head', 'put', 'connect', 'options', 'trace']
|
||||||
|
|
||||||
|
@Unit()
|
||||||
|
export default class Routing extends LifecycleUnit {
|
||||||
|
protected definitions: { [key: string]: RouteDefinition } = {}
|
||||||
|
protected instances: Collection<Route> = new Collection<Route>()
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected readonly routes: Routes,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public async up() {
|
||||||
|
const route_groups = this.routes.all()
|
||||||
|
for ( const route_group_name of route_groups ) {
|
||||||
|
const route_group: RouterDefinition | undefined = this.routes.get(route_group_name)
|
||||||
|
if ( !route_group ) continue
|
||||||
|
|
||||||
|
const prefix = route_group.prefix || '/'
|
||||||
|
for ( const verb of verbs ) {
|
||||||
|
// @ts-ignore
|
||||||
|
if ( route_group[verb] ) {
|
||||||
|
// @ts-ignore
|
||||||
|
const group = route_group[verb]
|
||||||
|
for ( const key in group ) {
|
||||||
|
if ( !group.hasOwnProperty(key) ) continue
|
||||||
|
const handlers = Array.isArray(group[key]) ? group[key] : [group[key]]
|
||||||
|
const base = this.resolve([prefix, key])
|
||||||
|
|
||||||
|
if ( !this.definitions[base] ) {
|
||||||
|
this.definitions[base] = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
this.definitions[base][verb] = this.build_handler(handlers) // TODO want to rework this
|
||||||
|
this.instances.push(this.build_route(base))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
public build_handler(group: string[]): () => any {
|
||||||
|
return () => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public resolve(parts: string[]): string {
|
||||||
|
const cleaned = parts.map(part => {
|
||||||
|
if ( part.startsWith('/') ) part = part.substr(1)
|
||||||
|
if ( part.endsWith('/') ) part = part.slice(0, -1)
|
||||||
|
return part
|
||||||
|
})
|
||||||
|
|
||||||
|
let joined = cleaned.join('/')
|
||||||
|
if ( joined.startsWith('/') ) joined = joined.substr(1)
|
||||||
|
if ( joined.endsWith('/') ) joined = joined.slice(0, -1)
|
||||||
|
return `/${joined}`.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
public build_route(base: string): Route {
|
||||||
|
if ( !base.includes(':') && !base.includes('*') ) {
|
||||||
|
return new SimpleRoute(base)
|
||||||
|
} else {
|
||||||
|
return new ComplexRoute(base)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO deep-match route
|
||||||
|
}
|
||||||
|
|
||||||
|
public match(incoming: string): Route | undefined {
|
||||||
|
return this.instances.firstWhere((route: Route) => route.match(incoming))
|
||||||
|
}
|
||||||
|
|
||||||
|
public build(incoming: string): ActivatedRoute | undefined {
|
||||||
|
const route: Route | undefined = this.match(incoming)
|
||||||
|
if ( route ) {
|
||||||
|
return new ActivatedRoute(incoming, route)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue