Spaces:
Sleeping
Sleeping
Download backend/src/main.ts from kishankrish19/Chat: direct link, hf CLI and curl.
- Browser
- Download file 2.65 kB
-
https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/src/main.ts
- Command line
-
hf download hf://spaces/kishankrish19/Chat/backend/src/main.ts
-
curl -L -o main.ts https://huggingface.co/spaces/kishankrish19/Chat/resolve/main/backend/src/main.ts
2.65 kB
| import 'dotenv/config'; | |
| import { setDefaultResultOrder } from 'node:dns'; | |
| import { setDefaultAutoSelectFamily } from 'node:net'; | |
| import { NestFactory } from '@nestjs/core'; | |
| import { ValidationPipe } from '@nestjs/common'; | |
| import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; | |
| import { raw } from 'express'; | |
| import { AppModule } from './app.module'; | |
| async function bootstrap() { | |
| setDefaultResultOrder('ipv4first'); | |
| if(process.platform==='win32'&&process.env.PREFER_IPV4!=='false')setDefaultAutoSelectFamily(false); | |
| const app = await NestFactory.create(AppModule); | |
| app.enableShutdownHooks(); | |
| app.enableCors({ | |
| origin: | |
| process.env.CORS_ORIGINS && process.env.CORS_ORIGINS !== '*' | |
| ? process.env.CORS_ORIGINS.split(',') | |
| : true, | |
| methods: ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], | |
| allowedHeaders: ['Content-Type', 'Authorization'], | |
| exposedHeaders: ['Content-Range', 'Accept-Ranges'], | |
| }); | |
| app.use( | |
| '/api/v1/uploads', | |
| raw({ type: 'application/octet-stream', limit: '1mb' }), | |
| ); | |
| const buckets = new Map<string, { count: number; expires: number }>(); | |
| app.use((req: any, res: any, next: () => void) => { | |
| res.setHeader('X-Content-Type-Options', 'nosniff'); | |
| res.setHeader('Referrer-Policy', 'no-referrer'); | |
| const auth = req.path.startsWith('/api/v1/auth/'); | |
| if (req.method === 'OPTIONS') return next(); | |
| const key = (auth ? 'auth:' : 'api:') + req.ip, | |
| time = Date.now(); | |
| let bucket = buckets.get(key); | |
| if (!bucket || bucket.expires < time) { | |
| bucket = { count: 0, expires: time + 60000 }; | |
| buckets.set(key, bucket); | |
| } | |
| if (++bucket.count > (auth ? 30 : 3000)) { | |
| res.setHeader('Retry-After', '60'); | |
| res | |
| .status(429) | |
| .json({ message: 'Too many requests. Please wait a moment.' }); | |
| return; | |
| } | |
| if (buckets.size > 10000) | |
| for (const [id, b] of buckets) if (b.expires < time) buckets.delete(id); | |
| next(); | |
| }); | |
| app.setGlobalPrefix('api/v1'); | |
| app.useGlobalPipes( | |
| new ValidationPipe({ | |
| whitelist: true, | |
| transform: true, | |
| forbidNonWhitelisted: true, | |
| }), | |
| ); | |
| const config = new DocumentBuilder() | |
| .setTitle('Stillwave API') | |
| .setDescription( | |
| 'Private music library, encrypted audio and resilient imports', | |
| ) | |
| .setVersion('1.0') | |
| .addBearerAuth() | |
| .build(); | |
| SwaggerModule.setup( | |
| 'api/docs', | |
| app, | |
| SwaggerModule.createDocument(app, config), | |
| ); | |
| await app.listen( | |
| Number(process.env.PORT) || 5000, | |
| process.env.HOST || '127.0.0.1', | |
| ); | |
| console.log('Stillwave API is ready.'); | |
| } | |
| void bootstrap(); | |