1.0.0 • Published 3 years ago

jsn6-http-koa v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

jsn6-http-koa

Http server base on koajs

Example

import { Startup } from 'jsn6-core/dist/core/Startup'
import { KoaServer } from 'jsn6-http-koa'

@Startup('koaServer.start')
export class HttpServer extends KoaServer {
  constructor() {
    super(config.port, config.host)
  }

  isCORS() {
    return true
  }

  async onInit() {
    await this.loadRouters()

    rabbit.registerExchange({
      'test-exchange': { type: 'direct', opts: { durable: true }, autoCreate: true }
    })

    await Promise.all([
      redis.connect(),
      mongo.connect(),
      rabbit.connect(),
    ])

    rabbit.publish('test-exchange', 'test', 'Hello man')

    return super.onInit()
  }

  async isAvailable() {
    await Promise.all([
      redis.check(),
      mongo.check(),
      rabbit.check(),
    ])
  }

  async onDispose() {
    await Promise.all([
      redis.disconnect(),
      mongo.disconnect(),
      rabbit.disconnect(),
    ])
  }

  private async loadRouters() {
    await Promise.all([
      import('@/controllers/http/GlobalRouter'),
    ])
  }

}
  • @/controllers/http/GlobalRouter.ts file
import { HttpServer } from '@/HttpServer'
import { Context, GET } from 'jsn6-http-koa'

declare const KoaServer: HttpServer

export class GlobalRouter {

  @GET('/healthz')
  static async healthz(ctx: Context) {
    await KoaServer.isAvailable()
    ctx.body = ''
  }

}