2021-01-02 13:17:29 +09:00
|
|
|
import Koa, { DefaultState } from 'koa';
|
|
|
|
import Router, { IParamMiddleware, IRouterParamContext } from 'koa-router';
|
2020-12-31 03:06:16 +09:00
|
|
|
|
|
|
|
import {get_setting} from './setting';
|
|
|
|
import {connectDB} from './database';
|
|
|
|
import {Watcher} from './diff'
|
2021-01-02 13:17:29 +09:00
|
|
|
|
2020-12-31 03:06:16 +09:00
|
|
|
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';
|
2021-01-03 01:08:57 +09:00
|
|
|
import {MangaReferrer} from './content/manga';
|
|
|
|
import {VideoReferrer} from './content/video';
|
2021-01-02 13:17:29 +09:00
|
|
|
|
2021-01-03 01:08:57 +09:00
|
|
|
import { ContentContext } from './route/context';
|
|
|
|
import { AllContentRouter } from './route/all';
|
2021-01-02 13:17:29 +09:00
|
|
|
|
2020-12-31 03:06:16 +09:00
|
|
|
//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());
|
2021-01-02 13:17:29 +09:00
|
|
|
router.use('/content',content_router.allowedMethods());
|
2021-01-03 01:08:57 +09:00
|
|
|
let ctnrouter = new AllContentRouter();
|
|
|
|
router.all('/image/(.*)', async (ctx,next)=>{
|
|
|
|
ctx.state['content'] = new MangaReferrer("testdata/test_zip.zip");
|
2020-12-31 03:06:16 +09:00
|
|
|
await next();
|
|
|
|
});
|
2021-01-03 01:08:57 +09:00
|
|
|
router.use('/image',ctnrouter.routes());
|
|
|
|
router.all('/ss/(.*)', async (ctx,next)=>{
|
|
|
|
ctx.state['content'] = new VideoReferrer("testdata/video_test.mp4");
|
2020-12-31 03:06:16 +09:00
|
|
|
await next();
|
|
|
|
});
|
2021-01-03 01:08:57 +09:00
|
|
|
router.use('/ss',ctnrouter.routes());
|
2020-12-31 03:06:16 +09:00
|
|
|
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();
|