- Start support for auto-generated routes using UniversalPath
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
- Start support for custom view engine props & functions - Start login template and namespace
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {Singleton, Inject} from '../di'
|
||||
import {UniversalPath, Collection} from '../util'
|
||||
import {UniversalPath, Collection, Pipe, universalPath} from '../util'
|
||||
import {Unit} from '../lifecycle/Unit'
|
||||
import {Logging} from './Logging'
|
||||
import {Route} from '../http/routing/Route'
|
||||
@@ -7,6 +7,7 @@ import {HTTPMethod} from '../http/lifecycle/Request'
|
||||
import {ViewEngineFactory} from '../views/ViewEngineFactory'
|
||||
import {ViewEngine} from '../views/ViewEngine'
|
||||
import {lib} from '../lib'
|
||||
import {Config} from './Config'
|
||||
|
||||
/**
|
||||
* Application unit that loads the various route files from `app/http/routes` and pre-compiles the route handlers.
|
||||
@@ -16,6 +17,9 @@ export class Routing extends Unit {
|
||||
@Inject()
|
||||
protected readonly logging!: Logging
|
||||
|
||||
@Inject()
|
||||
protected readonly config!: Config
|
||||
|
||||
protected compiledRoutes: Collection<Route> = new Collection<Route>()
|
||||
|
||||
public async up(): Promise<void> {
|
||||
@@ -68,4 +72,47 @@ export class Routing extends Unit {
|
||||
public getCompiled(): Collection<Route> {
|
||||
return this.compiledRoutes
|
||||
}
|
||||
|
||||
public getAssetPath(...parts: string[]): UniversalPath {
|
||||
return this.getAssetBase().concat(...parts)
|
||||
}
|
||||
|
||||
public getAssetBase(): UniversalPath {
|
||||
return this.getAppUrl().concat(this.config.get('server.builtIns.assets.prefix', '/assets'))
|
||||
}
|
||||
|
||||
public getVendorPath(namespace: string, ...parts: string[]): UniversalPath {
|
||||
return this.getVendorBase().concat(encodeURIComponent(namespace), ...parts)
|
||||
}
|
||||
|
||||
public getVendorBase(): UniversalPath {
|
||||
return this.getAppUrl().concat(this.config.get('server.builtIns.vendor.prefix', '/vendor'))
|
||||
}
|
||||
|
||||
public getAppUrl(): UniversalPath {
|
||||
const rawHost = String(this.config.get('server.url', 'http://localhost')).toLowerCase()
|
||||
const isSSL = rawHost.startsWith('https://')
|
||||
const port = this.config.get('server.port', 8000)
|
||||
|
||||
return Pipe.wrap<string>(rawHost)
|
||||
.unless(
|
||||
host => host.startsWith('http://') || host.startsWith('https'),
|
||||
host => `http://${host}`,
|
||||
)
|
||||
.when(
|
||||
host => {
|
||||
const hasPort = host.split(':').length > 2
|
||||
const defaultRaw = !isSSL && port === 80
|
||||
const defaultSSL = isSSL && port === 443
|
||||
return !hasPort && !defaultRaw && !defaultSSL
|
||||
},
|
||||
host => {
|
||||
const parts = host.split('/')
|
||||
parts[2] += `:${port}`
|
||||
return parts.join('/')
|
||||
},
|
||||
)
|
||||
.tap<UniversalPath>(host => universalPath(host))
|
||||
.get()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user