import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { serverConfig } from './config/config'; import { join } from 'path'; import * as fs from 'fs'; import { NestExpressApplication } from '@nestjs/platform-express'; import session from 'express-session'; import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const certsDir = join(__dirname, '..', 'certs'); const httpsOptions = { key: fs.readFileSync(join(certsDir, 'myapp.local-key.pem')), cert: fs.readFileSync(join(certsDir, 'myapp.local.pem')), }; // Use HTTPS if needed: const app = await NestFactory.create(AppModule, { httpsOptions, }); // HTTP version for LAN testing: // const app = await NestFactory.create(AppModule); app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, }), ); // app.enableCors({ // origin: [ // 'http://localhost:4200', // browser dev // 'capacitor://localhost', // Capacitor WebView // 'http://192.168.100.80:4200', // optional LAN Angular dev // ], // credentials: true, // }); app.enableCors({ origin: (origin, callback) => { // console.log('CORS origin:', origin); // check what the WebView sends callback(null, true); // allow all origins dynamically }, credentials: true, }); app.setGlobalPrefix('api'); const angularDistPath = join( __dirname, '..', '..', 'web-app', 'dist', 'mobile-auth-web-app', 'browser', ); const indexPath = join(angularDistPath, 'index.html'); app.useStaticAssets(angularDistPath); app.setBaseViewsDir(angularDistPath); app.setViewEngine('html'); app.use( session({ secret: 'your-secret', resave: false, saveUninitialized: false, cookie: { httpOnly: true, secure: false, // set to true when using HTTPS sameSite: 'lax', maxAge: 24 * 60 * 60 * 1000, }, }), ); app.use((req, res, next) => { const isStaticAsset = req.url.includes('.'); const isApiCall = req.url.startsWith('/api') || req.method !== 'GET'; if (isStaticAsset || isApiCall) return next(); res.sendFile(indexPath); }); // Listen on all interfaces so LAN devices can reach it await app.listen(4000, '0.0.0.0'); console.log(`🚀 HTTP server running at ${serverConfig.exposedUrl}`); } bootstrap();