How to use awilix-koa - 10 common examples

To help you get started, we’ve selected a few awilix-koa examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inkubux / MediaSpeed / src / routes / library-api.js View on Github external
try {
            const query = ctx.params.id ? { uid: ctx.params.id } : {};

            const libraries = await this.service.find(query);
            Promise.all(libraries.map(async lib => this.libraryScanner.scan(lib)));
        } catch (err) {
            console.log('Error occurred while scanning', err);
        }
    }
}

// Maps routes to method calls on the `api` controller.
// See the `awilix-router-core` docs for info:
// https://github.com/jeffijoe/awilix-router-core
export default createController(LibraryRestApi)
    .prefix('/api/libraries')
    .get('', 'find')
    .get('/:id', 'get')
    .post('', 'create')
    .patch('/:id', 'update')
    .delete('/:id', 'remove')
    .post('/:id/scan', 'scan')
    .post('/scan', 'scan');
github inkubux / MediaSpeed / src / routes / transcode-api.js View on Github external
/* eslint-disable no-useless-constructor */
import { createController } from 'awilix-koa';
import StreamApi from './base-stream-api';

class TranscodeApi extends StreamApi {
    constructor(movieService, episodeService, ffmpegStreamer) {
        super(movieService, episodeService, ffmpegStreamer);
    }

    getRange(ctx) {
        return false;
    }
}

export default createController(TranscodeApi)
    .prefix('/transcode')
    .get('/:type/:id', 'stream');
github inkubux / MediaSpeed / src / routes / season-api.js View on Github external
/* eslint-disable no-useless-constructor */
import { createController } from 'awilix-koa';
import BaseRestApi from './base-api';

class SeasonsRestApi extends BaseRestApi {
    constructor(seasonService) {
        super(seasonService);
    }
}

// Maps routes to method calls on the `api` controller.
// See the `awilix-router-core` docs for info:
// https://github.com/jeffijoe/awilix-router-core
export default createController(SeasonsRestApi)
    .prefix('/api/seasons')
    .get('', 'find')
    .get('/:id', 'get')
    .post('', 'create')
    .patch('/:id', 'update')
    .delete('/:id', 'remove');
github inkubux / MediaSpeed / src / routes / episode-api.js View on Github external
/* eslint-disable no-useless-constructor */
import { createController } from 'awilix-koa';
import BaseRestApi from './base-api';

class EpisodeRestApi extends BaseRestApi {
    constructor(episodeService) {
        super(episodeService);
    }
}

// Maps routes to method calls on the `api` controller.
// See the `awilix-router-core` docs for info:
// https://github.com/jeffijoe/awilix-router-core
export default createController(EpisodeRestApi)
    .prefix('/api/episodes')
    .get('', 'find')
    .get('/:id', 'get')
    .post('', 'create')
    .patch('/:id', 'update')
    .delete('/:id', 'remove');
github jeffijoe / koa-es7-boilerplate / src / routes / todos-api.js View on Github external
// just over HTTP.
const api = todoService => ({
  findTodos: async ctx => ctx.ok(await todoService.find(ctx.query)),
  getTodo: async ctx => ctx.ok(await todoService.get(ctx.params.id)),
  createTodo: async ctx =>
    ctx.created(await todoService.create(ctx.request.body)),
  updateTodo: async ctx =>
    ctx.ok(await todoService.update(ctx.params.id, ctx.request.body)),
  removeTodo: async ctx =>
    ctx.noContent(await todoService.remove(ctx.params.id))
})

// Maps routes to method calls on the `api` controller.
// See the `awilix-router-core` docs for info:
// https://github.com/jeffijoe/awilix-router-core
export default createController(api)
  .prefix('/todos')
  .get('', 'findTodos')
  .get('/:id', 'getTodo')
  .post('', 'createTodo')
  .patch('/:id', 'updateTodo')
  .delete('/:id', 'removeTodo')
github inkubux / MediaSpeed / src / routes / show-api.js View on Github external
return ctx.ok(await this.seasonService.find(query));
    }

    async getEpisodes(ctx) {
        let query = { ...{ show_uid: ctx.params.id }, ...ctx.query };

        if (ctx.params.sid) {
            const seasonIdkey = isNaN(ctx.params.sid) ? 'season_uid' : 'season_number';
            query[seasonIdkey] = ctx.params.sid;
        }

        return ctx.ok(await this.episodeService.find(query));
    }
}

export default createController(ShowRestApi)
    .prefix('/api/shows')
    .get('', 'find')
    .get('/:id', 'get')
    .get('/:id/seasons', 'getSeasons')
    .get('/:id/episodes', 'getEpisodes')
    .get('/:id/seasons/:sid', 'getSeasons')
    .get('/:id/seasons/:sid/episodes', 'getEpisodes')
    .post('', 'create')
    .patch('/:id', 'update')
    .delete('/:id', 'remove');
// Maps routes to method calls on the `api` controller.
// See the `awilix-router-core` docs for info:
// https://github.com/jeffijoe/awilix-router-core
github mollyywang / koa2-mongodb-jwt-server / src / lib / server.js View on Github external
}).unless({
    path: [/^\/public/, "/"]
  }));

  app
    // Top middleware is the error handler.
    .use(errorHandler)
    // Compress all responses.
    .use(compress())
    // Adds ctx.ok(), ctx.notFound(), etc..
    .use(respond())
    // Parses request bodies.
    .use(bodyParser())
    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    .use(scopePerRequest(container))
    // load routes 
    .use(loadControllers('../routes/*.js', { cwd: __dirname }))

  //open database 开启数据库
  dbconnect()

  // Create http server 开启服务
  const server = http.createServer(app.callback())
  // Add a `close` event listener 监听应用关闭
  server.on('close', () => {
    // tear down database connections, etc.
    logger.debug('Server closing, bye!')
  })
  logger.debug('Server created, ready to listen', { scope: 'startup' })
  return server
}
github jeffijoe / koa-es7-boilerplate / src / lib / server.js View on Github external
// Container is configured with our services and whatnot.
  const container = (app.container = configureContainer())
  app
    // Top middleware is the error handler.
    .use(errorHandler)
    // Compress all responses.
    .use(compress())
    // Adds ctx.ok(), ctx.notFound(), etc..
    .use(respond())
    // Handles CORS.
    .use(cors())
    // Parses request bodies.
    .use(bodyParser())
    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    .use(scopePerRequest(container))
    // Create a middleware to add request-specific data to the scope.
    .use(registerContext)
    // Load routes (API "controllers")
    .use(loadControllers('../routes/*.js', { cwd: __dirname }))
    // Default handler when nothing stopped the chain.
    .use(notFoundHandler)

  // Creates a http server ready to listen.
  const server = http.createServer(app.callback())

  // Add a `close` event listener so we can clean up resources.
  server.on('close', () => {
    // You should tear down database connections, TCP connections, etc
    // here to make sure Jest's watch-mode some process management
    // tool does not release resources.
    logger.debug('Server closing, bye!')
github inkubux / MediaSpeed / src / lib / server.js View on Github external
rootDir: app.container.resolve('imageDestinationFolder')
            })
        );

    let buildFolder = process.env.NODE_ENV !== ' development' ? '/build' : '';
    app.use(
        serveStatic({
            rootPath: '/web',
            rootDir: path.join(__dirname, '/../../frontend', buildFolder),
            notFoundFile: 'index.html'
        })
    );

    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    app.use(scopePerRequest(container));

    // Load routes (API "controllers")
    app.use(loadControllers('../routes/*.js', { cwd: __dirname }));
    // Default handler when nothing stopped the chain.
    app.use(notFoundHandler);

    // Creates a http server ready to listen.
    const server = http.createServer(app.callback());

    // Add a `close` event listener so we can clean up resources.
    server.on('close', () => {
        // You should tear down database connections, TCP connections, etc
        // here to make sure Jest's watch-mode some process management
        // tool does not release resources.
        logger.debug('Server closing, bye!');
    });
github mollyywang / koa2-mongodb-jwt-server / src / lib / server.js View on Github external
}));

  app
    // Top middleware is the error handler.
    .use(errorHandler)
    // Compress all responses.
    .use(compress())
    // Adds ctx.ok(), ctx.notFound(), etc..
    .use(respond())
    // Parses request bodies.
    .use(bodyParser())
    // Creates an Awilix scope per request. Check out the awilix-koa
    // docs for details: https://github.com/jeffijoe/awilix-koa
    .use(scopePerRequest(container))
    // load routes 
    .use(loadControllers('../routes/*.js', { cwd: __dirname }))

  //open database 开启数据库
  dbconnect()

  // Create http server 开启服务
  const server = http.createServer(app.callback())
  // Add a `close` event listener 监听应用关闭
  server.on('close', () => {
    // tear down database connections, etc.
    logger.debug('Server closing, bye!')
  })
  logger.debug('Server created, ready to listen', { scope: 'startup' })
  return server
}

awilix-koa

Awilix helpers for Koa

MIT
Latest version published 2 months ago

Package Health Score

64 / 100
Full package analysis

Similar packages