Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
62f0d99
Merge pull request #37 from Arylo/release/1.2.0
Arylo Feb 15, 2018
b29e893
Add TODO
Arylo Feb 15, 2018
8e987cb
Import `helmet` module
Arylo Feb 24, 2018
892df80
Support for `redis-session`
Arylo Feb 24, 2018
6d6d0fb
Update NestFacory Api
Arylo Feb 24, 2018
f69cccd
Update Dockerfile
Arylo Feb 25, 2018
15c793f
Merge pull request #39 from Arylo/feature/redis_session
Arylo Feb 25, 2018
98cabba
Add Regexp Service
Arylo Feb 26, 2018
896939f
Regexp Service replace Regexp Model Method
Arylo Feb 26, 2018
4144095
Add Usergroup Service
Arylo Feb 28, 2018
e74ea8b
Add Usergroup apis
Arylo Mar 1, 2018
ed1ab5e
Add Test Unit
Arylo Mar 1, 2018
4157216
Add the feature of Default Usergroup ID
Arylo Mar 5, 2018
2de7000
Add user into usergroup validator
Arylo Mar 5, 2018
4379d00
Merge pull request #40 from Arylo/feature/usergroup
Arylo Mar 5, 2018
ea60285
Merge branch 'develop' into feature/regexps
Arylo Mar 5, 2018
6e9214b
Support for default values in config file
Arylo Mar 7, 2018
86fd1ab
Update
Arylo Mar 8, 2018
5557445
Add Append Category Test Unit , and update specified category test unit
Arylo Mar 8, 2018
fd57082
Finish TODO
Arylo Mar 8, 2018
8a2f22f
Merge pull request #41 from Arylo/feature/regexps
Arylo Mar 8, 2018
408b958
Update
Arylo Mar 8, 2018
7b0f61b
Add to-array pipe test unit
Arylo Mar 8, 2018
29c36ce
Fix Name repeat name when run test unit
Arylo Mar 8, 2018
6b3a666
delay any second because new file sometime
Arylo Mar 8, 2018
1b06cc2
Fix Service Cache Fail
Arylo Mar 11, 2018
a18268a
Update Code
Arylo Mar 12, 2018
6e96c0c
Remove Unset function
Arylo Mar 13, 2018
949fece
Update Code
Arylo Mar 13, 2018
65ede41
Add Common Exists Validator
Arylo Mar 13, 2018
0b8ab4e
Fix Validator always fail
Arylo Mar 14, 2018
5da6ae1
Add Cors Middleware
Arylo Mar 14, 2018
35499ca
User Nest's Cors
Arylo Mar 14, 2018
3d4b1cd
Merge pull request #42 from Arylo/update
Arylo Mar 14, 2018
949343d
About Gitlab Issue 2
Arylo Mar 14, 2018
09e9ce7
Merge pull request #43 from Arylo/issues/gitlab_2
Arylo Mar 14, 2018
3d22b8d
Move Github Issues Test Units
Arylo Mar 14, 2018
a995527
Lost `category` field in `/goods`
Arylo Mar 14, 2018
2b0fc7e
Update Api Doc
Arylo Mar 14, 2018
1431c66
Add Feature
Arylo Mar 15, 2018
5f46984
Merge pull request #44 from Arylo/issues/gitlab_1
Arylo Mar 15, 2018
e340c0b
Fix Edit Regexp Fail
Arylo Mar 15, 2018
978e518
Merge pull request #45 from Arylo/issues/gitlab_2
Arylo Mar 15, 2018
7ad33c0
Fix Bug
Arylo Mar 16, 2018
55293fd
Add And Update
Arylo Mar 16, 2018
f4b2fd6
Remove Non-exist module
Arylo Mar 16, 2018
7e7d5d1
update
Arylo Mar 17, 2018
30b743e
set Cache to Redis
Arylo Mar 19, 2018
6ab0e69
Merge pull request #46 from Arylo/feature/redis
Arylo Mar 19, 2018
91ea0ef
Update Code
Arylo Mar 20, 2018
9bf014c
Move Origin Url to boxsystem/storebox-api
Arylo Mar 21, 2018
077bb1e
Add System module
Arylo Mar 25, 2018
94a92fc
Add Test Unit
Arylo Mar 25, 2018
f525cba
Fix Cache Namespace name
Arylo Mar 25, 2018
e74464b
Merge pull request #47 from BoxSystem/feature/system
Arylo Mar 25, 2018
1cdffb2
Add new function of ids for test
Arylo Mar 25, 2018
7d700b2
Move Service Files path
Arylo Mar 25, 2018
ec34586
Move helpers to utils and add new cache factory
Arylo Mar 25, 2018
2fcaf23
Add istanbul ignore comment
Arylo Mar 25, 2018
feb1899
Step to v1.3.0
Arylo Mar 25, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
- "npm install"

