2018-08-29 01:57:18 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
// I M P O R T S
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-08-29 18:58:55 +02:00
|
|
|
import async from "choo-async";
|
|
|
|
import asyncHtml from "choo-async/html";
|
2018-08-29 01:57:18 +02:00
|
|
|
import choo from "choo";
|
|
|
|
import ssr from "choo-ssr";
|
|
|
|
|
2018-10-10 19:56:35 +02:00
|
|
|
// U T I L S
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
import head from "./components/head";
|
|
|
|
import wrapper from "./components/wrapper";
|
2018-08-29 01:57:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// P R O G R A M
|
|
|
|
|
|
|
|
function main() {
|
2018-08-29 18:58:55 +02:00
|
|
|
const app = async(choo());
|
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
const page = view => (
|
|
|
|
shell(
|
|
|
|
ssr.head(
|
|
|
|
head,
|
|
|
|
ssr.state()
|
|
|
|
),
|
|
|
|
ssr.body(wrapper(view))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
app.use(ssr());
|
|
|
|
|
|
|
|
app.route("/", page(require("./views/home")));
|
|
|
|
app.route("/api/*", page(require("./views/api")));
|
|
|
|
app.route("/*", page(require("./views/redirect")));
|
|
|
|
|
2018-08-29 18:58:55 +02:00
|
|
|
app.mount("html");
|
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:58:55 +02:00
|
|
|
if (typeof window !== "undefined") main();
|
2018-08-29 01:57:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// E X P O R T
|
|
|
|
|
|
|
|
module.exports = exports = main;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// H E L P E R
|
|
|
|
|
2018-10-01 22:47:10 +02:00
|
|
|
function shell(head, body) {
|
2018-08-29 01:57:18 +02:00
|
|
|
return (state, emit) => {
|
2018-08-29 18:58:55 +02:00
|
|
|
const bodyPromise = Promise.resolve(body(state, emit));
|
|
|
|
const headPromise = bodyPromise.then(() => head(state, emit)); // resolve `head` once `body` is resolved
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-08-29 18:58:55 +02:00
|
|
|
return asyncHtml`
|
2018-08-29 01:57:18 +02:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
2018-08-29 18:58:55 +02:00
|
|
|
${headPromise}
|
|
|
|
${bodyPromise}
|
2018-08-29 01:57:18 +02:00
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
}
|