import Koa from 'koa'; import Router from 'koa-router'; import {get_setting} from './setting'; import {connectDB} from './database'; import {Watcher} from './diff' import {renderImage, renderVideo, renderZipImage} from './route/render'; 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); 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 = readFileSync("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('/get' ,async (ctx,next)=>{ ctx.body = ctx.query; console.log(JSON.stringify(ctx.query)); await next(); } ); let content_router = getContentRouter(createKnexContentsAccessor(db)); router.use('/content',content_router.routes()); router.get('/ss.mp4',async (ctx,next)=>{ /*for(let i in ctx.header){ if(i !== undefined) console.log(i,ctx.get(i)); }*/ await renderVideo(ctx,"testdata/video_test.mp4"); await next(); }); router.get('/image/:number',async (ctx,next)=>{ let page = ctx.params.number; console.log("page type : "+typeof page) await renderZipImage(ctx,"testdata/test_zip.zip",page); ctx.set("cache-control","max-age=3600"); await next(); }); router.get('/aaaa/:number(\\d+)',async (ctx,next)=>{ let page = ctx.params.number; console.log("matched"); await renderZipImage(ctx,"testdata/test_zip.zip",page); ctx.set("cache-control","max-age=3600"); await next(); }); 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("log"); app.listen(3002); return app; } main();