Skip to content

Commit 5c35557

Browse files
committed
feat: SessionLoader
1 parent 4007ec7 commit 5c35557

File tree

21 files changed

+101
-31
lines changed

21 files changed

+101
-31
lines changed

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,26 @@
3737
"dependencies": {
3838
"confman": "^0.2.10",
3939
"console3": "^1.0.6",
40-
"express": "^4.16.4",
4140
"globby": "^9.2.0",
4241
"koa": "^2.7.0",
4342
"koa-compose": "^4.1.0",
4443
"koa-conditional-get": "^2.0.0",
4544
"koa-etag": "^3.0.0",
4645
"koa-router": "^7.4.0",
46+
"koa-session": "^5.12.0",
4747
"koa-static": "^5.0.0",
4848
"ntils": "^4.0.8",
4949
"nunjucks": "^3.2.0",
5050
"oneport": "^1.0.2",
51-
"reflect-metadata": "^0.1.13",
52-
"stp": "^0.0.4"
51+
"reflect-metadata": "^0.1.13"
5352
},
5453
"devDependencies": {
5554
"@types/express": "^4.16.1",
5655
"@types/globby": "^9.1.0",
5756
"@types/koa": "^2.0.48",
5857
"@types/koa-compose": "^3.2.3",
59-
"@types/koa-conditional-get": "^2.0.0",
60-
"@types/koa-etag": "^3.0.0",
6158
"@types/koa-router": "^7.0.40",
59+
"@types/koa-session": "^5.10.0",
6260
"@types/koa-static": "^4.0.1",
6361
"@types/node": "^12.0.1",
6462
"@types/nunjucks": "^3.1.1",

src/AbstractLoader/AbstractLoader.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,34 @@ export abstract class AbstractLoader<T = any> implements ILoader<T> {
1717
this.options = { ...options };
1818
}
1919

20+
/**
21+
* Koa 实例
22+
*/
23+
protected get server() {
24+
return this.app.server;
25+
}
26+
27+
/**
28+
* IoC 容器
29+
*/
30+
protected get container() {
31+
return this.app.container;
32+
}
33+
34+
/**
35+
* 应用根目录
36+
*/
37+
protected get root() {
38+
return this.app.root;
39+
}
40+
41+
/**
42+
* 环境标识
43+
*/
44+
protected get env() {
45+
return this.app.env;
46+
}
47+
2048
/**
2149
* 已加载的资源或类型列表
2250
*/

src/Application/builtInLoaders.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { ControllerLoader } from "../ControllerLoader";
22
import { ILoaderInfoMap } from "../AbstractLoader";
33
import { InfoLoader } from "../InfoLoader";
4+
import { ModelLoader } from "../ModelLoader";
45
import { ServiceLoader } from "../ServiceLoader";
6+
import { SessionLoader } from "../SessionLoader";
57
import { SetupLoader } from "../SetupLoader";
68
import { StaticLoader } from "../StaticLoader";
79
import { ViewLoader } from "../ViewLoader";
@@ -15,13 +17,16 @@ export const builtLoaders: ILoaderInfoMap = {
1517
options: { path: "./{src}/setups/**/*.{ext}" }
1618
},
1719
model: {
18-
loader: ServiceLoader,
20+
loader: ModelLoader,
1921
options: { path: "./{src}/models/**/*.{ext}" }
2022
},
2123
service: {
2224
loader: ServiceLoader,
2325
options: { path: "./{src}/services/**/*.{ext}" }
2426
},
27+
session: {
28+
loader: SessionLoader
29+
},
2530
controller: {
2631
loader: ControllerLoader,
2732
options: { path: "./{src}/controllers/**/*.{ext}" }

src/Application/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pkg = require("../../package.json");
1+
import { pkg } from "../common/utils";
22

33
/**
44
* ENV 环境变量名

src/ConfigLoader/ConfigLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export class ConfigLoader<T = any> extends AbstractLoader<T> {
1515
const { root } = this.app;
1616
const { path } = this.options;
1717
const configFile = resolve(root, path);
18-
const configParser = new Parser({ env: this.app.env });
18+
const configParser = new Parser({ env: this.env });
1919
const configObject = configParser.load(configFile);
20-
this.app.container.registerValue(CONFIG_ENTITY_KEY, configObject);
20+
this.container.registerValue(CONFIG_ENTITY_KEY, configObject);
2121
}
2222
}

src/ControllerLoader/ControllerLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ export class ControllerLoader<T = any[]> extends IoCLoader<T> {
3535
this.app.router.register(
3636
normalize(`/${ctlInfo.path}/${path}`),
3737
httpMethods,
38-
async (ctx: Context, next: Function) => {
38+
async (ctx: any, next: Function) => {
3939
const ctlInstance = new CtlType();
40-
this.app.container.inject(ctlInstance);
40+
this.container.inject(ctlInstance);
4141
ctx.body = await this.execCtlMethod(ctx, ctlInstance, method);
4242
ctx.preventCahce = true;
4343
await next();

src/ControllerLoader/context.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface ICtxMappingInfo {
1313
* 从 ctx 上获取内容
1414
* @param name JSON Path
1515
*/
16-
export function ctx(name: string) {
16+
export function ctx(name = ".") {
1717
return (target: any, member: string, index: number) => {
1818
const type = "ctx",
1919
list = getCtxMappingInfos(target, member);
@@ -32,6 +32,11 @@ export const req = () => ctx("request");
3232
*/
3333
export const res = () => ctx("response");
3434

35+
/**
36+
* Cookie 信息
37+
*/
38+
export const cookie = () => ctx("cookies");
39+
3540
/**
3641
* 路由参数
3742
* @param name 路由参数名

src/InfoLoader/InfoLoader.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { AbstractLoader } from "../AbstractLoader";
2-
3-
const pkg = require("../../package.json");
2+
import { pkg } from "../common/utils";
43

54
/**
65
* 静态资源加载器
@@ -10,7 +9,7 @@ export class InfoLoader<T = any> extends AbstractLoader<T> {
109
* 加载一个框架信息
1110
*/
1211
public async load() {
13-
this.app.server.use(async (ctx, next) => {
12+
this.server.use(async (ctx, next) => {
1413
ctx.set("Server", pkg.displayName);
1514
await next();
1615
});

src/IoCLoader/IoCLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export class IoCLoader<T = any[]> extends AbstractLoader<T> {
99
*/
1010
public async load<T>() {
1111
await super.load<T>();
12-
this.app.container.registerTypes(this.content);
12+
this.container.registerTypes(this.content);
1313
}
1414
}

src/SessionLoader/SessionLoader.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as session from "koa-session";
2+
import { AbstractLoader } from "../AbstractLoader";
3+
import { pkg } from "../common/utils";
4+
5+
const defaultOptions: any = {
6+
key: `${pkg.name[0]}sid`.toUpperCase(),
7+
maxAge: 86400000
8+
};
9+
10+
/**
11+
* Session 加载器
12+
*/
13+
export class SessionLoader<T = any> extends AbstractLoader<T> {
14+
/**
15+
* 加载 Session
16+
*/
17+
public async load() {
18+
const options = { ...defaultOptions, ...this.options };
19+
this.server.keys = options.keys || [options.key];
20+
this.server.use(session(options, this.server));
21+
}
22+
}

0 commit comments

Comments
 (0)