2024-03-21 15:08:29 +01:00
|
|
|
import { NestFactory } from '@nestjs/core';
|
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
|
|
import { json } from 'body-parser';
|
|
|
|
import cookieParser from 'cookie-parser';
|
|
|
|
import { CommandFactory } from 'nest-commander';
|
|
|
|
import { existsSync } from 'node:fs';
|
2024-05-14 16:28:20 +02:00
|
|
|
import { Worker } from 'node:worker_threads';
|
2024-03-21 15:08:29 +01:00
|
|
|
import sirv from 'sirv';
|
2024-05-14 16:28:20 +02:00
|
|
|
import { ApiModule, ImmichAdminModule } from 'src/app.module';
|
2024-03-21 15:08:29 +01:00
|
|
|
import { WEB_ROOT, envName, excludePaths, isDev, serverVersion } from 'src/constants';
|
|
|
|
import { LogLevel } from 'src/entities/system-config.entity';
|
2024-04-16 01:39:06 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-21 15:08:29 +01:00
|
|
|
import { WebSocketAdapter } from 'src/middleware/websocket.adapter';
|
|
|
|
import { ApiService } from 'src/services/api.service';
|
|
|
|
import { otelSDK } from 'src/utils/instrumentation';
|
|
|
|
import { useSwagger } from 'src/utils/misc';
|
|
|
|
|
2024-04-26 08:02:19 +02:00
|
|
|
const host = process.env.HOST;
|
|
|
|
|
2024-03-21 15:08:29 +01:00
|
|
|
async function bootstrapApi() {
|
2024-04-16 01:39:06 +02:00
|
|
|
otelSDK.start();
|
|
|
|
|
2024-03-21 15:08:29 +01:00
|
|
|
const port = Number(process.env.SERVER_PORT) || 3001;
|
|
|
|
const app = await NestFactory.create<NestExpressApplication>(ApiModule, { bufferLogs: true });
|
2024-04-16 23:30:31 +02:00
|
|
|
const logger = await app.resolve(ILoggerRepository);
|
2024-03-21 15:08:29 +01:00
|
|
|
|
2024-05-14 16:28:20 +02:00
|
|
|
logger.setAppName('ImmichServer');
|
2024-04-16 01:39:06 +02:00
|
|
|
logger.setContext('ImmichServer');
|
|
|
|
app.useLogger(logger);
|
2024-03-21 15:08:29 +01:00
|
|
|
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
|
|
|
|
app.set('etag', 'strong');
|
|
|
|
app.use(cookieParser());
|
|
|
|
app.use(json({ limit: '10mb' }));
|
|
|
|
if (isDev) {
|
|
|
|
app.enableCors();
|
|
|
|
}
|
|
|
|
app.useWebSocketAdapter(new WebSocketAdapter(app));
|
|
|
|
useSwagger(app, isDev);
|
|
|
|
|
|
|
|
app.setGlobalPrefix('api', { exclude: excludePaths });
|
|
|
|
if (existsSync(WEB_ROOT)) {
|
|
|
|
// copied from https://github.com/sveltejs/kit/blob/679b5989fe62e3964b9a73b712d7b41831aa1f07/packages/adapter-node/src/handler.js#L46
|
|
|
|
// provides serving of precompressed assets and caching of immutable assets
|
|
|
|
app.use(
|
|
|
|
sirv(WEB_ROOT, {
|
|
|
|
etag: true,
|
|
|
|
gzip: true,
|
|
|
|
brotli: true,
|
|
|
|
setHeaders: (res, pathname) => {
|
|
|
|
if (pathname.startsWith(`/_app/immutable`) && res.statusCode === 200) {
|
|
|
|
res.setHeader('cache-control', 'public,max-age=31536000,immutable');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
app.use(app.get(ApiService).ssr(excludePaths));
|
|
|
|
|
2024-04-26 08:02:19 +02:00
|
|
|
const server = await (host ? app.listen(port, host) : app.listen(port));
|
2024-03-21 15:08:29 +01:00
|
|
|
server.requestTimeout = 30 * 60 * 1000;
|
|
|
|
|
|
|
|
logger.log(`Immich Server is listening on ${await app.getUrl()} [v${serverVersion}] [${envName}] `);
|
|
|
|
}
|
2023-06-08 17:01:07 +02:00
|
|
|
|
|
|
|
const immichApp = process.argv[2] || process.env.IMMICH_APP;
|
|
|
|
|
|
|
|
if (process.argv[2] === immichApp) {
|
|
|
|
process.argv.splice(2, 1);
|
|
|
|
}
|
|
|
|
|
2024-03-21 15:08:29 +01:00
|
|
|
async function bootstrapImmichAdmin() {
|
|
|
|
process.env.LOG_LEVEL = LogLevel.WARN;
|
|
|
|
await CommandFactory.run(ImmichAdminModule);
|
|
|
|
}
|
|
|
|
|
2024-05-14 16:28:20 +02:00
|
|
|
function bootstrapMicroservicesWorker() {
|
|
|
|
const worker = new Worker('./dist/workers/microservices.js');
|
|
|
|
worker.on('exit', (exitCode) => {
|
|
|
|
if (exitCode !== 0) {
|
|
|
|
console.error(`Microservices worker exited with code ${exitCode}`);
|
|
|
|
process.exit(exitCode);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:01:07 +02:00
|
|
|
function bootstrap() {
|
|
|
|
switch (immichApp) {
|
2024-02-02 04:18:00 +01:00
|
|
|
case 'immich': {
|
2023-08-02 23:03:26 +02:00
|
|
|
process.title = 'immich_server';
|
2024-05-14 16:28:20 +02:00
|
|
|
if (process.env.INTERNAL_MICROSERVICES === 'true') {
|
|
|
|
bootstrapMicroservicesWorker();
|
|
|
|
}
|
2024-03-20 22:22:47 +01:00
|
|
|
return bootstrapApi();
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
case 'microservices': {
|
2023-08-02 23:03:26 +02:00
|
|
|
process.title = 'immich_microservices';
|
2024-05-14 16:28:20 +02:00
|
|
|
return bootstrapMicroservicesWorker();
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
case 'immich-admin': {
|
2023-08-02 23:03:26 +02:00
|
|
|
process.title = 'immich_admin_cli';
|
2024-03-20 22:22:47 +01:00
|
|
|
return bootstrapImmichAdmin();
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
|
|
|
default: {
|
2024-03-21 15:08:29 +01:00
|
|
|
throw new Error(`Invalid app name: ${immichApp}. Expected one of immich|microservices|immich-admin`);
|
2024-02-02 04:18:00 +01:00
|
|
|
}
|
2023-06-08 17:01:07 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-21 15:08:29 +01:00
|
|
|
|
2023-10-13 07:22:40 +02:00
|
|
|
void bootstrap();
|