File size: 2,142 Bytes
4acc19f
 
 
f3abb0d
4acc19f
 
f3abb0d
801d9e5
 
 
 
 
 
f3abb0d
 
 
 
 
 
 
 
 
 
 
4acc19f
df84e17
 
 
4acc19f
f3abb0d
 
 
 
 
 
 
4acc19f
df84e17
f3abb0d
 
 
df84e17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4acc19f
 
f3abb0d
4acc19f
 
 
 
 
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
import { INestApplication } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { execSync } from 'child_process';
const basicAuth = require('express-basic-auth');

const getBranchName = (): string => {
  // First, try to get from environment variable (for Docker/Hugging Face)
  if (process.env.BRANCH_NAME) {
    return process.env.BRANCH_NAME;
  }

  // Fallback to git command (for local development)
  try {
    return execSync('git rev-parse --abbrev-ref HEAD', {
      encoding: 'utf8',
      cwd: process.cwd(),
    }).trim();
  } catch (error: any) {
    console.warn('Could not get git branch name:', error.message);
    return 'unknown';
  }
};

export const setupSwagger = (app: INestApplication) => {
  const configService = app.get(ConfigService);
  const swaggerPassword = configService.get('SWAGGER_PASSWORD');
  const isApiTokenEnabled = configService.get('API_TOKEN_ENABLED') === 'true';

  app.use(
    ['/api'],
    basicAuth({
      challenge: true,
      users: { admin: swaggerPassword },
    }),
  );

  const configBuilder = new DocumentBuilder()
    .addBearerAuth()
    .setTitle(`StreamFlix API: ${getBranchName()}`)
    .setDescription('The StreamFlix API documentation')
    .setVersion('1.0.0');

  if (isApiTokenEnabled) {
    configBuilder.addApiKey(
      {
        type: 'apiKey',
        name: 'x-api-token',
        in: 'header',
      },
      'x-api-token',
    );
  }

  const config = configBuilder.build();

  const documentOptions = isApiTokenEnabled
    ? {
        operationIdFactory: (controllerKey: string, methodKey: string) =>
          methodKey,
      }
    : undefined;

  const document = SwaggerModule.createDocument(app, config, documentOptions);

  // Apply security globally if API token is enabled
  if (isApiTokenEnabled && document.components?.securitySchemes) {
    document.security = [{ 'x-api-token': [] }];
  }

  SwaggerModule.setup('api', app, document, {
    customCss: '.swagger-ui { background: #E6F3FF; }',
    swaggerOptions: {
      persistAuthorization: true,
    },
  });
};