import { BadRequestException , Controller , Get , HttpCode , Post , UseInterceptors , } from '@nestjs/common' ; import { ApiTags } from '@nestjs/swagger' ; import { InjectModel } from 'nestjs-typegoose' ; import PKG from '../package.json' ; import { Auth } from './common/decorator/auth.decorator' ; import { HttpCache } from './common/decorator/cache.decorator' ; import { IpLocation , IpRecord } from './common/decorator/ip.decorator' ; import { AllowAllCorsInterceptor } from './common/interceptors/allow-all-cors.interceptor' ; import { RedisKeys } from './constants/cache.constant' ; import { OptionModel } from './modules/configs/configs.model' ; import { CacheService } from './processors/cache/cache.service' ; import { getRedisKey } from './utils/redis.util' ; @ Controller ( ) @ ApiTags ( 'Root' ) export class AppController { constructor ( private readonly cacheService , @ InjectModel ( OptionModel ) private readonly optionModel ) { } @ UseInterceptors ( AllowAllCorsInterceptor ) @ Get ( [ '/' , '/info' ] ) async appInfo ( ) { return { name : PKG . name , author : PKG . author , version : isDev ? 'dev' : PKG . version , homepage : PKG . homepage , issues : PKG . issues , } ; } @ Get ( '/ping' ) @ UseInterceptors ( AllowAllCorsInterceptor ) ping ( ) { return 'pong' ; } @ Post ( '/like_this' ) @ HttpCache . disable @ HttpCode ( 204 ) async likeThis ( @ IpLocation ( ) { ip } ) { const redis = this . cacheService . getClient ( ) ; const isLikedBefore = await redis . sismember ( getRedisKey ( RedisKeys . LikeSite ) , ip ) ; if ( isLikedBefore ) { throw new BadRequestException ( '一天一次就够啦' ) ; } else { redis . sadd ( getRedisKey ( RedisKeys . LikeSite ) , ip ) ; } await this . optionModel . updateOne ( { name : 'like' , } , { $inc : { value : 1 , } , } , { upsert : true } ) ; return ; } @ Get ( '/like_this' ) @ HttpCache . disable async getLikeNumber ( ) { const doc = await this . optionModel . findOne ( { name : 'like' } ) . lean ( ) ; return doc ? doc . value : 0 ; } @ Get ( '/clean_catch' ) @ HttpCache . disable @ Auth ( ) async cleanCatch ( ) { const redis = this . cacheService . getClient ( ) ; const keys = await redis . keys ( 'mx-api-cache:*' ) ; await Promise . all ( keys . map ( ( key ) => redis . del ( key ) ) ) ; return ; } }