Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Release/1.9.0-Share API (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarqin authored Nov 2, 2022
1 parent 6313848 commit d58dcc3
Show file tree
Hide file tree
Showing 29 changed files with 514 additions and 14 deletions.
7 changes: 5 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# eoapi-server coinfigure
EOAPI_SERVER_PORT=3000
EOAPI_SERVER_PATH=/api
EOAPI_API_PREFIX=/api

# mysql configure
TZ=Asia/Shanghai
Expand All @@ -17,4 +17,7 @@ SWAGGER_ENABLE=true
SWAGGER_VERSION=1.0
SWAGGER_TITLE=Eoapi-remote-server API文档
SWAGGER_DESC=Eoapi remote server API document。


# eoapi-test-server
TEST_SERVER_PORT=4201
EOAPI_WEBSOCKET_POST=4202
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ lerna-debug.log*

# env
.env.local
.env.prodction
.env.prodction
docker-compose.dev.yaml
Dockerfile.dev
69 changes: 69 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
version: '3'

services:
eoapi-remote-server-test:
# build: 从当前路径构建镜像
# build: .
image: eolinker/eoapi-remote-server:1.9.0
container_name: eoapi-remote-server-test
deploy:
restart_policy:
condition: on-failure
max_attempts: 10
environment:
# 服务配置
EOAPI_SERVER_PORT: 3001
EOAPI_API_PREFIX: /api
MYSQL_HOST: host.docker.internal # 数据库地址
TZ: Asia/Shanghai
# 数据库配置
MYSQL_PORT: 33066 # 数据库端口号
MYSQL_USERNAME: root
MYSQL_DATABASE: eoapi-test
MYSQL_PASSWORD: 123456a.
MYSQL_ROOT_PASSWORD: 123456a.
# swagger配置
SWAGGER_PATH: swagger-docs
SWAGGER_ENABLE: 'true'
SWAGGER_VERSION: 1.0
SWAGGER_TITLE: Eoapi-remote-server API文档
SWAGGER_DESC: Eoapi remote server API document。
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '3001:3001'
networks:
- eoapi_net

eoapi:
image: eolinker/eoapi:1.9.0-dev
container_name: eoapi-dev
deploy:
restart_policy:
condition: on-failure
max_attempts: 3
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '3002:80'
networks:
- eoapi_net

eoapi-test-server:
image: eolinker/eoapi-test-server:1.9.0-dev
container_name: eoapi-test-server-dev
deploy:
restart_policy:
condition: on-failure
max_attempts: 3
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '42011:4201'
- '42022:4202'
networks:
- eoapi_net

networks:
eoapi_net:
name: eoapi_net
49 changes: 43 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ version: '3'
services:
eoapi-remote-server:
# build: 从当前路径构建镜像
build: .
image: eoapi/eoapi-remote-server:1.8.2
# build: .
image: eolinker/eoapi-remote-server:latest
container_name: eoapi-remote-server
restart: always
deploy:
restart_policy:
condition: on-failure
max_attempts: 10
env_file:
- .env
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${EOAPI_SERVER_PORT}:3000'
- 3000:3000
# 当前服务启动之前先要把depends_on指定的服务启动起来才行
depends_on:
- mysql
Expand All @@ -22,17 +25,51 @@ services:
mysql:
image: mysql:latest
container_name: eoapi-mysql
restart: always
deploy:
restart_policy:
condition: on-failure
max_attempts: 10
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci #设置utf8字符集
env_file:
- .env
ports:
- '${MYSQL_PORT}:3306'
- 33066:3306
volumes:
- ./sample/mysql/:/var/lib/mysql/ # ./sample/mysql/路径可以替换成自己的路径
networks:
- eoapi_net

eoapi:
image: eolinker/eoapi:latest
container_name: eoapi
deploy:
restart_policy:
condition: on-failure
max_attempts: 3
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '3002:80'
# volumes:
# - ./nginx.conf:/etc/nginx/conf.d/default.conf
networks:
- eoapi_net

eoapi-test-server:
image: eolinker/eoapi-test-server:latest
container_name: eoapi-test-server
deploy:
restart_policy:
condition: on-failure
max_attempts: 3
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '42011:4201'
- '42022:4202'
networks:
- eoapi_net

networks:
eoapi_net:
name: eoapi_net
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eoapi-remote-server",
"version": "1.8.2",
"version": "1.9.0",
"description": "Storage api data in remote server",
"author": "eoapi",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export class AppController {
@Get('system/status')
@Public()
status() {
return `v${version}`;
return version;
}
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppController } from './app.controller';
import { AppService } from './app.service';
// 引入数据库的及配置文件
import { AuthModule } from './modules/auth/auth.module';
import { ShareDocsModule } from './modules/shared-docs/shared-docs.module';
import { getConfiguration } from './config/configuration';
import { UserModule } from '@/modules/user/user.module';
import { WorkspaceModule } from '@/modules/workspace/workspace.module';
Expand Down Expand Up @@ -37,6 +38,7 @@ console.log('process.env.NODE_ENV', `.env.${process.env.NODE_ENV}`);
SharedModule,
AuthModule, // 认证
UserModule,
ShareDocsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
19 changes: 19 additions & 0 deletions src/common/decorators/xheader.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export type XHeaderType = {
projectID: number;
workspaceID: number;
};

type Keys = keyof XHeaderType;

export const XHeader = createParamDecorator(
(_data: Keys, ctx: ExecutionContext) => {
const { headers } = ctx.switchToHttp().getRequest();
const result: XHeaderType = {
projectID: headers['x-project-id'],
workspaceID: headers['x-workspace-id'],
};
return result[_data] ?? result;
},
);
12 changes: 12 additions & 0 deletions src/entities/shared.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Column, Entity, Generated } from 'typeorm';
import { FictitiousBase } from './base.entity';

