Skip to content

Commit

Permalink
refactor(server): resource paths (immich-app#13194)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 authored Oct 4, 2024
1 parent 5d0a4bb commit 6bbaba7
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 29 deletions.
23 changes: 0 additions & 23 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Duration } from 'luxon';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { SemVer } from 'semver';

export const POSTGRES_VERSION_RANGE = '>=14.0.0';
Expand All @@ -26,28 +25,6 @@ export const DEFAULT_EXTERNAL_DOMAIN = 'http://localhost:' + HOST_SERVER_PORT;

export const citiesFile = 'cities500.txt';

const buildFolder = process.env.IMMICH_BUILD_DATA || '/build';

const folders = {
geodata: join(buildFolder, 'geodata'),
web: join(buildFolder, 'www'),
};

export const resourcePaths = {
lockFile: join(buildFolder, 'build-lock.json'),
geodata: {
dateFile: join(folders.geodata, 'geodata-date.txt'),
admin1: join(folders.geodata, 'admin1CodesASCII.txt'),
admin2: join(folders.geodata, 'admin2Codes.txt'),
cities500: join(folders.geodata, citiesFile),
naturalEarthCountriesPath: join(folders.geodata, 'ne_10m_admin_0_countries.geojson'),
},
web: {
root: folders.web,
indexHtml: join(folders.web, 'index.html'),
},
};

export const MOBILE_REDIRECT = 'app.immich:///oauth-callback';
export const LOGIN_URL = '/auth/login?autoLaunch=0';

Expand Down
15 changes: 15 additions & 0 deletions server/src/interfaces/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ export interface EnvData {
server: string;
};

resourcePaths: {
lockFile: string;
geodata: {
dateFile: string;
admin1: string;
admin2: string;
cities500: string;
naturalEarthCountriesPath: string;
};
web: {
root: string;
indexHtml: string;
};
};

