Skip to content

Instantly share code, notes, and snippets.

@johnthethird
Created March 30, 2017 02:03
Show Gist options
  • Save johnthethird/6853b3031a61c6e22914f4d2786aaef2 to your computer and use it in GitHub Desktop.
Save johnthethird/6853b3031a61c6e22914f4d2786aaef2 to your computer and use it in GitHub Desktop.
Microservice in Micro with livenessProbe
'use strict'
import {send, json} from 'micro'
import jwtAuth from 'micro-jwt-auth'
import visualize from './visualize'
// Import your custom command here
import cmd from './cmd'
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)))
// micro will error out with a 400 Invalid JSON if you GET / Kubernetes has a livenessProbe
// that does just that, so return a 200 instead
const handleErrors = fn => async (req, res) => {
try {
return await fn(req, res)
} catch (err) {
if (req.method == 'GET' && req.url == '/' && err.statusCode == '400') {
send(res, 200, '{"healthy": true}')
} else {
throw(err)
}
}
}
const handler = async(req, res) => {
const input = await json(req)
const output = await cmd(input)
send(res, 200, output)
}
module.exports = compose(
handleErrors,
jwtAuth("sekret"),
visualize
)(handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment