Skip to content

Instantly share code, notes, and snippets.

@jubianchi
Last active January 29, 2016 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jubianchi/fb1a379d722a52848061 to your computer and use it in GitHub Desktop.
Save jubianchi/fb1a379d722a52848061 to your computer and use it in GitHub Desktop.
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)
];
})
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