storage: {
ignoreMountCheckErrors: boolean;
};
Expand Down
22 changes: 22 additions & 0 deletions server/src/repositories/config.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Injectable } from '@nestjs/common';
import { join } from 'node:path';
import { citiesFile } from 'src/constants';
import { ImmichEnvironment, ImmichWorker, LogLevel } from 'src/enum';
import { EnvData, IConfigRepository } from 'src/interfaces/config.interface';
import { DatabaseExtension } from 'src/interfaces/database.interface';
Expand Down Expand Up @@ -41,6 +43,11 @@ export class ConfigRepository implements IConfigRepository {

const environment = process.env.IMMICH_ENV as ImmichEnvironment;
const isProd = environment === ImmichEnvironment.PRODUCTION;
const buildFolder = process.env.IMMICH_BUILD_DATA || '/build';
const folders = {
geodata: join(buildFolder, 'geodata'),
web: join(buildFolder, 'www'),
};

return {
port: Number(process.env.IMMICH_PORT) || 3001,
Expand Down Expand Up @@ -79,6 +86,21 @@ export class ConfigRepository implements IConfigRepository {

licensePublicKey: isProd ? productionKeys : stagingKeys,

resourcePaths: {
lockFile: join(buildFolder, 'build-lock.json'),
geodata: {
dateFile: join(folders.geodata, 'geodata-date.txt'),
admin1: join(folders.geodata, 'admin1CodesASCII.txt'),
admin2: join(folders.geodata, 'admin2Codes.txt'),
cities500: join(folders.geodata, citiesFile),
naturalEarthCountriesPath: join(folders.geodata, 'ne_10m_admin_0_countries.geojson'),
},
web: {
root: folders.web,
indexHtml: join(folders.web, 'index.html'),
},
},

storage: {
ignoreMountCheckErrors: process.env.IMMICH_IGNORE_MOUNT_CHECK_ERRORS === 'true',
},
Expand Down
9 changes: 8 additions & 1 deletion server/src/repositories/map.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { getName } from 'i18n-iso-countries';
import { createReadStream, existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import readLine from 'node:readline';
import { citiesFile, resourcePaths } from 'src/constants';
import { citiesFile } from 'src/constants';
import { AssetEntity } from 'src/entities/asset.entity';
import { GeodataPlacesEntity } from 'src/entities/geodata-places.entity';
import { NaturalEarthCountriesEntity } from 'src/entities/natural-earth-countries.entity';
import { SystemMetadataKey } from 'src/enum';
import { IConfigRepository } from 'src/interfaces/config.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import {
GeoPoint,
Expand All @@ -32,6 +33,7 @@ export class MapRepository implements IMapRepository {
@InjectRepository(NaturalEarthCountriesEntity)
private naturalEarthCountriesRepository: Repository<NaturalEarthCountriesEntity>,
@InjectDataSource() private dataSource: DataSource,
@Inject(IConfigRepository) private configRepository: IConfigRepository,
@Inject(ISystemMetadataRepository) private metadataRepository: ISystemMetadataRepository,
@Inject(ILoggerRepository) private logger: ILoggerRepository,
) {
Expand All @@ -40,6 +42,7 @@ export class MapRepository implements IMapRepository {

async init(): Promise<void> {
this.logger.log('Initializing metadata repository');
const { resourcePaths } = this.configRepository.getEnv();
const geodataDate = await readFile(resourcePaths.geodata.dateFile, 'utf8');

// TODO move to service init
Expand Down Expand Up @@ -181,6 +184,8 @@ export class MapRepository implements IMapRepository {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();

const { resourcePaths } = this.configRepository.getEnv();

try {
await queryRunner.startTransaction();
await queryRunner.manager.clear(NaturalEarthCountriesEntity);
Expand Down Expand Up @@ -225,6 +230,7 @@ export class MapRepository implements IMapRepository {
const queryRunner = this.dataSource.createQueryRunner();
await queryRunner.connect();

const { resourcePaths } = this.configRepository.getEnv();
const admin1 = await this.loadAdmin(resourcePaths.geodata.admin1);
const admin2 = await this.loadAdmin(resourcePaths.geodata.admin2);

Expand Down Expand Up @@ -280,6 +286,7 @@ export class MapRepository implements IMapRepository {
admin1Map: Map<string, string>,
admin2Map: Map<string, string>,
) {
const { resourcePaths } = this.configRepository.getEnv();
await this.loadGeodataToTableFromFile(
queryRunner,
(lineSplit: string[]) =>
Expand Down
3 changes: 1 addition & 2 deletions server/src/repositories/server-info.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { exec as execCallback } from 'node:child_process';
import { readFile } from 'node:fs/promises';
import { promisify } from 'node:util';
import sharp from 'sharp';
import { resourcePaths } from 'src/constants';
import { IConfigRepository } from 'src/interfaces/config.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { GitHubRelease, IServerInfoRepository, ServerBuildVersions } from 'src/interfaces/server-info.interface';
Expand Down Expand Up @@ -60,7 +59,7 @@ export class ServerInfoRepository implements IServerInfoRepository {
}

async getBuildVersions(): Promise<ServerBuildVersions> {
const { nodeVersion } = this.configRepository.getEnv();
const { nodeVersion, resourcePaths } = this.configRepository.getEnv();

const [nodejsOutput, ffmpegOutput, magickOutput] = await Promise.all([
maybeFirstLine('node --version'),
Expand Down
6 changes: 5 additions & 1 deletion server/src/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Inject, Injectable } from '@nestjs/common';
import { Cron, CronExpression, Interval } from '@nestjs/schedule';
import { NextFunction, Request, Response } from 'express';
import { readFileSync } from 'node:fs';
import { ONE_HOUR, resourcePaths } from 'src/constants';
import { ONE_HOUR } from 'src/constants';
import { IConfigRepository } from 'src/interfaces/config.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { AuthService } from 'src/services/auth.service';
import { JobService } from 'src/services/job.service';
Expand Down Expand Up @@ -37,6 +38,7 @@ export class ApiService {
private jobService: JobService,
private sharedLinkService: SharedLinkService,
private versionService: VersionService,
@Inject(IConfigRepository) private configRepository: IConfigRepository,
@Inject(ILoggerRepository) private logger: ILoggerRepository,
) {
this.logger.setContext(ApiService.name);
Expand All @@ -53,6 +55,8 @@ export class ApiService {
}

ssr(excludePaths: string[]) {
const { resourcePaths } = this.configRepository.getEnv();

let index = '';
try {
index = readFileSync(resourcePaths.web.indexHtml).toString();
Expand Down
4 changes: 2 additions & 2 deletions server/src/workers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cookieParser from 'cookie-parser';
import { existsSync } from 'node:fs';
import sirv from 'sirv';
import { ApiModule } from 'src/app.module';
import { excludePaths, resourcePaths, serverVersion } from 'src/constants';
import { excludePaths, serverVersion } from 'src/constants';
import { ImmichEnvironment } from 'src/enum';
import { IConfigRepository } from 'src/interfaces/config.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
Expand Down Expand Up @@ -36,7 +36,7 @@ async function bootstrap() {
const logger = await app.resolve<ILoggerRepository>(ILoggerRepository);
const configRepository = app.get<IConfigRepository>(IConfigRepository);

const { environment, port } = configRepository.getEnv();
const { environment, port, resourcePaths } = configRepository.getEnv();
const isDev = environment === ImmichEnvironment.DEVELOPMENT;

logger.setContext('Bootstrap');
Expand Down
15 changes: 15 additions & 0 deletions server/test/repositories/config.repository.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ const envData: EnvData = {
server: 'server-public-key',
},

resourcePaths: {
lockFile: 'build-lock.json',
geodata: {
dateFile: '/build/geodata/geodata-date.txt',
admin1: '/build/geodata/admin1CodesASCII.txt',
admin2: '/build/geodata/admin2Codes.txt',
cities500: '/build/geodata/cities500.txt',
naturalEarthCountriesPath: 'build/ne_10m_admin_0_countries.geojson',
},
web: {
root: '/build/www',
indexHtml: '/build/www/index.html',
},
},

storage: {
ignoreMountCheckErrors: false,
},
Expand Down

0 comments on commit 6bbaba7

Please sign in to comment.