diff --git a/index.js b/index.js index e169b32..0ada8ff 100644 --- a/index.js +++ b/index.js @@ -1,120 +1,10 @@ 'use strict'; -const Application = require('./src/app'); +const { Application, KoaApplication } = require('./src/apps'); const Controller = require('./src/controller'); -const KoaBodyParser = require('koa-bodyparser'); const { Router } = require('./src/router'); -const { printer } = require('@axiosleo/cli-tool'); const response = require('./src/response'); -const session = require('koa-session'); -const KoaStaticServer = require('koa-static-server'); -const path = require('path'); -const Koa = require('koa'); const Model = require('./src/model'); -const { dispatcher } = require('./src/core'); -const { _assign } = require('@axiosleo/cli-tool/src/helper/obj'); -const multer = require('@koa/multer'); - -/** - * @param {import('./index').KoaContext} context - */ -const handleRes = (context) => { - let response = context.response; - if (response.format === 'json' && response.notResolve !== true) { - let code, message; - if (response.code) { - [code, message] = response.code.split(';'); - } - response.data = { - request_id: context.request_id, - timestamp: (new Date()).getTime(), - code: code || `${response.status}`, - message: message || context.response.message, - data: response.data, - }; - } - context.koa.type = response.format; - Object.keys(response.headers).forEach(k => { - context.koa.set(k, response.headers[k]); - }); - context.koa.body = response.data || ''; - context.koa.response.status = response.status; -}; - -class KoaApplication extends Application { - constructor(config = {}) { - config = _assign({ - port: 8080, - listen_host: 'localhost', - debug: false, - routers: [], - app_id: '', - // session_key: '', - session: { - /** (number || 'session') maxAge in ms (default is 1 days) */ - /** 'session' will result in a cookie that expires when session/browser is closed */ - /** Warning: If a session cookie is stolen, this cookie will never expire */ - maxAge: 1296000000, // ms, 15 days - // autoCommit: true, /** (boolean) automatically commit headers (default true) */ - overwrite: true, /** (boolean) can overwrite or not (default true) */ - httpOnly: true, /** (boolean) httpOnly or not (default true) */ - signed: true, /** (boolean) signed or not (default true) */ - rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */ - renew: true, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/ - // secure: process.env.DEBUG ? false : true, /** (boolean) secure cookie*/ - secure: false, - // sameSite: null, /** (string) session cookie sameSite options (default null, don't set it) */ - }, - static: { - rootDir: path.join(__dirname, './public'), - }, - body_parser: {} - }, config); - - printer.println().green('start on ').println(`http://localhost:${config.port}`).println(); - super(config); - this.koa = new Koa(this.config.server); - - // session middleware - if (this.config.session) { - this.koa.keys = [this.app_id]; - this.koa.use(session({ - key: this.config.session_key || 'koa.sess', /** (string) cookie key (default is koa.sess) */ - ...this.config.session - }, this.koa)); - } - - ['a', 'b', 'c'].forEach((item, index) => { - index; - }); - - const upload = multer(); - this.koa.use(upload.any()); - - // body parser - this.koa.use(KoaBodyParser(this.config.body_parser)); - - // dispatcher request - this.koa.use(dispatcher({ - app: this, - app_id: this.app_id, - workflow: this.workflow, - routes: this.routes - })); - - // koa static services - if (this.config.static) { - this.koa.use(KoaStaticServer(this.config.static)); - } - this.on('response', handleRes); - } - - async start() { - this.emit('starting'); - // set '0.0.0.0' for public access - this.koa.listen(this.config.port, this.config.listen_host); - } -} module.exports = { Controller, diff --git a/src/app.js b/src/apps/app.js similarity index 87% rename from src/app.js rename to src/apps/app.js index 4c9ea29..d77b076 100644 --- a/src/app.js +++ b/src/apps/app.js @@ -2,10 +2,9 @@ const EventEmitter = require('events'); const { v4 } = require('uuid'); - const { Configuration, Workflow } = require('@axiosleo/cli-tool'); -const { resolveRouters } = require('./core'); -const flowOperator = require('./workflow'); +const { resolveRouters } = require('../core'); +const flowOperator = require('../workflow'); class Application extends EventEmitter { constructor(config) { diff --git a/src/apps/index.js b/src/apps/index.js new file mode 100644 index 0000000..7b8687a --- /dev/null +++ b/src/apps/index.js @@ -0,0 +1,11 @@ +'use strict'; + +const Application = require('./app'); +const KoaApplication = require('./koa'); +const SocketApplication = require('./koa'); + +module.exports = { + Application, + KoaApplication, + SocketApplication +}; diff --git a/src/apps/koa.js b/src/apps/koa.js new file mode 100644 index 0000000..cd4d79b --- /dev/null +++ b/src/apps/koa.js @@ -0,0 +1,116 @@ +'use strict'; + +const KoaBodyParser = require('koa-bodyparser'); +const session = require('koa-session'); +const KoaStaticServer = require('koa-static-server'); +const multer = require('@koa/multer'); +const path = require('path'); +const Koa = require('koa'); +const { dispatcher } = require('../core'); +const { printer } = require('@axiosleo/cli-tool'); +const { _assign } = require('@axiosleo/cli-tool/src/helper/obj'); + +const Application = require('./app'); + +/** + * @param {import('./index').KoaContext} context + */ +const handleRes = (context) => { + let response = context.response; + if (response.format === 'json' && response.notResolve !== true) { + let code, message; + if (response.code) { + [code, message] = response.code.split(';'); + } + response.data = { + request_id: context.request_id, + timestamp: (new Date()).getTime(), + code: code || `${response.status}`, + message: message || context.response.message, + data: response.data, + }; + } + context.koa.type = response.format; + Object.keys(response.headers).forEach(k => { + context.koa.set(k, response.headers[k]); + }); + context.koa.body = response.data || ''; + context.koa.response.status = response.status; +}; + +class KoaApplication extends Application { + constructor(config = {}) { + config = _assign({ + port: 8080, + listen_host: 'localhost', + debug: false, + routers: [], + app_id: '', + // session_key: '', + session: { + /** (number || 'session') maxAge in ms (default is 1 days) */ + /** 'session' will result in a cookie that expires when session/browser is closed */ + /** Warning: If a session cookie is stolen, this cookie will never expire */ + maxAge: 1296000000, // ms, 15 days + // autoCommit: true, /** (boolean) automatically commit headers (default true) */ + overwrite: true, /** (boolean) can overwrite or not (default true) */ + httpOnly: true, /** (boolean) httpOnly or not (default true) */ + signed: true, /** (boolean) signed or not (default true) */ + rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. (default is false) */ + renew: true, /** (boolean) renew session when session is nearly expired, so we can always keep user logged in. (default is false)*/ + // secure: process.env.DEBUG ? false : true, /** (boolean) secure cookie*/ + secure: false, + // sameSite: null, /** (string) session cookie sameSite options (default null, don't set it) */ + }, + static: { + rootDir: path.join(__dirname, './public'), + }, + body_parser: {} + }, config); + + printer.println().green('start on ').println(`http://localhost:${config.port}`).println(); + super(config); + this.koa = new Koa(this.config.server); + + // session middleware + if (this.config.session) { + this.koa.keys = [this.app_id]; + this.koa.use(session({ + key: this.config.session_key || 'koa.sess', /** (string) cookie key (default is koa.sess) */ + ...this.config.session + }, this.koa)); + } + + ['a', 'b', 'c'].forEach((item, index) => { + index; + }); + + const upload = multer(); + this.koa.use(upload.any()); + + // body parser + this.koa.use(KoaBodyParser(this.config.body_parser)); + + // dispatcher request + this.koa.use(dispatcher({ + app: this, + app_id: this.app_id, + workflow: this.workflow, + routes: this.routes + })); + + // koa static services + if (this.config.static) { + this.koa.use(KoaStaticServer(this.config.static)); + } + this.on('response', handleRes); + } + + async start() { + this.emit('starting'); + // set '0.0.0.0' for public access + this.koa.listen(this.config.port, this.config.listen_host); + } +} + +module.exports = KoaApplication;