ionian/src/server.ts
2021-02-22 23:08:30 +09:00

74 lines
No EOL
2.2 KiB
TypeScript

import Koa from 'koa';
import Router from 'koa-router';
import {get_setting} from './setting';
import {connectDB} from './database';
import {Watcher} from './diff'
import { createReadStream, readFileSync } from 'fs';
import getContentRouter from './route/contents';
import { createKnexContentsAccessor } from './db/contents';
import bodyparser from 'koa-bodyparser';
import {error_handler} from './route/error_handler';
//let Koa = require("koa");
async function main(){
let app = new Koa();
app.use(bodyparser());
app.use(error_handler);
const index_html = readFileSync("index.html");
let router = new Router();
let settings = get_setting();
let db = await connectDB();
let watcher = new Watcher(settings.path[0]);
await watcher.setup([]);
console.log(settings);
router.get('/', async (ctx,next)=>{
ctx.type = "html";
ctx.body = index_html;
});
router.get('/dist/css/style.css',async (ctx,next)=>{
ctx.type = "css";
ctx.body = createReadStream("dist/css/style.css");
});
router.get('/dist/js/bundle.js',async (ctx,next)=>{
ctx.type = "js";
ctx.body = createReadStream("dist/js/bundle.js");
});
router.get('/dist/js/bundle.js.map',async (ctx,next)=>{
ctx.type = "text";
ctx.body = createReadStream("dist/js/bundle.js.map");
});
router.get('/doc/:rest(.*)'
,async (ctx,next)=>{
ctx.type = "html";
ctx.body = index_html;
}
);
router.get('/search'
,async (ctx,next)=>{
ctx.type = "html";
ctx.body = index_html;
}
);
let content_router = getContentRouter(createKnexContentsAccessor(db));
router.use('/content',content_router.routes());
router.use('/content',content_router.allowedMethods());
let mm_count=0;
app.use(async (ctx,next)=>{
console.log(`==========================${mm_count++}`);
console.log(`connect ${ctx.ip} : ${ctx.method} ${ctx.url}`);
await next();
//console.log(`404`);
});
app.use(router.routes());
app.use(router.allowedMethods());
console.log("start server");
app.listen(8080,"0.0.0.0");
return app;
}
main();