@Entity({ name: 'shared' })
export class SharedEntity extends FictitiousBase {
@Column()
@Generated('uuid')
uniqueID: string;

@Column()
projectID: number;
}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { ValidationPipe } from '@/pipe/validation.pipe';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();

app.setGlobalPrefix(process.env.EOAPI_API_PREFIX || '/api');

app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));

Expand Down
34 changes: 34 additions & 0 deletions src/migrations/1666580390462-update-table_1_9_0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class updateTable1901666580390462 implements MigrationInterface {
name = 'updateTable1901666580390462'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`shared\` (\`createdAt\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updatedAt\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`uuid\` int NOT NULL AUTO_INCREMENT, \`uniqueID\` varchar(36) NOT NULL, \`projectID\` int NOT NULL, PRIMARY KEY (\`uuid\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`api_data\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`api_data\` ADD \`name\` varchar(255) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`api_group\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`api_group\` ADD \`name\` varchar(255) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`project\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`project\` ADD \`name\` varchar(255) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`mock\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`mock\` ADD \`name\` varchar(255) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`environment\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`environment\` ADD \`name\` varchar(255) NOT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`environment\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`environment\` ADD \`name\` varchar(500) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`mock\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`mock\` ADD \`name\` varchar(500) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`project\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`project\` ADD \`name\` varchar(500) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`api_group\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`api_group\` ADD \`name\` varchar(500) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`api_data\` DROP COLUMN \`name\``);
await queryRunner.query(`ALTER TABLE \`api_data\` ADD \`name\` varchar(500) NOT NULL`);
await queryRunner.query(`DROP TABLE \`shared\``);
}

}
9 changes: 7 additions & 2 deletions src/modules/auth/guards/jwt-auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ export class JwtAuthGuard implements CanActivate {
throw new UnauthorizedException('您的密码已更新,请重新登录');
}
} catch (e) {
console.log('e', e);
// 无法通过token校验
throw new UnauthorizedException('token已失效,请重新登录');
}
if (isEmpty(request.currentUser)) {
throw new UnauthorizedException('当前用户不存在');
}

const workspaceID = Number(request?.params?.workspaceID);
const projectID = Number(request?.params?.projectID);
const workspaceID = Number(
request?.params?.workspaceID || request.headers['x-workspace-id'],
);
const projectID = Number(
request?.params?.projectID || request.headers['x-project-id'],
);
if (!Number.isNaN(workspaceID)) {
const hasWorkspaceAuth = await this.userService.findOneBy({
id: request.currentUser.userId,
Expand Down
3 changes: 3 additions & 0 deletions src/modules/shared-docs/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './login.dto';
export * from './logout.dto';
export * from './refresh.dto';
20 changes: 20 additions & 0 deletions src/modules/shared-docs/dto/login.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import {
IsMobilePhone,
IsNotEmpty,
IsString,
MinLength,
} from 'class-validator';

export class LoginInfoDto {
@ApiProperty({ description: '用户名' })
@IsString()
@IsNotEmpty({ message: '用户名不能为空' })
username: string;

@ApiProperty({ description: '密码' })
@IsNotEmpty({ message: '密码不能为空' })
@IsString()
@MinLength(6)
password: string;
}
8 changes: 8 additions & 0 deletions src/modules/shared-docs/dto/logout.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class JwtLogoutDto {
@ApiProperty()
@IsString()
refreshToken: string;
}
8 changes: 8 additions & 0 deletions src/modules/shared-docs/dto/refresh.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class JwtRefreshTokenDto {
@ApiProperty()
@IsString()
refreshToken: string;
}
Loading

0 comments on commit d58dcc3

Please sign in to comment.