File size: 2,649 Bytes
b0798af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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();