Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nkzawa committed May 2, 2016
0 parents commit a501848
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "standard",
"parser": "babel-eslint",
"rules": {
"yoda": 0,
"semi": [2, "never"],
"no-extra-semi": 2
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
node_modules
*.log
20 changes: 20 additions & 0 deletions bin/micro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

import program from 'commander'
import micro from 'micro-core'

program
.option('-p, --port <port>', 'Port to listen on (3000)', parseInt)
.parse(process.argv)

const port = program.port || 3000
const file = program.args[0]

const mod = require(file).default

micro(mod).listen(port, (err) => {
if (err) {
console.error(err.stack)
process.exit(1)
}
})
80 changes: 80 additions & 0 deletions bin/micro-cluster
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env node

import program from 'commander'
import { parse as parseUrl } from 'url'
import { dirname, resolve } from 'path'
import { readFile } from 'mz/fs'
import { spawn } from 'mz/child_process'
import micro from 'micro-core'
import { createProxyServer } from 'http-proxy'
import getPort from 'get-port'
import { version } from '../../package'

program
.version(version)
.option('-p, --port <port>', 'Port to listen on (3000)', parseInt)
.parse(process.argv)

const programPort = program.port || 3000
let file = program.args[0] || 'app.json'
file = resolve(process.cwd(), file)

readFile(file, 'utf8')
.then(async (data) => {
const config = JSON.parse(data)
const mappings = new Map()

for (const route of Object.keys(config)) {
let path = config[route]
let port
let env

if ('string' !== typeof path) {
path = path.path
port = path.port
env = path.env
}

if (!port) port = await getPort()
path = resolve(dirname(file), path)

const opts = { env, stdio: 'inherit', customFds: [0, 1, 2] }
await spawn(resolve(__dirname, 'micro'), ['--port', port, path], opts)
mappings.set(route, port)
}

const proxy = createProxyServer()

micro(async (req, res) => {
const parsed = parseUrl(req.url)
const { pathname } = parsed

const match = [...mappings].some(([route, port]) => {
if (!pathname.startsWith(route)) return

const rest = pathname.slice(route.length)
if (rest && '/' !== rest[0]) return

const target = `http://localhost:${port}${rest}${parsed.search || ''}`
proxy.web(req, res, { target, ignorePath: true })
return true
})

if (!match) {
res.writeHead(404)
res.end('Not found')
return
}
}).listen(programPort, (err) => {
if (err) {
console.error(err.stack)
process.exit(1)
}

console.log(`> \u001b[96mReady!\u001b[39m Listening on ${programPort}.`)
})
}).catch((err) => {
process.nextTick(() => {
throw err
})
})
30 changes: 30 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const gulp = require('gulp')
const del = require('del')
const cache = require('gulp-cached')
const eslint = require('gulp-eslint')
const help = require('gulp-task-listing')
const babel = require('gulp-babel')
const ext = require('gulp-ext')

gulp.task('help', help)

gulp.task('compile', function () {
return gulp.src('bin/**/*')
.pipe(cache('bin'))
.pipe(babel())
.pipe(ext.crop())
.pipe(gulp.dest('build/bin'))
})

gulp.task('lint', function () {
return gulp.src([
'gulpfile.js',
'bin/*'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError())
})

gulp.task('clean', () => del(['build']))
gulp.task('default', ['lint', 'compile'])
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "micro-cluster",
"version": "0.0.0",
"description": "",
"private": true,
"files": [
"build"
],
"bin": {
"micro-cluster": "build/bin/micro-cluster"
},
"babel": {
"presets": [
"es2015"
],
"plugins": [
"syntax-async-functions",
"transform-async-to-generator",
"transform-runtime"
]
},
"dependencies": {
"commander": "2.9.0",
"get-port": "2.1.0",
"http-proxy": "1.13.2",
"micro-core": "0.1.0",
"mz": "2.4.0"
},
"devDependencies": {
"babel-eslint": "6.0.4",
"babel-plugin-syntax-async-functions": "6.5.0",
"babel-plugin-transform-async-to-generator": "6.7.4",
"babel-plugin-transform-runtime": "6.7.5",
"babel-preset-es2015": "6.6.0",
"babel-runtime": "6.6.1",
"del": "2.2.0",
"eslint": "2.9.0",
"eslint-config-standard": "5.2.0",
"eslint-plugin-promise": "1.1.0",
"eslint-plugin-standard": "1.3.2",
"gulp": "3.9.1",
"gulp-babel": "6.1.2",
"gulp-cached": "1.1.0",
"gulp-eslint": "2.0.0",
"gulp-ext": "1.0.0",
"gulp-task-listing": "1.0.1"
}
}

0 comments on commit a501848

Please sign in to comment.