Skip to content

Instantly share code, notes, and snippets.

View crizstian's full-sized avatar

Cristian Ramirez crizstian

View GitHub Profile
@crizstian
crizstian / di.js
Last active August 11, 2017 18:24
Example of dependency injection registration
const { createContainer, asValue, asFunction, asClass } = require('awilix')
function initDI ({serverSettings, dbSettings, database, models, services}, mediator) {
mediator.once('init', () => {
mediator.on('db.ready', (db) => {
const container = createContainer()
// loading dependecies in a single source of truth
container.register({
database: asValue(db).singleton(),
'use strict'
const {EventEmitter} = require('events')
const server = require('./server/server')
const repository = require('./repository/repository')
const di = require('./config')
const mediator = new EventEmitter()
console.log('--- Booking Service ---')
console.log('Connecting to movies repository...')
@crizstian
crizstian / server-with-container.js
Last active February 5, 2017 17:06
Example of expressjs middleware
const express = require('express')
const morgan = require('morgan')
const helmet = require('helmet')
const bodyparser = require('body-parser')
const cors = require('cors')
const spdy = require('spdy')
const _api = require('../api/booking')
const start = (container) => {
return new Promise((resolve, reject) => {
@crizstian
crizstian / booking.repository.js
Created February 6, 2017 18:29
Example of repository
'use strict'
const repository = (container) => {
// we get the db object via the container
const {db} = container.resolve('database')
const makeBooking = (user, booking) => {
return new Promise((resolve, reject) => {
// payload to be insterted to the booking collection
const payload = {
city: booking.city,
@crizstian
crizstian / payment.service.di.js
Created February 9, 2017 04:25
Example of a stripe charge
const { createContainer, asValue } = require('awilix')
const stripe = require('stripe')
// here we include the stripeSettings
function initDI ({serverSettings, dbSettings, database, models, stripeSettings}, mediator) {
mediator.once('init', () => {
mediator.on('db.ready', (db) => {
const container = createContainer()
container.register({
@crizstian
crizstian / payment.api.js
Last active February 12, 2017 20:24
Example of an API
'use strict'
const status = require('http-status')
module.exports = ({repo}, app) => {
app.post('/payment/makePurchase', (req, res, next) => {
const {validate} = req.container.cradle
validate(req.body.paymentOrder, 'payment')
.then(payment => {
return repo.registerPurchase(payment)
@crizstian
crizstian / payment.repository.js
Created February 9, 2017 04:33
Example of using stripe
// this the function that makes the charge, when it's done
// returns the charge object returned by stripe
const makePurchase = (payment) => {
return new Promise((resolve, reject) => {
// here we retrieve or stripe dependecy
const {stripe} = container.cradle
// we create the charge
@crizstian
crizstian / notification.api.js
Last active February 13, 2017 14:53
Example of di for notifications
module.exports = ({repo}, app) => {
// this our endpoint where is going to validate our email, and the create and finally send it
app.post('/notifiaction/sendEmail', (req, res, next) => {
const {validate} = req.container.cradle
validate(req.body.payload, 'notification')
.then(payload => {
return repo.sendEmail(payload)
})
.then(ok => {
@crizstian
crizstian / booking.raml
Last active September 11, 2017 02:41
Cinema Microservices Endpoints
#%RAML 1.0
title: Booking Service
version: v1
baseUri: /booking
/:
type: { POST: {item : Booking, item2 : User, item3: Ticket} }
/verify/{orderId}:
type: { GET: {item : Ticket} }
@crizstian
crizstian / api-gateway.config.js
Created February 13, 2017 02:56
Example of config file
const fs = require('fs')
const serverSettings = {
port: process.env.PORT || 8080,
ssl: require('./ssl')
}
const machine = process.env.DOCKER_HOST
const tls = process.env.DOCKER_TLS_VERIFY
const certDir = process.env.DOCKER_CERT_PATH