Commit 22bf467f authored by nanahira's avatar nanahira

a very quick design

parents
Pipeline #3641 canceled with stages
in 4 minutes and 42 seconds
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
stages:
- build
- combine
- deploy
variables:
GIT_DEPTH: "1"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_TEST_ARM_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
CONTAINER_TEST_X86_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build-x86:
stage: build
tags:
- docker
script:
- docker build --pull -t $CONTAINER_TEST_X86_IMAGE .
- docker push $CONTAINER_TEST_X86_IMAGE
build-arm:
stage: build
tags:
- docker-arm
script:
- docker build --pull -t $CONTAINER_TEST_ARM_IMAGE .
- docker push $CONTAINER_TEST_ARM_IMAGE
combine:
stage: combine
tags:
- docker
script:
- docker pull $CONTAINER_TEST_X86_IMAGE
- docker pull $CONTAINER_TEST_ARM_IMAGE
- docker manifest create $CONTAINER_TEST_IMAGE --amend $CONTAINER_TEST_X86_IMAGE --amend $CONTAINER_TEST_ARM_IMAGE
- docker manifest push $CONTAINER_TEST_IMAGE
deploy_latest:
stage: deploy
tags:
- docker
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
- docker push $CONTAINER_RELEASE_IMAGE
only:
- master
deploy_tag:
stage: deploy
tags:
- docker
variables:
CONTAINER_TAG_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker tag $CONTAINER_TEST_IMAGE $CONTAINER_TAG_IMAGE
- docker push $CONTAINER_TAG_IMAGE
only:
- tags
/install-npm.sh
.git*
/output
/dest
/config.yaml
.idea
.dockerignore
Dockerfile
\ No newline at end of file
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:buster-slim
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
WORKDIR /usr/src/app
COPY ./package*.json ./
RUN npm ci
COPY . ./
RUN npm run build
CMD ["npm", "run", "start:prod"]
This diff is collapsed.
# ygopro-release-helper
## Environment
* `DB_HOST` `DB_PORT` `DB_USER` `DB_PASS` `DB_NAME` Database configs.
* `ACCESS_TOKEN` The token for `Authorization` header.
* `PACKAGER_NOTIFY_URL` If specified, it would call ygopro-packager to make a package.
## Installation
```bash
$ npm install
```
## Running the app
```bash
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
```
## License
AGPLv3
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
/* it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
}); */
});
});
#!/bin/bash
npm install --save \
class-validator \
class-transformer \
@nestjs/swagger \
swagger-ui-express \
lodash \
typeorm \
@nestjs/typeorm \
mysql \
reflect-metadata
npm install --save-dev \
@types/lodash \
@types/express
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": ["@nestjs/swagger"]
}
}
This diff is collapsed.
{
"name": "ygopro-release-helper",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^7.5.1",
"@nestjs/core": "^7.5.1",
"@nestjs/platform-express": "^7.5.1",
"@nestjs/swagger": "^4.8.0",
"@nestjs/typeorm": "^7.1.5",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"lodash": "^4.17.21",
"mysql": "^2.18.1",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3",
"swagger-ui-express": "^4.1.6",
"typeorm": "^0.2.34"
},
"devDependencies": {
"@nestjs/cli": "^7.5.1",
"@nestjs/schematics": "^7.1.3",
"@nestjs/testing": "^7.5.1",
"@types/express": "^4.17.12",
"@types/jest": "^26.0.15",
"@types/lodash": "^4.14.170",
"@types/multer": "^1.4.5",
"@types/node": "^14.14.6",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.12.1",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"supertest": "^6.0.0",
"ts-jest": "^26.4.3",
"ts-loader": "^8.0.8",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.0.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
/* it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
}); */
});
});
import {
Body,
Controller,
Get,
Headers,
Post,
Query,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { AppService } from './app.service';
import { Release } from './entities/Release.entity';
import { ApiBody, ApiConsumes } from '@nestjs/swagger';
import { FileUploadDto } from './dto/FileUpload.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import multer from 'multer';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('release')
async getRelease(
@Headers('Authorization') token?: string,
@Query('name') name?: string,
): Promise<Release> {
await this.appService.checkToken(token);
return this.appService.getRelease(name);
}
@Post('release')
@ApiConsumes('multipart/form-data')
@ApiBody({
description: '要上传的文件',
type: FileUploadDto,
})
@UseInterceptors(FileInterceptor('file'))
async postRelease(
@UploadedFile() file: Express.Multer.File,
@Body('name') name: string,
@Headers('Authorization') token?: string,
): Promise<Release> {
await this.appService.checkToken(token);
return this.appService.postRelease(name, file.buffer);
}
}
import { Injectable, Scope, Logger } from '@nestjs/common';
@Injectable()
export class AppLogger extends Logger {}
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AppLogger } from './app.logger';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeormConfig } from './config';
@Module({
imports: [TypeOrmModule.forRoot(typeormConfig())],
controllers: [AppController],
providers: [AppService, AppLogger],
})
export class AppModule {}
import {
BadRequestException,
ForbiddenException,
Injectable,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';
import { AppLogger } from './app.logger';
import { Connection } from 'typeorm';
import { InjectConnection } from '@nestjs/typeorm';
import { Release } from './entities/Release.entity';
import { releaseHelperConfig, ReleaseHelperConfig } from './config';
import { ReleaseFile } from './entities/ReleaseFile.entity';
@Injectable()
export class AppService {
private config: ReleaseHelperConfig;
constructor(
@InjectConnection('app')
private db: Connection,
private log: AppLogger,
) {
this.log.setContext('app');
this.config = releaseHelperConfig;
}
checkToken(token: string) {
if (this.config.accessToken && this.config.accessToken !== token) {
throw new ForbiddenException('incorrect token');
}
}
async notifyPackager(release: Release) {
if (!this.config.packagerUrl) {
return;
}
// todo
}
async getRelease(name?: string) {
const query = this.db
.getRepository(Release)
.createQueryBuilder('release')
.orderBy('release.id', 'DESC')
.take(1)
.leftJoinAndSelect('release.files', 'file');
if (name) {
query.where('name = :name', { name });
}
const release = await query.getOne();
if (!release) {
throw new NotFoundException(`release ${name} not found.`);
}
return release;
}
async postRelease(name: string, content: Buffer) {
if (!name || !content) {
throw new BadRequestException('content missing');
}
const files = content
.toString('utf-8')
.trim()
.split('\n')
.map((fname) => {
const file = new ReleaseFile();
file.filename = fname;
return file;
});
let release = new Release();
release.releaseTime = new Date();
release.name = name;
release.files = files;
try {
release = await this.db.getRepository(Release).save(release);
} catch (e) {
const message = `Failed to save release ${release.name}: ${e.toString()}`;
this.log.error(message);
throw new InternalServerErrorException(message);
}
await this.notifyPackager(release);
return release;
}
}
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ReleaseFile } from './entities/ReleaseFile.entity';
import { Release } from './entities/Release.entity';
export function dbConfig() {
return {
host: process.env.DB_HOST,
port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 3306,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
};
}
export function typeormConfig(): TypeOrmModuleOptions {
return {
name: 'app',
type: 'mysql',
entities: [ReleaseFile, Release], // entities here
synchronize: true,
...dbConfig(),
};
}
export interface ReleaseHelperConfig {
accessToken: string;
packagerUrl: string;
}
export const releaseHelperConfig: ReleaseHelperConfig = {
accessToken: process.env.ACCESS_TOKEN as string,
packagerUrl: process.env.PACKAGER_NOTIFY_URL as string,
};
import { ApiProperty } from '@nestjs/swagger';
export class FileUploadDto {
@ApiProperty({ type: 'string', format: 'binary' })
file: any;
}
import { TimeBase } from './TimeBase.entity';
import { ReleaseFile } from './ReleaseFile.entity';
import {
Column,
Entity,
Index,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
export class Release extends TimeBase {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', length: 32 })
@Index()
name: string;
@Column({ type: 'datetime' })
releaseTime: Date;
@OneToMany(() => ReleaseFile, (f) => f.release, { cascade: true })
files: ReleaseFile[];
}
import { TimeBase } from './TimeBase.entity';
import {
Column,
Entity,
Index,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Release } from './Release.entity';
@Entity()
@Index((r) => [r.filename, r.release], { unique: true })
export class ReleaseFile extends TimeBase {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', length: 128 })
filename: string;
@ManyToOne(() => Release, (r) => r.files)
release: Release;
}
import { CreateDateColumn, UpdateDateColumn } from 'typeorm';
export class TimeBase {
@CreateDateColumn({ select: false })
createTime: Date;
@UpdateDateColumn({ select: false })
updateTime: Date;
toObject() {
return JSON.parse(JSON.stringify(this));
}
}
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
const documentConfig = new DocumentBuilder()
.setTitle('ygopro-release-helper')
.setDescription('YGOPro Release Helper')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, documentConfig);
SwaggerModule.setup('docs', app, document);
await app.listen(3000);
}
bootstrap();
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
/* it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
}); */
});
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"esModuleInterop": true
},
"compileOnSave": true,
"allowJs": true
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment