Last active
January 29, 2016 10:09
-
-
Save jubianchi/fb1a379d722a52848061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import koa from "koa"; | |
import route from "koa-route"; | |
import crypto from "crypto"; | |
import { createRuntime, call, fork } from './runtime'; | |
import { runtimeMiddleware, put, take, reduxControls } from './runtime-redux'; | |
import { createStore, applyMiddleware } from 'redux' | |
import Router from "./router"; | |
const reducer = (state = {}, action) => state; | |
const createStoreWithRuntime = applyMiddleware(runtimeMiddleware)(createStore); | |
const store = createStoreWithRuntime(reducer); | |
const runtime = createRuntime([ ...reduxControls(store) ]); | |
const app = koa(); | |
const router = new Router(store); | |
const md5sum = crypto.createHash("md5"); | |
function* hello() { | |
while(true) { | |
let { context } = yield take("/"); | |
context.status = 200; | |
context.body = "Hello"; | |
} | |
} | |
function* world() { | |
while(true) { | |
let { context } = yield take("/"); | |
context.body += " World!"; | |
} | |
} | |
function* etag() { | |
let { context } = yield take("/"); | |
md5sum.update(context.body); | |
context.set("ETag", md5sum.digest("hex")); | |
} | |
function* log(context) { | |
while(true) { | |
let { context } = yield take("/"); | |
console.log(`${context.status}: ${context.body}`); | |
} | |
} | |
function* ready() { | |
console.log("App is ready to handle requests") | |
} | |
runtime(function* boot() { | |
app.listen(4000); | |
app.use(router.get("/")); | |
yield [ | |
fork(ready), | |
fork(hello), | |
fork(world), | |
fork(etag), | |
fork(log) | |
]; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import route from "koa-route"; | |
export default class Router { | |
constructor(store) { | |
this.store = store; | |
} | |
get(url) { | |
const store = this.store; | |
return route.get(url, function *() { | |
store.dispatch({ type: url, context: this }); | |
}); | |
} | |
post(url) { | |
// ... | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment