Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions api/ecosystem.config.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"$schema": "https://json.schemastore.org/pm2-ecosystem",
"apps": [
{
"name": "unraid-api",
"script": "./dist/main.js",
"cwd": "/usr/local/unraid-api",
"log": "/var/log/unraid-api/unraid-api.log",
"exec_mode": "fork",
"wait_ready": true,
"listen_timeout": 30000,
"listen_timeout": 15000,
"max_restarts": 10,
"min_uptime": 10000,
"ignore_watch": [
"node_modules",
"src",
".env.*",
"myservers.cfg"
]
],
"log": "/var/log/unraid-api/unraid-api.log"
}
]
}
22 changes: 0 additions & 22 deletions api/src/core/logrotate/setup-logrotate.ts

This file was deleted.

46 changes: 0 additions & 46 deletions api/src/core/sso/auth-request-setup.ts

This file was deleted.

20 changes: 0 additions & 20 deletions api/src/core/sso/sso-remove.ts

This file was deleted.

69 changes: 0 additions & 69 deletions api/src/core/sso/sso-setup.ts

This file was deleted.

9 changes: 7 additions & 2 deletions api/src/dotenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { config } from 'dotenv';

const env =
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test'
? config({ debug: true, path: `./.env.${process.env.NODE_ENV}`, encoding: 'utf-8' })
? config({
debug: true,
path: `./.env.${process.env.NODE_ENV}`,
encoding: 'utf-8',
override: true,
})
: config({
path: '/usr/local/unraid-api/.env',
encoding: 'utf-8',
});

export default env;
export default env;
29 changes: 15 additions & 14 deletions api/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@ import { version } from 'package.json';
export const API_VERSION =
process.env.npm_package_version ?? version ?? new Error('API_VERSION not set');

export const NODE_ENV = process.env.NODE_ENV as 'development' | 'test' | 'staging' | 'production' ?? 'production';
export const NODE_ENV =
(process.env.NODE_ENV as 'development' | 'test' | 'staging' | 'production') ?? 'production';
export const environment = {
IS_MAIN_PROCESS: false,
};
export const CHOKIDAR_USEPOLLING = process.env.CHOKIDAR_USEPOLLING === 'true';
export const IS_DOCKER = process.env.IS_DOCKER === 'true';
export const DEBUG = process.env.DEBUG === 'true';
export const INTROSPECTION = process.env.INTROSPECTION === 'true';
export const ENVIRONMENT = process.env.ENVIRONMENT as 'production' | 'staging' | 'development' ?? 'production';
export const ENVIRONMENT = process.env.ENVIRONMENT
? (process.env.ENVIRONMENT as 'production' | 'staging' | 'development')
: 'production';
export const GRAPHQL_INTROSPECTION = Boolean(INTROSPECTION ?? DEBUG ?? ENVIRONMENT !== 'production');
export const PORT = process.env.PORT ?? '/var/run/unraid-api.sock';
export const DRY_RUN = process.env.DRY_RUN === 'true';
export const BYPASS_PERMISSION_CHECKS = process.env.BYPASS_PERMISSION_CHECKS === 'true';
export const BYPASS_CORS_CHECKS = process.env.BYPASS_CORS_CHECKS === 'true';
export const LOG_CORS = process.env.LOG_CORS === 'true';
export const LOG_TYPE = (process.env.LOG_TYPE as 'pretty' | 'raw') ?? 'pretty';
export const LOG_LEVEL = process.env.LOG_LEVEL?.toUpperCase() as
| 'TRACE'
| 'DEBUG'
| 'INFO'
| 'WARN'
| 'ERROR'
| 'FATAL' ?? process.env.ENVIRONMENT === 'production' ? 'INFO' : 'TRACE';
export const MOTHERSHIP_GRAPHQL_LINK =
process.env.MOTHERSHIP_GRAPHQL_LINK ??
(process.env.ENVIRONMENT === 'staging'
? 'https://staging.mothership.unraid.net/ws'
: 'https://mothership.unraid.net/ws');
export const LOG_LEVEL = process.env.LOG_LEVEL
? (process.env.LOG_LEVEL.toUpperCase() as 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL')
: process.env.ENVIRONMENT === 'production'
? 'INFO'
: 'TRACE';
export const MOTHERSHIP_GRAPHQL_LINK = process.env.MOTHERSHIP_GRAPHQL_LINK
? process.env.MOTHERSHIP_GRAPHQL_LINK
: ENVIRONMENT === 'staging'
? 'https://staging.mothership.unraid.net/ws'
: 'https://mothership.unraid.net/ws';
22 changes: 2 additions & 20 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import exitHook from 'exit-hook';
import { WebSocket } from 'ws';

import { logger } from '@app/core/log';
import { setupLogRotation } from '@app/core/logrotate/setup-logrotate';
import { setupAuthRequest } from '@app/core/sso/auth-request-setup';
import { removeSso } from '@app/core/sso/sso-remove';
import { setupSso } from '@app/core/sso/sso-setup';
import { fileExistsSync } from '@app/core/utils/files/file-exists';
import { environment, PORT } from '@app/environment';
import * as envVars from '@app/environment';
Expand Down Expand Up @@ -61,8 +57,6 @@ try {
// Must occur before config is loaded to ensure that the handler can fix broken configs
await startStoreSync();

await setupLogRotation();

// Load my servers config file into store
await store.dispatch(loadConfigFile());

Expand Down Expand Up @@ -100,22 +94,10 @@ try {

startMiddlewareListeners();

// If the config contains SSO IDs, enable SSO
try {
if (store.getState().config.remote.ssoSubIds) {
await setupAuthRequest();
await setupSso();
logger.info('SSO setup complete');
} else {
await removeSso();
}
} catch (err) {
logger.error('Failed to setup SSO with error: %o', err);
}
// On process exit stop HTTP server
exitHook((signal) => {
exitHook(async (signal) => {
console.log('exithook', signal);
server?.close?.();
await server?.close?.();
// If port is unix socket, delete socket before exiting
unlinkUnixPort();

Expand Down
2 changes: 2 additions & 0 deletions api/src/unraid-api/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AuthModule } from '@app/unraid-api/auth/auth.module';
import { CronModule } from '@app/unraid-api/cron/cron.module';
import { GraphModule } from '@app/unraid-api/graph/graph.module';
import { RestModule } from '@app/unraid-api/rest/rest.module';
import { UnraidFileModifierModule } from '@app/unraid-api/unraid-file-modifier/unraid-file-modifier.module';

@Module({
imports: [
Expand All @@ -30,6 +31,7 @@ import { RestModule } from '@app/unraid-api/rest/rest.module';
limit: 100, // 100 requests per 10 seconds
},
]),
UnraidFileModifierModule,
],
controllers: [],
providers: [
Expand Down
2 changes: 1 addition & 1 deletion api/src/unraid-api/auth/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ApiKeyService implements OnModuleInit {
}

private setupWatch() {
watch(this.basePath, { ignoreInitial: false }).on('change', async (path) => {
watch(this.basePath, { ignoreInitial: false }).on('all', async (path) => {
this.logger.debug(`API key changed: ${path}`);
this.memoryApiKeys = [];
this.memoryApiKeys = await this.loadAllFromDisk();
Expand Down
1 change: 1 addition & 0 deletions api/src/unraid-api/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('AuthService', () => {
description: 'Test API Key Description',
roles: [Role.GUEST, Role.CONNECT],
createdAt: new Date().toISOString(),
permissions: [],
};

const mockApiKeyWithSecret: ApiKeyWithSecret = {
Expand Down
32 changes: 27 additions & 5 deletions api/src/unraid-api/cli/log.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Injectable } from '@nestjs/common';

import { levels, LogLevel } from '@app/core/log';
import { LOG_LEVEL } from '@app/environment';

@Injectable()
export class LogService {
private logger = console;
Expand All @@ -8,23 +11,42 @@ export class LogService {
this.logger.clear();
}

shouldLog(level: LogLevel): boolean {
const logLevelsLowToHigh = levels;
const shouldLog = (
logLevelsLowToHigh.indexOf(level) >=
logLevelsLowToHigh.indexOf(LOG_LEVEL.toLowerCase() as LogLevel)
);
return shouldLog;
}

log(message: string): void {
this.logger.log(message);
if (this.shouldLog('info')) {
this.logger.log(message);
}
}

info(message: string): void {
this.logger.info(message);
if (this.shouldLog('info')) {
this.logger.info(message);
}
}

warn(message: string): void {
this.logger.warn(message);
if (this.shouldLog('warn')) {
this.logger.warn(message);
}
}

error(message: string, trace: string = ''): void {
this.logger.error(message, trace);
if (this.shouldLog('error')) {
this.logger.error(message, trace);
}
}

debug(message: any, ...optionalParams: any[]): void {
this.logger.debug(message, ...optionalParams);
if (this.shouldLog('debug')) {
this.logger.debug(message, ...optionalParams);
}
}
}
Loading
Loading