script:
- "npm test"
- "npm run tsc && npm test"

after_success:
- npm install coveralls@~3.0.0 --global
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ RUN npm run tsc && \
rm -rf node_modules && \
npm install --production && \
npm install cross-env tsconfig-paths --save-dev && \
npm install pm2 --global && \
npm cache clean -f

CMD [ "npm", "run", "start:prod" ]
CMD pm2 start index.js --node-args="-r tsconfig-paths/register" -i 0 --no-daemon
# CMD [ "npm", "run", "start:prod" ]

EXPOSE 9000
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

Store PandoraBox Firmware Images and its packages

[![Travis](https://img.shields.io/travis/Arylo/StoreBox.svg?style=flat-square)](https://travis-ci.org/Arylo/StoreBox)
[![Coveralls](https://img.shields.io/coveralls/github/Arylo/StoreBox.svg?style=flat-square)](https://coveralls.io/github/Arylo/StoreBox)
[![license](https://img.shields.io/github/license/Arylo/StoreBox.svg?style=flat-square)](https://github.com/Arylo/storebox)
[![Travis](https://img.shields.io/travis/BoxSystem/StoreBox-Api.svg?style=flat-square)](https://travis-ci.org/BoxSystem/StoreBox-Api)
[![Coveralls](https://img.shields.io/coveralls/github/BoxSystem/StoreBox-Api.svg?style=flat-square)](https://coveralls.io/github/BoxSystem/StoreBox-Api)
[![Known Vulnerabilities](https://snyk.io/test/github/BoxSystem/StoreBox-Api/badge.svg?style=flat-square)](https://snyk.io/test/github/BoxSystem/StoreBox-Api)
[![license](https://img.shields.io/github/license/BoxSystem/StoreBox-Api.svg?style=flat-square)](https://github.com/BoxSystem/StoreBox-Api)

# Usage

Expand Down
16 changes: 16 additions & 0 deletions TODOLIST.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TODO LIST

- [x] 用户组
- [ ] 权限
- [x] 默认用户组
- [x] 上传到指定Categroy
- [x] 上传时追加Catogroy
- [x] 整顿collectin info的goods 列表
- [ ] Token 使用日志显示
- [ ] Good 下载次数统计
- [ ] 接入统计
- [x] 配置文件写入初始化用户账号密码
- [ ] 接入AuthBox
- [x] Redis 接入
- [x] 支持Session
- [x] 缓存整理
12 changes: 11 additions & 1 deletion config/app.default.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
server:
port: 9000
redis:
host: 127.0.0.1
port: 6379
url:
db:
type: mongo
host: 0.0.0.0
Expand All @@ -10,4 +14,10 @@ paths:
upload: upload
log: log
resource: resource
backup: backup
backup: backup
defaults:
user:
name: root
pass: admin
group:
name: admin
2 changes: 2 additions & 0 deletions docker/app.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
db:
host: "db"
redis:
host: "cache"
path:
tmp: /tmp
6 changes: 6 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ db:
container_name: storebox_db
restart: always

cache:
image: redis
container_name: storebox_cache
restart: always

svr:
build: ..
ports:
Expand All @@ -16,5 +21,6 @@ svr:
- ./app.yaml:/usr/src/app/config/app.yaml:ro
links:
- db
- cache
container_name: storebox_svr
restart: always
27 changes: 17 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"name": "storebox",
"version": "1.2.0",
"name": "storebox-api",
"version": "1.3.0",
"description": "",
"main": "index.js",
"scripts": {
"clean": "rimraf dist tmp",
"tsc": "tsc -P .",
"tsc": "tsc --locale zh-cn --pretty -P .",
"build": "npm run clean && npm run tsc",
"prestart": "npm run build",
"start-svr": "node -r tsconfig-paths/register index.js",
"start:dev": "npm run prestart && cross-env NODE_ENV=development npm run start-svr",
"start:prod": "cross-env NODE_ENV=production npm run start-svr",
"start": "npm run start:prod",
"test-spec": "cross-env NODE_ENV=test mocha",
"test-spec": "cross-env NODE_ENV=test TS_NODE_PROJECT=./test/tsconfig.json mocha",
"test-cov": "nyc npm run test-spec && nyc report",
"test": "npm run test-cov",
"report-coverage": "cat ./coverage/lcov.info | coveralls"
Expand All @@ -27,21 +27,25 @@
"author": "AryloYeung <arylo.open@gmail.com>",
"license": "MIT",
"devDependencies": {
"@nestjs/testing": "^4.5.1",
"@nestjs/testing": "4.6.1",
"@types/basic-auth": "^1.1.2",
"@types/bunyan": "^1.8.4",
"@types/connect-redis": "0.0.7",
"@types/cors": "^2.8.3",
"@types/express": "^4.0.39",
"@types/express-session": "^1.15.6",
"@types/faker": "^4.1.2",
"@types/fs-extra": "^4.0.4",
"@types/hasha": "^3.0.0",
"@types/helmet": "0.0.37",
"@types/is-promise": "^2.1.0",
"@types/lodash": "^4.14.85",
"@types/md5": "^2.1.32",
"@types/mocha": "^2.2.44",
"@types/mocha-steps": "^1.1.0",
"@types/mongoose": "^4.7.26",
"@types/multer": "^1.3.6",
"@types/node": "^9.3.0",
"@types/path-exists": "^3.0.0",
"@types/reflect-metadata": "0.0.5",
"@types/should": "^11.2.0",
Expand All @@ -64,20 +68,24 @@
"typescript": "^2.6.1"
},
"dependencies": {
"@nestjs/common": "^4.5.1",
"@nestjs/core": "^4.5.1",
"@nestjs/swagger": "^1.1.3",
"@types/node": "^9.3.0",
"@keyv/redis": "^1.3.8",
"@nestjs/common": "4.6.5",
"@nestjs/core": "4.6.5",
"@nestjs/swagger": "1.1.4",
"basic-auth": "^2.0.0",
"body-parser": "^1.18.2",
"bunyan": "^1.8.12",
"class-validator": "^0.7.3",
"connect-redis": "^3.3.3",
"cookie-parser": "^1.4.3",
"cors": "^2.8.4",
"express": "^4.16.2",
"express-session": "^1.15.6",
"fs-extra": "^4.0.2",
"hasha": "^3.0.0",
"helmet": "^3.11.0",
"is-promise": "^2.1.0",
"keyv": "^3.0.0",
"lodash": "^4.17.4",
"md5": "^2.2.1",
"md5-file": "^3.2.3",
Expand All @@ -86,7 +94,6 @@
"path-exists": "^3.0.0",
"reflect-metadata": "^0.1.10",
"rxjs": "^5.5.2",
"schedule-cache": "^1.0.0",
"useragent": "^2.2.1",
"uuid": "^3.1.0",
"y-config": "^1.1.5"
Expand Down
22 changes: 19 additions & 3 deletions src/express.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as express from "express";
import * as session from "express-session";
import session = require("express-session");
import * as bodyParser from "body-parser";
import * as cookieParser from "cookie-parser";
import connectRedis = require("connect-redis");
import { config } from "@utils/config";
import helmet = require("helmet");

import { error } from "./modules/common/middlewares/logger.middleware";
import { isTest } from "@utils/env";

const RedisStore = connectRedis(session);

let server: express.Express;

Expand All @@ -16,15 +23,24 @@ export const initExpress = () => {

mServer.enable("trust proxy");

mServer.use(helmet());
mServer.use(bodyParser.json());
mServer.use(bodyParser.urlencoded());
mServer.use(cookieParser("storebox"));
mServer.use(session({
const sessionOpts = {
store: undefined,
secret: "storebox",
resave: false,
saveUninitialized: true,
cookie: { secure: false, maxAge: 7200 * 1000 }
}));
};
/* istanbul ignore if */
if (!isTest) {
sessionOpts.store = new RedisStore({
url: config.redis.url
});
}
mServer.use(session(sessionOpts));
mServer.use(error);

server = mServer;
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { config } from "@utils/config";
import { initExpress } from "./express";
import { ApplicationModule } from "./modules/app.module";
import { ValidationPipe } from "@pipes/validation";
import { systemLogger } from "./modules/common/helper/log";
import { isDevelopment } from "./modules/common/helper/env";
import { systemLogger } from "@utils/log";
import { isDevelopment } from "@utils/env";

const bootstrap = async () => {
const server = initExpress();

const app = await NestFactory.create(ApplicationModule, server);
const app = await NestFactory.create(ApplicationModule, server, {
cors: true
});
app.useGlobalPipes(new ValidationPipe());

if (isDevelopment) {
Expand Down
82 changes: 4 additions & 78 deletions src/models/Categroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { DEF_PER_COUNT } from "@dtos/page";
import { isArray } from "util";
import { reduce, includes, difference } from "lodash";
import { MongoError } from "mongodb";
import Cache = require("schedule-cache");

const cache = Cache.create(`${Date.now()}${Math.random()}`);
import newCache = require("@utils/newCache");

export const FLAG = "categories";

export const cache = newCache(FLAG);

export type CategoryDoc = IDoc<ICategory>;

const Definition: SchemaDefinition = {
Expand Down Expand Up @@ -39,28 +40,6 @@ export interface ICategoryRaw extends ICategory {

const CategorySchema = new Base(Definition).createSchema();

CategorySchema.static("countCategories", async (perNum = 1) => {
const FLAG = `page_count_${perNum}`;
if (cache.get(FLAG)) {
return cache.get(FLAG);
}
const count = Math.ceil((await Model.count({ }).exec()) / perNum);
cache.put(FLAG, count);
return cache.get(FLAG);
});

CategorySchema.static("list", (perNum = DEF_PER_COUNT, page = 1) => {
const FLAG_LIST = `list_${perNum}_${page}`;
if (cache.get(FLAG_LIST)) {
return cache.get(FLAG_LIST);
}
const p = Model.find({ })
.skip((page - 1) * perNum).limit(perNum)
.exec();
cache.put(FLAG_LIST, p);
return cache.get(FLAG_LIST);
});

const getIdGroups = (obj): string[] => {
const selfIdArr = [ obj._id.toString() ];
if (obj.pid) {
Expand Down Expand Up @@ -101,52 +80,8 @@ CategorySchema.static("moveCategory", async (id: ObjectId, pid: ObjectId) => {
}).exec();
});

CategorySchema.static("getCategories", async (tags: string | string[] = [ ]) => {
if (!isArray(tags)) {
tags = [ tags ];
}
if (tags.length === 0) {
return Promise.resolve([ ]);
}
const conditions = tags.length === 1 ? {
tags: { $in: tags }
} : {
$or: reduce(tags, (arr, tag) => {
arr.push({ tags: { $in: [ tag ] } });
return arr;
}, [])
};
const p = (await Model.find(conditions)
.populate({ path: "pid", populate: { path: "pid" } })
.populate("attributes")
.exec())
.map((item) => item.toObject())
.map((item) => {
item.tags = Array.from(new Set(getTags(item)));
delete item.pid;
return item;
})
.filter((item) => {
const diffLength = difference(item.tags, tags).length;
return diffLength + tags.length === item.tags.length ;
});
return p;
});

export interface ICategoryModel<T extends CategoryDoc> extends M<T> {
moveCategory(id: ObjectId, pid: ObjectId): Promise<T>;
getCategories(tags: string | string[]): Promise<ICategoryRaw[]>;
/**
* Category 列表
* @param perNum {number} 每页数量
* @param page {number} 页数
* @return {Promise}
*/
list(perNum?: number, page?: number): Promise<T[]>;
/**
* 返回总页数
*/
countCategories(perNum?: number): Promise<number>;
}

for (const method of MODIFY_MOTHODS) {
Expand All @@ -156,12 +91,3 @@ for (const method of MODIFY_MOTHODS) {
}

export const Model = model(FLAG, CategorySchema) as ICategoryModel<CategoryDoc>;

const getTags = (obj: ICategoryRaw | ICategory) => {
const tags = obj.tags;
const pid = obj.pid as ICategoryRaw | void;
if (pid && pid.tags) {
return tags.concat(getTags(pid));
}
return tags;
};
Loading