From c8aac34b48a236ee6ecc23bb2eea600508c2130a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AF=BB?= Date: Tue, 21 Jun 2022 18:10:14 +0800 Subject: [PATCH] refactory: global prefix routes (#583) --- src/app.controller.ts | 4 +-- src/app.module.ts | 2 ++ src/bootstrap.ts | 10 +----- .../decorator/api-controller.decorator.ts | 34 +++++++++++++++++++ src/constants/path.constant.ts | 4 +++ src/global/index.global.ts | 4 ++- src/modules/aggregate/aggregate.controller.ts | 5 +-- src/modules/analyze/analyze.controller.ts | 5 +-- src/modules/auth/auth.controller.ts | 5 ++- src/modules/backup/backup.controller.ts | 4 +-- src/modules/category/category.controller.ts | 4 +-- src/modules/comment/comment.controller.ts | 4 +-- src/modules/debug/debug.controller.ts | 12 ++----- src/modules/feed/feed.controller.ts | 7 ++-- src/modules/file/file.controller.ts | 14 ++------ src/modules/health/health.controller.ts | 4 +-- src/modules/init/init.controller.ts | 4 +-- src/modules/link/link.controller.ts | 7 ++-- src/modules/markdown/markdown.controller.ts | 4 +-- src/modules/note/note.controller.ts | 4 +-- src/modules/option/option.decorator.ts | 11 +++--- src/modules/page/page.controller.ts | 4 +-- src/modules/post/post.controller.ts | 4 +-- src/modules/pty/pty.controller.ts | 5 +-- src/modules/recently/recently.controller.ts | 4 +-- src/modules/render/render.controller.ts | 7 ++++ src/modules/render/render.module.ts | 8 +++++ src/modules/search/search.controller.ts | 11 ++---- .../serverless/serverless.controller.ts | 4 +-- src/modules/sitemap/sitemap.controller.ts | 8 +++-- src/modules/snippet/snippet.controller.ts | 4 +-- src/modules/tool/tool.controller.ts | 5 +-- src/modules/user/user.controller.ts | 5 +-- src/transformers/crud-factor.transformer.ts | 4 +-- 34 files changed, 134 insertions(+), 91 deletions(-) create mode 100644 src/common/decorator/api-controller.decorator.ts create mode 100644 src/modules/render/render.controller.ts create mode 100644 src/modules/render/render.module.ts diff --git a/src/app.controller.ts b/src/app.controller.ts index 7a435c53..de48d8b3 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -1,6 +1,5 @@ import { BadRequestException, - Controller, Get, HttpCode, Post, @@ -8,6 +7,7 @@ import { } from '@nestjs/common' import { ApiTags } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { InjectModel } from '~/transformers/model.transformer' import PKG from '../package.json' @@ -21,7 +21,7 @@ import { OptionModel } from './modules/configs/configs.model' import { CacheService } from './processors/redis/cache.service' import { getRedisKey } from './utils/redis.util' -@Controller() +@ApiController() @ApiTags('Root') export class AppController { constructor( diff --git a/src/app.module.ts b/src/app.module.ts index 312ee678..a92d5f8a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -35,6 +35,7 @@ import { PostModule } from './modules/post/post.module' import { ProjectModule } from './modules/project/project.module' import { PTYModule } from './modules/pty/pty.module' import { RecentlyModule } from './modules/recently/recently.module' +import { RenderEjsModule } from './modules/render/render.module' import { SayModule } from './modules/say/say.module' import { SearchModule } from './modules/search/search.module' import { ServerlessModule } from './modules/serverless/serverless.module' @@ -86,6 +87,7 @@ import { RedisModule } from './processors/redis/redis.module' UserModule, PageProxyModule, + RenderEjsModule, GatewayModule, HelperModule, diff --git a/src/bootstrap.ts b/src/bootstrap.ts index 5dc581e0..bd7f1c71 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -1,7 +1,7 @@ import cluster from 'cluster' import { performance } from 'perf_hooks' -import { LogLevel, Logger, RequestMethod, ValidationPipe } from '@nestjs/common' +import { LogLevel, Logger, ValidationPipe } from '@nestjs/common' import { NestFactory } from '@nestjs/core' import { NestFastifyApplication } from '@nestjs/platform-fastify' @@ -47,14 +47,6 @@ export async function bootstrap() { : undefined, ) - app.setGlobalPrefix(isDev ? '' : `api/v${API_VERSION}`, { - exclude: [ - { path: '/qaqdmin', method: RequestMethod.GET }, - { path: '/proxy/qaqdmin', method: RequestMethod.GET }, - { path: '/proxy/*', method: RequestMethod.GET }, - ], - }) - if (isDev) { app.useGlobalInterceptors(new LoggingInterceptor()) } diff --git a/src/common/decorator/api-controller.decorator.ts b/src/common/decorator/api-controller.decorator.ts new file mode 100644 index 00000000..d18c7b06 --- /dev/null +++ b/src/common/decorator/api-controller.decorator.ts @@ -0,0 +1,34 @@ +import { Controller, ControllerOptions } from '@nestjs/common' + +import { API_VERSION } from '~/app.config' + +export const apiRoutePrefix = isDev ? '' : `/api/v${API_VERSION}` +export const ApiController: ( + optionOrString?: string | string[] | undefined | ControllerOptions, +) => ReturnType = (...rest) => { + const [controller, ...args] = rest + if (!controller) { + return Controller(apiRoutePrefix) + } + + const transformPath = (path: string) => + `${apiRoutePrefix}/${path.replace(/\//, '')}` + + if (typeof controller === 'string') { + return Controller(transformPath(controller), ...args) + } else if (Array.isArray(controller)) { + return Controller( + controller.map((path) => transformPath(path)), + ...args, + ) + } else { + const path = controller.path || '' + + return Controller( + Array.isArray(path) + ? path.map((i) => transformPath(i)) + : transformPath(path), + ...args, + ) + } +} diff --git a/src/constants/path.constant.ts b/src/constants/path.constant.ts index 20b34cf8..e2b6f42e 100644 --- a/src/constants/path.constant.ts +++ b/src/constants/path.constant.ts @@ -9,6 +9,10 @@ export const TEMP_DIR = isDev ? join(cwd, './tmp') : '/tmp/mx-space' export const DATA_DIR = isDev ? join(cwd, './tmp') : join(HOME, '.mx-space') +export const THEME_DIR = isDev + ? join(cwd, './tmp/theme') + : join(DATA_DIR, 'theme') + export const USER_ASSET_DIR = join(DATA_DIR, 'assets') export const LOG_DIR = join(DATA_DIR, 'log') diff --git a/src/global/index.global.ts b/src/global/index.global.ts index 84d6869c..2496e227 100644 --- a/src/global/index.global.ts +++ b/src/global/index.global.ts @@ -12,6 +12,7 @@ import { LOG_DIR, STATIC_FILE_DIR, TEMP_DIR, + THEME_DIR, USER_ASSET_DIR, } from '~/constants/path.constant' @@ -33,9 +34,10 @@ function mkdirs() { Logger.log(chalk.blue(`日志目录已经建好: ${LOG_DIR}`)) mkdirSync(USER_ASSET_DIR, { recursive: true }) Logger.log(chalk.blue(`资源目录已经建好: ${USER_ASSET_DIR}`)) - mkdirSync(STATIC_FILE_DIR, { recursive: true }) Logger.log(chalk.blue(`文件存放目录已经建好: ${STATIC_FILE_DIR}`)) + mkdirSync(THEME_DIR, { recursive: true }) + Logger.log(chalk.blue(`主题目录已经建好: ${THEME_DIR}`)) } } diff --git a/src/modules/aggregate/aggregate.controller.ts b/src/modules/aggregate/aggregate.controller.ts index 73e12615..a0f33761 100644 --- a/src/modules/aggregate/aggregate.controller.ts +++ b/src/modules/aggregate/aggregate.controller.ts @@ -1,8 +1,9 @@ import { omit } from 'lodash' -import { CacheKey, CacheTTL, Controller, Get, Query } from '@nestjs/common' +import { CacheKey, CacheTTL, Get, Query } from '@nestjs/common' import { ApiProperty } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { IsMaster } from '~/common/decorator/role.decorator' @@ -13,7 +14,7 @@ import { ConfigsService } from '../configs/configs.service' import { TimelineQueryDto, TopQueryDto } from './aggregate.dto' import { AggregateService } from './aggregate.service' -@Controller('aggregate') +@ApiController('aggregate') @ApiName export class AggregateController { constructor( diff --git a/src/modules/analyze/analyze.controller.ts b/src/modules/analyze/analyze.controller.ts index 8bfa2dd8..e1891a18 100644 --- a/src/modules/analyze/analyze.controller.ts +++ b/src/modules/analyze/analyze.controller.ts @@ -1,7 +1,8 @@ import dayjs from 'dayjs' -import { Controller, Delete, Get, HttpCode, Query } from '@nestjs/common' +import { Delete, Get, HttpCode, Query } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { Paginator } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -14,7 +15,7 @@ import { getTodayEarly, getWeekStart } from '~/utils/time.util' import { AnalyzeDto } from './analyze.dto' import { AnalyzeService } from './analyze.service' -@Controller({ path: 'analyze' }) +@ApiController({ path: 'analyze' }) @ApiName @Auth() export class AnalyzeController { diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index e8d0e54f..bee8f206 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -9,7 +9,6 @@ import { import { Body, - Controller, Delete, Get, NotFoundException, @@ -19,6 +18,7 @@ import { import { EventEmitter2 } from '@nestjs/event-emitter' import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { IsMaster as Master } from '~/common/decorator/role.decorator' @@ -37,8 +37,7 @@ export class TokenDto { @IsNotEmpty() name: string } - -@Controller({ +@ApiController({ path: 'auth', }) @ApiName diff --git a/src/modules/backup/backup.controller.ts b/src/modules/backup/backup.controller.ts index 2826f198..6099a690 100644 --- a/src/modules/backup/backup.controller.ts +++ b/src/modules/backup/backup.controller.ts @@ -3,7 +3,6 @@ import { Readable } from 'stream' import { BadRequestException, - Controller, Delete, Get, Header, @@ -16,6 +15,7 @@ import { } from '@nestjs/common' import { ApiProperty, ApiResponseProperty } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { BanInDemo } from '~/common/decorator/demo.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' @@ -25,7 +25,7 @@ import { getMediumDateTime } from '~/utils' import { BackupService } from './backup.service' -@Controller({ path: 'backups' }) +@ApiController({ path: 'backups' }) @ApiName @Auth() @BanInDemo diff --git a/src/modules/category/category.controller.ts b/src/modules/category/category.controller.ts index 11cb3148..f16054d5 100644 --- a/src/modules/category/category.controller.ts +++ b/src/modules/category/category.controller.ts @@ -3,7 +3,6 @@ import { isValidObjectId } from 'mongoose' import { BadRequestException, Body, - Controller, Delete, Get, HttpCode, @@ -17,6 +16,7 @@ import { } from '@nestjs/common' import { ApiQuery } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -36,7 +36,7 @@ import { } from './category.model' import { CategoryService } from './category.service' -@Controller({ path: 'categories' }) +@ApiController({ path: 'categories' }) @ApiName export class CategoryController { constructor( diff --git a/src/modules/comment/comment.controller.ts b/src/modules/comment/comment.controller.ts index b40f877d..a09b3e73 100644 --- a/src/modules/comment/comment.controller.ts +++ b/src/modules/comment/comment.controller.ts @@ -2,7 +2,6 @@ import { isUndefined } from 'lodash' import { Body, - Controller, Delete, ForbiddenException, Get, @@ -16,6 +15,7 @@ import { import { ApiOperation, ApiParam } from '@nestjs/swagger' import { DocumentType } from '@typegoose/typegoose' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { CurrentUser } from '~/common/decorator/current-user.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' @@ -43,7 +43,7 @@ import { CommentService } from './comment.service' const idempotenceMessage = '哦吼,这句话你已经说过啦' -@Controller({ path: 'comments' }) +@ApiController({ path: 'comments' }) @UseInterceptors(CommentFilterEmailInterceptor) @ApiName export class CommentController { diff --git a/src/modules/debug/debug.controller.ts b/src/modules/debug/debug.controller.ts index 753bec76..1a6b1ee9 100644 --- a/src/modules/debug/debug.controller.ts +++ b/src/modules/debug/debug.controller.ts @@ -1,12 +1,6 @@ -import { - Body, - Controller, - Post, - Query, - Request, - Response, -} from '@nestjs/common' +import { Body, Post, Query, Request, Response } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { BusinessEvents, EventScope } from '~/constants/business-event.constant' @@ -17,7 +11,7 @@ import { ServerlessService } from '../serverless/serverless.service' import { SnippetModel, SnippetType } from '../snippet/snippet.model' @ApiName -@Controller('debug') +@ApiController('debug') export class DebugController { constructor( private readonly serverlessService: ServerlessService, diff --git a/src/modules/feed/feed.controller.ts b/src/modules/feed/feed.controller.ts index 71c42543..3396c5c1 100644 --- a/src/modules/feed/feed.controller.ts +++ b/src/modules/feed/feed.controller.ts @@ -1,7 +1,9 @@ +import { uniq } from 'lodash' import xss from 'xss' import { CacheKey, CacheTTL, Controller, Get, Header } from '@nestjs/common' +import { apiRoutePrefix } from '~/common/decorator/api-controller.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { CacheKeys } from '~/constants/cache.constant' @@ -10,7 +12,7 @@ import { AggregateService } from '../aggregate/aggregate.service' import { ConfigsService } from '../configs/configs.service' import { MarkdownService } from '../markdown/markdown.service' -@Controller('feed') +@Controller() @ApiName export class FeedController { constructor( @@ -19,7 +21,8 @@ export class FeedController { private readonly markdownService: MarkdownService, ) {} - @Get('/') + // TODO drop 兼容老版本 + @Get(uniq(['/feed', `${apiRoutePrefix}/feed`, '/atom.xml'])) @CacheKey(CacheKeys.RSSXmlCatch) @CacheTTL(3600) @HTTPDecorators.Bypass diff --git a/src/modules/file/file.controller.ts b/src/modules/file/file.controller.ts index 19993863..cb4b7e6d 100644 --- a/src/modules/file/file.controller.ts +++ b/src/modules/file/file.controller.ts @@ -2,18 +2,10 @@ import { FastifyReply, FastifyRequest } from 'fastify' import { lookup } from 'mime-types' import { customAlphabet } from 'nanoid/async' -import { - Controller, - Delete, - Get, - Param, - Post, - Query, - Req, - Res, -} from '@nestjs/common' +import { Delete, Get, Param, Post, Query, Req, Res } from '@nestjs/common' import { Throttle } from '@nestjs/throttler' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -26,7 +18,7 @@ import { FileQueryDto, FileUploadDto } from './file.dto' import { FileService } from './file.service' @ApiName -@Controller(['objects', 'files']) +@ApiController(['objects', 'files']) export class FileController { constructor( private readonly service: FileService, diff --git a/src/modules/health/health.controller.ts b/src/modules/health/health.controller.ts index 5e3eebe2..44ea1b30 100644 --- a/src/modules/health/health.controller.ts +++ b/src/modules/health/health.controller.ts @@ -5,7 +5,6 @@ import { Readable } from 'stream' import { BadRequestException, - Controller, Delete, Get, Param, @@ -17,6 +16,7 @@ import { import { Reflector } from '@nestjs/core' import { SchedulerRegistry } from '@nestjs/schedule' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { BanInDemo } from '~/common/decorator/demo.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' @@ -31,7 +31,7 @@ import { formatByteSize } from '~/utils' import { LogQueryDto, LogTypeDto } from './health.dto' -@Controller({ +@ApiController({ path: 'health', }) @Auth() diff --git a/src/modules/init/init.controller.ts b/src/modules/init/init.controller.ts index 060e47ad..e3c89eb5 100644 --- a/src/modules/init/init.controller.ts +++ b/src/modules/init/init.controller.ts @@ -1,7 +1,6 @@ import { BadRequestException, Body, - Controller, ForbiddenException, Get, Param, @@ -10,13 +9,14 @@ import { UnprocessableEntityException, } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { ConfigsService } from '../configs/configs.service' import { ConfigKeyDto } from '../option/dtos/config.dto' import { InitService } from './init.service' -@Controller({ +@ApiController({ path: '/init', scope: Scope.REQUEST, }) diff --git a/src/modules/link/link.controller.ts b/src/modules/link/link.controller.ts index c1fe8e37..2a1c7630 100644 --- a/src/modules/link/link.controller.ts +++ b/src/modules/link/link.controller.ts @@ -2,7 +2,6 @@ import mongoose from 'mongoose' import { Body, - Controller, ForbiddenException, Get, Param, @@ -11,6 +10,7 @@ import { Query, } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators, Paginator } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -26,7 +26,7 @@ import { LinkModel, LinkState } from './link.model' import { LinkService } from './link.service' const paths = ['links', 'friends'] -@Controller(paths) +@ApiController(paths) @ApiName export class LinkControllerCrud extends BaseCrudFactory({ model: LinkModel, @@ -57,8 +57,7 @@ export class LinkControllerCrud extends BaseCrudFactory({ return await this._model.find(condition).sort({ created: -1 }).lean() } } - -@Controller(paths) +@ApiController(paths) @ApiName export class LinkController { constructor(private readonly linkService: LinkService) {} diff --git a/src/modules/markdown/markdown.controller.ts b/src/modules/markdown/markdown.controller.ts index 7d116f92..eb78571b 100644 --- a/src/modules/markdown/markdown.controller.ts +++ b/src/modules/markdown/markdown.controller.ts @@ -12,7 +12,6 @@ import xss from 'xss' import { Body, CacheTTL, - Controller, ForbiddenException, Get, Header, @@ -22,6 +21,7 @@ import { } from '@nestjs/common' import { ApiProperty } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HttpCache } from '~/common/decorator/cache.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' @@ -44,7 +44,7 @@ import { import { MarkdownYAMLProperty } from './markdown.interface' import { MarkdownService } from './markdown.service' -@Controller('markdown') +@ApiController('markdown') @ApiName export class MarkdownController { constructor( diff --git a/src/modules/note/note.controller.ts b/src/modules/note/note.controller.ts index 6ec94d97..075ec57e 100644 --- a/src/modules/note/note.controller.ts +++ b/src/modules/note/note.controller.ts @@ -3,7 +3,6 @@ import { FilterQuery } from 'mongoose' import { BadRequestException, Body, - Controller, Delete, ForbiddenException, Get, @@ -15,6 +14,7 @@ import { } from '@nestjs/common' import { ApiOperation } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators, Paginator } from '~/common/decorator/http.decorator' import { IpLocation, IpRecord } from '~/common/decorator/ip.decorator' @@ -41,7 +41,7 @@ import { NoteModel, PartialNoteModel } from './note.model' import { NoteService } from './note.service' @ApiName -@Controller({ path: 'notes' }) +@ApiController({ path: 'notes' }) export class NoteController { constructor( private readonly noteService: NoteService, diff --git a/src/modules/option/option.decorator.ts b/src/modules/option/option.decorator.ts index 26e8091c..936bf8f7 100644 --- a/src/modules/option/option.decorator.ts +++ b/src/modules/option/option.decorator.ts @@ -1,14 +1,17 @@ -import { Controller, applyDecorators } from '@nestjs/common' +import { applyDecorators } from '@nestjs/common' import { ApiTags } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' export function OptionController(name?: string, postfixRoute?: string) { - const routes = ['options', 'config', 'setting', 'configs', 'option'] + const routes = ['options', 'config'] return applyDecorators( Auth(), - Controller( - postfixRoute ? routes.map((route) => `${route}/${postfixRoute}`) : routes, + ApiController( + postfixRoute + ? routes.map((route) => `/${route}/${postfixRoute}`) + : routes, ), ApiTags(`${name ? `${name} ` : ''}Option Routes`), ) diff --git a/src/modules/page/page.controller.ts b/src/modules/page/page.controller.ts index c088e258..1337cca2 100644 --- a/src/modules/page/page.controller.ts +++ b/src/modules/page/page.controller.ts @@ -1,6 +1,5 @@ import { Body, - Controller, Delete, Get, Param, @@ -11,6 +10,7 @@ import { UnprocessableEntityException, } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators, Paginator } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -22,7 +22,7 @@ import { PagerDto } from '~/shared/dto/pager.dto' import { PageModel, PartialPageModel } from './page.model' import { PageService } from './page.service' -@Controller('pages') +@ApiController('pages') @ApiName export class PageController { constructor( diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index b8b62919..7719d0a4 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -3,7 +3,6 @@ import { Types } from 'mongoose' import { BadRequestException, Body, - Controller, Delete, Get, HttpCode, @@ -16,6 +15,7 @@ import { } from '@nestjs/common' import { ApiOperation } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators, Paginator } from '~/common/decorator/http.decorator' import { IpLocation, IpRecord } from '~/common/decorator/ip.decorator' @@ -32,7 +32,7 @@ import { CategoryAndSlugDto } from './post.dto' import { PartialPostModel, PostModel } from './post.model' import { PostService } from './post.service' -@Controller('posts') +@ApiController('posts') @ApiName export class PostController { constructor( diff --git a/src/modules/pty/pty.controller.ts b/src/modules/pty/pty.controller.ts index 22290b36..2fb2ab4e 100644 --- a/src/modules/pty/pty.controller.ts +++ b/src/modules/pty/pty.controller.ts @@ -1,5 +1,6 @@ -import { Controller, Get } from '@nestjs/common' +import { Get } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -7,7 +8,7 @@ import { PTYService } from './pty.service' @ApiName @Auth() -@Controller({ path: 'pty' }) +@ApiController({ path: 'pty' }) export class PTYController { constructor(private readonly service: PTYService) {} diff --git a/src/modules/recently/recently.controller.ts b/src/modules/recently/recently.controller.ts index ad368da7..0585ba84 100644 --- a/src/modules/recently/recently.controller.ts +++ b/src/modules/recently/recently.controller.ts @@ -1,7 +1,6 @@ import { BadRequestException, Body, - Controller, Delete, Get, Param, @@ -9,6 +8,7 @@ import { Query, } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -18,7 +18,7 @@ import { OffsetDto } from '~/shared/dto/pager.dto' import { RecentlyModel } from './recently.model' import { RecentlyService } from './recently.service' -@Controller(['recently', 'shorthand']) +@ApiController(['recently', 'shorthand']) @ApiName export class RecentlyController { constructor(private readonly recentlyService: RecentlyService) {} diff --git a/src/modules/render/render.controller.ts b/src/modules/render/render.controller.ts new file mode 100644 index 00000000..bd98924f --- /dev/null +++ b/src/modules/render/render.controller.ts @@ -0,0 +1,7 @@ +import { Controller } from '@nestjs/common' + +import { ApiName } from '~/common/decorator/openapi.decorator' + +@ApiName +@Controller('/render') +export class RenderEjsController {} diff --git a/src/modules/render/render.module.ts b/src/modules/render/render.module.ts new file mode 100644 index 00000000..76ad12fb --- /dev/null +++ b/src/modules/render/render.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common' + +import { RenderEjsController } from './render.controller' + +@Module({ + controllers: [RenderEjsController], +}) +export class RenderEjsModule {} diff --git a/src/modules/search/search.controller.ts b/src/modules/search/search.controller.ts index aaa4ca39..ea091135 100644 --- a/src/modules/search/search.controller.ts +++ b/src/modules/search/search.controller.ts @@ -1,11 +1,6 @@ -import { - BadRequestException, - Controller, - Get, - Param, - Query, -} from '@nestjs/common' +import { BadRequestException, Get, Param, Query } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { HttpCache } from '~/common/decorator/cache.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { IsMaster } from '~/common/decorator/role.decorator' @@ -13,7 +8,7 @@ import { SearchDto } from '~/modules/search/search.dto' import { SearchService } from './search.service' -@Controller('search') +@ApiController('search') @ApiName export class SearchController { constructor(private readonly searchService: SearchService) {} diff --git a/src/modules/serverless/serverless.controller.ts b/src/modules/serverless/serverless.controller.ts index 2684be8e..82692626 100644 --- a/src/modules/serverless/serverless.controller.ts +++ b/src/modules/serverless/serverless.controller.ts @@ -2,7 +2,6 @@ import { FastifyReply, FastifyRequest } from 'fastify' import { CacheTTL, - Controller, ForbiddenException, Get, InternalServerErrorException, @@ -12,6 +11,7 @@ import { Response, } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -23,7 +23,7 @@ import { ServerlessReferenceDto } from './serverless.dto' import { ServerlessService } from './serverless.service' @ApiName -@Controller(['serverless', 'fn']) +@ApiController(['serverless', 'fn']) export class ServerlessController { constructor(private readonly serverlessService: ServerlessService) {} diff --git a/src/modules/sitemap/sitemap.controller.ts b/src/modules/sitemap/sitemap.controller.ts index e049bd40..6f7d5720 100644 --- a/src/modules/sitemap/sitemap.controller.ts +++ b/src/modules/sitemap/sitemap.controller.ts @@ -1,17 +1,21 @@ +import { uniq } from 'lodash' + import { CacheKey, CacheTTL, Controller, Get, Header } from '@nestjs/common' +import { apiRoutePrefix } from '~/common/decorator/api-controller.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' import { CacheKeys } from '~/constants/cache.constant' import { AggregateService } from '../aggregate/aggregate.service' -@Controller('sitemap') +@Controller() @ApiName export class SitemapController { constructor(private readonly aggregateService: AggregateService) {} - @Get('/') + // TODO drop 兼容老版本 + @Get(uniq(['/sitemap', `${apiRoutePrefix}/sitemap`])) @CacheTTL(3600) @CacheKey(CacheKeys.SiteMapXmlCatch) @HTTPDecorators.Bypass diff --git a/src/modules/snippet/snippet.controller.ts b/src/modules/snippet/snippet.controller.ts index 8407b217..60acbe3b 100644 --- a/src/modules/snippet/snippet.controller.ts +++ b/src/modules/snippet/snippet.controller.ts @@ -1,6 +1,5 @@ import { Body, - Controller, Delete, ForbiddenException, Get, @@ -10,6 +9,7 @@ import { Query, } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { BanInDemo } from '~/common/decorator/demo.decorator' import { HTTPDecorators } from '~/common/decorator/http.decorator' @@ -26,7 +26,7 @@ import { SnippetModel, SnippetType } from './snippet.model' import { SnippetService } from './snippet.service' @ApiName -@Controller('snippets') +@ApiController('snippets') export class SnippetController { constructor(private readonly snippetService: SnippetService) {} diff --git a/src/modules/tool/tool.controller.ts b/src/modules/tool/tool.controller.ts index 6dbcf86a..e185652d 100644 --- a/src/modules/tool/tool.controller.ts +++ b/src/modules/tool/tool.controller.ts @@ -1,5 +1,6 @@ -import { CacheTTL, Controller, Get, Param, Query } from '@nestjs/common' +import { CacheTTL, Get, Param, Query } from '@nestjs/common' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HttpCache } from '~/common/decorator/cache.decorator' import { ApiName } from '~/common/decorator/openapi.decorator' @@ -11,7 +12,7 @@ import { ConfigsService } from '../configs/configs.service' import { GaodeMapLocationDto, GaodeMapSearchDto, IpDto } from './tool.dto' import { ToolService } from './tool.service' -@Controller('tools') +@ApiController('tools') @ApiName @Auth() export class ToolController { diff --git a/src/modules/user/user.controller.ts b/src/modules/user/user.controller.ts index 68494304..bcd28a03 100644 --- a/src/modules/user/user.controller.ts +++ b/src/modules/user/user.controller.ts @@ -1,6 +1,7 @@ -import { Body, Controller, Get, HttpCode, Patch, Post } from '@nestjs/common' +import { Body, Get, HttpCode, Patch, Post } from '@nestjs/common' import { ApiOperation } from '@nestjs/swagger' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HttpCache } from '~/common/decorator/cache.decorator' import { @@ -19,7 +20,7 @@ import { UserDocument, UserModel } from './user.model' import { UserService } from './user.service' @ApiName -@Controller(['master', 'user']) +@ApiController(['master', 'user']) export class UserController { constructor( private readonly userService: UserService, diff --git a/src/transformers/crud-factor.transformer.ts b/src/transformers/crud-factor.transformer.ts index 1b5eb98c..1d8a2d1f 100644 --- a/src/transformers/crud-factor.transformer.ts +++ b/src/transformers/crud-factor.transformer.ts @@ -2,7 +2,6 @@ import pluralize from 'pluralize' import { Body, - Controller, Delete, Get, HttpCode, @@ -16,6 +15,7 @@ import { import { ApiTags, PartialType } from '@nestjs/swagger' import { AnyParamConstructor } from '@typegoose/typegoose/lib/types' +import { ApiController } from '~/common/decorator/api-controller.decorator' import { Auth } from '~/common/decorator/auth.decorator' import { HTTPDecorators, Paginator } from '~/common/decorator/http.decorator' import { EventScope } from '~/constants/business-event.constant' @@ -47,7 +47,7 @@ export function BaseCrudFactory< const Upper = classUpper || class {} @ApiTags(`${tagPrefix} Routes`) - @Controller(pluralizeName) + @ApiController(pluralizeName) class BaseCrud extends Upper { constructor( @InjectModel(model) private readonly _model: MongooseModel,