Skip to content

Commit

Permalink
Merge pull request #22 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat: add facades to project
  • Loading branch information
jlenon7 authored Apr 8, 2022
2 parents adc9ec2 + ad10349 commit 89b00c3
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 126 deletions.
11 changes: 0 additions & 11 deletions container/index.ts

This file was deleted.

6 changes: 5 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* file that was distributed with this source code.
*/

export * from 'src/Facade'
export * from 'src/Ignite'
export * from 'src/Application'

export * from 'src/Utils/AthennaErrorHandler'
export * from 'src/Facades/Log'
export * from 'src/Facades/HttpRoute'
export * from 'src/Facades/HttpServer'
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/core",
"version": "1.1.9",
"version": "1.2.0",
"description": "",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -149,10 +149,6 @@
"src/*.d.ts",
"src/**/*.js",
"src/**/*.d.ts",
"container/*.js",
"container/*.d.ts",
"container/**/*.js",
"container/**/*.d.ts",
"*.js",
"*.d.ts"
],
Expand All @@ -165,7 +161,7 @@
"@athenna/config": "1.0.8",
"@athenna/ioc": "1.1.2",
"@athenna/logger": "1.1.4",
"@athenna/http": "1.1.9",
"@athenna/http": "1.2.0",
"@secjs/utils": "1.8.3",
"reflect-metadata": "0.1.13",
"tscpaths": "0.0.9"
Expand Down
23 changes: 5 additions & 18 deletions src/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@

import { parse } from 'path'
import { Ioc } from '@athenna/ioc'
import { Logger } from '@athenna/logger'
import { Log } from 'src/Facades/Log'
import { Path, resolveModule } from '@secjs/utils'
import { Http, Router as HttpRoute } from '@athenna/http'
import { AthennaErrorHandler } from 'src/Utils/AthennaErrorHandler'
import { NotBootedException } from 'src/Exceptions/NotBootedException'
import { AlreadyBootedException } from 'src/Exceptions/AlreadyBootedException'
import { AlreadyShutdownException } from 'src/Exceptions/AlreadyShutdownException'

