git-history/cli/server.js

36 lines
986 B
JavaScript
Raw Normal View History

2019-02-18 06:52:09 +03:00
const serve = require("koa-static");
const Koa = require("koa");
const pather = require("path");
const getCommits = require("./git");
2019-02-10 05:21:05 +03:00
const getPort = require("get-port");
const open = require("open");
2019-02-18 06:52:09 +03:00
const router = require("koa-router")();
2019-02-10 22:59:26 +03:00
const sitePath = pather.join(__dirname, "site/");
2019-02-10 05:21:05 +03:00
2019-02-18 06:52:09 +03:00
router.get("/api/commits", async ctx => {
const query = ctx.query;
const { path, last = 10, before = null } = query;
2019-02-10 05:21:05 +03:00
2019-02-18 06:52:09 +03:00
const commits = await getCommits(path, last, before);
2019-02-10 05:21:05 +03:00
2019-02-18 06:52:09 +03:00
ctx.body = commits;
});
2019-02-10 05:21:05 +03:00
2019-02-18 06:52:09 +03:00
const app = new Koa();
app.use(router.routes());
app.use(serve(sitePath));
app.on("error", err => {
console.error("Server error", err);
console.error(
"Let us know of the error at https://github.com/pomber/git-history/issues"
);
});
2019-02-10 05:21:05 +03:00
2019-02-18 06:52:09 +03:00
module.exports = async function runServer(path) {
const port = await getPort({ port: 5000 });
app.listen(port);
console.log("Running at http://localhost:" + port);
open(`http://localhost:${port}/?path=${encodeURIComponent(path)}`);
2019-02-10 05:21:05 +03:00
};