diff --git a/packages/app/app.ts b/packages/app/app.ts index b67d725..375cd7c 100644 --- a/packages/app/app.ts +++ b/packages/app/app.ts @@ -12,15 +12,33 @@ import { createApp } from './src/index'; buildin: { serve: { enable: true, root: join(import.meta.url, '.', 'public'), opts: {} }, cors: { enable: true }, + jwt: { + enable: true, + secret: 'shhhhhh', + debug: true, + unless: ['/public', '/a'], + }, + view: { + enable: true, + root: join(import.meta.url, '.', 'view'), + opts: { + map: { + html: 'ejs', + }, + }, + }, }, beforeAll: async (ctx, next) => { - console.dir(ctx.jwt); + // console.dir(ctx.jwt); await next(); }, }); + // rpc.view vs rpc.fn变成插件 rpc.fn('a', function (a) { - return a; + console.dir(this.render); + // this.render('user', { user: { name: 'alfred' } }); + return { a: a }; }); rpc.start(); diff --git a/packages/app/package.json b/packages/app/package.json index 1ac5922..095881d 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -37,6 +37,7 @@ "@tomrpc/mount": "^1.0.0", "debug": "^4.3.4", "desm": "^1.3.0", + "ejs": "^3.1.9", "flat": "^6.0.1", "jsonwebtoken": "^9.0.2", "koa-cors": "^0.0.16", diff --git a/packages/app/src/index.ts b/packages/app/src/index.ts index 4897c01..c984386 100644 --- a/packages/app/src/index.ts +++ b/packages/app/src/index.ts @@ -22,10 +22,26 @@ const log = debug('@tomrpc/app'); // debug: true // logLevel: // } +interface ICors { + enable?: boolean | false; + opts?: object; +} interface IServe { enable?: boolean | false; root: string; - opts: object; + opts?: object; +} +interface IView { + enable?: boolean | false; + root: string; + opts?: object; +} +interface IJwt { + enable?: boolean | false; + secret?: string; + issuer?: string; + debug: boolean; + unless?: Array; } interface IConfig { name: string | 'tomapp'; @@ -35,9 +51,9 @@ interface IConfig { mount?: string; buildin: { serve?: IServe; - cors?: { enable: true }; - view?: { enable: true }; - jwt?: { enable: true }; + cors?: ICors; + view?: IView; + jwt?: IJwt; }; beforeAll: any; } diff --git a/packages/app/src/mw/cors.ts b/packages/app/src/mw/cors.ts index a75d3c9..86243eb 100644 --- a/packages/app/src/mw/cors.ts +++ b/packages/app/src/mw/cors.ts @@ -3,8 +3,8 @@ import corsMiddleware from '@koa/cors'; export const cors = { name: 'cors', lifeCycle: 'load', - mw: (opts = {}) => { + mw: (cfg = { opts: {} }) => { // console.dir(opts); - return corsMiddleware(opts); + return corsMiddleware(cfg.opts); }, }; diff --git a/packages/app/src/mw/jwt.ts b/packages/app/src/mw/jwt.ts index 0b883d3..5a5d830 100644 --- a/packages/app/src/mw/jwt.ts +++ b/packages/app/src/mw/jwt.ts @@ -5,6 +5,6 @@ export const jwt = { lifeCycle: 'load', mw: (opts) => { // console.dir(opts); - return koajwt(opts); + return koajwt(opts).unless({ path: opts.unless }); }, }; diff --git a/packages/app/src/mw/view.ts b/packages/app/src/mw/view.ts index 7a0c31c..cc43f1e 100644 --- a/packages/app/src/mw/view.ts +++ b/packages/app/src/mw/view.ts @@ -1,7 +1,10 @@ +import views from 'koa-views'; + export const view = { name: 'view', lifeCycle: 'load', - mw: async (ctx: any, next: any) => { - await next(); + mw: (cfg) => { + // console.dir(opts); + return views(cfg.root, cfg.opts); }, }; diff --git a/packages/app/view/user.html b/packages/app/view/user.html new file mode 100644 index 0000000..f824178 --- /dev/null +++ b/packages/app/view/user.html @@ -0,0 +1,3 @@ +<% if (user) { %> +

<%= user.name %>

+<% } %>