export class Application {
/**
* Simple logger for Application class.
*
* @private
*/
private logger: Logger

/**
* An instance of the Ioc class that is a Monostate with
* the Awilix container inside.
Expand Down Expand Up @@ -106,10 +98,6 @@ export class Application {
* @return void
*/
async bootHttpServer(): Promise<Http> {
if (!this.logger) {
this.logger = resolveModule(await import('./Utils/Logger'))
}

if (this.httpServer) {
throw new AlreadyBootedException('HttpServer')
}
Expand All @@ -129,15 +117,14 @@ export class Application {
*/
await this.preloadFile(Path.pwd('routes/http'))

this.httpServer.setErrorHandler(AthennaErrorHandler.http)
this.httpRoute.register()

const port = Config.get('http.port')
const host = Config.get('http.host')

await this.httpServer.listen(port, host)

this.logger.success(`Http server started on http://${host}:${port}`)
Log.success(`Http server started on http://${host}:${port}`)

return this.httpServer
}
Expand All @@ -152,7 +139,7 @@ export class Application {
throw new AlreadyShutdownException('HttpServer')
}

this.logger.warn(`Http server shutdown, bye! :)`)
Log.warn(`Http server shutdown, bye! :)`)

await this.httpServer.close()

Expand All @@ -171,7 +158,7 @@ export class Application {
private async preloadFile(filePath: string) {
const { dir, name } = parse(filePath)

this.logger.success(`Preloading ${name} file`)
Log.success(`Preloading ${name} file`)

await import(`${dir}/${name}${this.extension}`)
}
Expand All @@ -188,7 +175,7 @@ export class Application {
await import(`${dir}/${name}${this.extension}`),
)

this.logger.success('Booting the Http Kernel')
Log.success('Booting the Http Kernel')

await new HttpKernel().registerMiddlewares()
}
Expand Down
38 changes: 38 additions & 0 deletions src/Facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @athenna/core
*
* (c) João Lenon <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

export class ProxyHandler {
private readonly alias: any

public constructor(alias: any) {
this.alias = alias
}

get(_object: any, key: string) {
const provider = ioc.use(this.alias)

if (!provider) {
return () => {}
}

return provider[key]
}
}

export class Facade {
/**
* Creates a new facade for alias.
*
* @param alias
* @return any
*/
static createFor<Provider = any>(alias: string): Provider {
return new Proxy(this, new ProxyHandler(alias))
}
}
3 changes: 2 additions & 1 deletion container/HttpRoute.ts → src/Facades/HttpRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import { Facade } from 'src/Facade'
import { Router } from '@athenna/http'

export const Route = ioc.safeUse<Router>('Athenna/Core/HttpRoute')
export const Route = Facade.createFor<Router>('Athenna/Core/HttpRoute')
3 changes: 2 additions & 1 deletion container/HttpServer.ts → src/Facades/HttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import { Facade } from 'src/Facade'
import { Http } from '@athenna/http'

export const Server = ioc.safeUse<Http>('Athenna/Core/HttpServer')
export const HttpServer = Facade.createFor<Http>('Athenna/Core/HttpServer')
3 changes: 2 additions & 1 deletion container/Logger.ts → src/Facades/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import { Facade } from 'src/Facade'
import { Logger as ILogger } from '@athenna/logger'

export const Logger = ioc.safeUse<ILogger>('Athenna/Core/Logger')
export const Log = Facade.createFor<ILogger>('Athenna/Core/Logger')
23 changes: 4 additions & 19 deletions src/Ignite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,14 @@
import '@athenna/ioc'
import '@athenna/config/src/Utils/global'

import { Logger } from '@athenna/logger'
import { File, Path, resolveModule } from '@secjs/utils'
import { Log } from 'src/Facades/Log'
import { Application } from 'src/Application'
import { resolveEnvFile } from '@athenna/config'
import { normalize, parse, resolve } from 'path'
import { File, Path, resolveModule } from '@secjs/utils'
import { DuplicatedApplicationException } from 'src/Exceptions/DuplicatedApplicationException'

export class Ignite {
/**
* Simple logger for Ignite class.
*
* @private
*/
private logger: Logger

/**
* An instance of the application. Is here that the
* client will bootstrap his type of application.
Expand Down Expand Up @@ -73,12 +66,6 @@ export class Ignite {

this.clearConsole()

/**
* Using import because logger needs to be set after
* resolveNodeEnv method has been called.
*/
this.logger = resolveModule(await import('./Utils/Logger'))

const providers = await this.getProviders()

await this.registerProviders(providers)
Expand Down Expand Up @@ -201,9 +188,7 @@ export class Ignite {
providersNormalized.push(resolveModule(Provider))
})

providersNormalized.forEach(p =>
this.logger.success(`Registering ${p.name}`),
)
providersNormalized.forEach(p => Log.success(`Registering ${p.name}`))

return providersNormalized
}
Expand Down Expand Up @@ -261,7 +246,7 @@ export class Ignite {
preload = normalize(preload)

const { dir, name } = parse(Path.config(preload))
this.logger.success(`Preloading ${name} file`)
Log.success(`Preloading ${name} file`)

return import(`${dir}/${name}${this.extension}`)
})
Expand Down
46 changes: 0 additions & 46 deletions src/Utils/AthennaErrorHandler.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Stubs/config/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export default {
|
*/

log: Env('APP_DEBUG', true),
log: Env('APP_DEBUG', false),
}
2 changes: 1 addition & 1 deletion tests/Stubs/routes/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route } from 'container/HttpRoute'
import { Route } from 'src/Facades/HttpRoute'

Route.get('/healthcheck', ({ response }) => {
return response.status(200).send({ status: 'ok' })
Expand Down
Loading

0 comments on commit 89b00c3

Please sign in to comment.