From 552a0614581bbcca07fa3a4847585c2ec939893c Mon Sep 17 00:00:00 2001 From: Daniele Guido Date: Mon, 22 Jul 2024 12:08:21 +0200 Subject: [PATCH] Fix/version service (#399) * Update configuration.ts * add env variables from process.env directly and use typescript --- src/configuration.ts | 114 +++++++++++++++++- ...{version.service.js => version.service.ts} | 29 +++-- 2 files changed, 131 insertions(+), 12 deletions(-) rename src/services/version/{version.service.js => version.service.ts} (50%) diff --git a/src/configuration.ts b/src/configuration.ts index 9e2c9702..b2207a56 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -5,6 +5,7 @@ import type { RedisClientOptions } from 'redis' import type { RateLimiterConfiguration } from './services/internal/rateLimiter/redis' import { Sequelize } from 'sequelize' import { CeleryClient } from './celery' +import { AuthenticationConfiguration } from '@feathersjs/authentication' export type RedisConfiguration = RedisClientOptions & { enable?: boolean; host?: string } @@ -21,6 +22,66 @@ export interface MediaConfiguration { protectedPath: string } +export interface SolrConfiguration { + host: string + port: number + auth: { + user: string + pass: string + } + search: { + alias: string + endpoint: string + } + mentions: { + alias: string + endpoint: string + } + topics: { + alias: string + endpoint: string + } + images: { + alias: string + endpoint: string + } + entities: { + alias: string + endpoint: string + } +} + +export interface SequelizeConfiguration { + alias?: string + dialect: string + host: string + port: number + auth: { + user: string + pass: string + } + database: string + logging?: boolean + tables?: { + articles: string + pages: string + newspapers: string + users: string + } +} + +export interface FeaturesConfiguration { + textReuse: { + enabled: boolean + } +} + +export interface LocalAuthenticationConfiguration extends AuthenticationConfiguration { + jwtOptions: { + issuer: string + } +} + export interface Configuration { isPublicApi?: boolean allowedCorsOrigins?: string[] @@ -29,12 +90,15 @@ export interface Configuration { publicApiPrefix?: string useDbUserInRequestContext?: boolean problemUriBase?: string - + features?: FeaturesConfiguration // TODO: move to services: + authentication: LocalAuthenticationConfiguration + sequelize: SequelizeConfiguration sequelizeClient?: Sequelize celery?: CeleryConfiguration celeryClient?: CeleryClient media?: MediaConfiguration + solr: SolrConfiguration } const configurationSchema: JSONSchemaDefinition = { @@ -79,6 +143,54 @@ const configurationSchema: JSONSchemaDefinition = { description: 'Base URI for problem URIs. Falls back to the default URI (https://impresso-project.ch/probs) if not set', }, + solr: { + type: 'object', + properties: { + host: { type: 'string', description: 'Solr host' }, + port: { type: 'number', description: 'Solr port' }, + auth: { + type: 'object', + properties: { + user: { type: 'string', description: 'Solr user' }, + pass: { type: 'string', description: 'Solr password' }, + }, + required: ['user', 'pass'], + }, + }, + description: 'Solr configuration', + required: ['host', 'port', 'auth'], + }, + sequelize: { + type: 'object', + properties: { + alias: { type: 'string', description: 'Alias for the Sequelize instance' }, + dialect: { type: 'string', description: 'Dialect of the database' }, + host: { type: 'string', description: 'Host of the database' }, + port: { type: 'number', description: 'Port of the database' }, + auth: { + type: 'object', + properties: { + user: { type: 'string', description: 'Database user' }, + pass: { type: 'string', description: 'Database password' }, + }, + required: ['user', 'pass'], + }, + database: { type: 'string', description: 'Database name' }, + logging: { type: 'boolean', description: 'Enable logging' }, + tables: { + type: 'object', + properties: { + articles: { type: 'string', description: 'Name of the articles table' }, + pages: { type: 'string', description: 'Name of the pages table' }, + newspapers: { type: 'string', description: 'Name of the newspapers table' }, + users: { type: 'string', description: 'Name of the users table' }, + }, + required: ['articles', 'pages', 'newspapers', 'users'], + }, + }, + description: 'Sequelize configuration', + required: ['dialect', 'host', 'port', 'auth', 'database'], + }, }, } as const diff --git a/src/services/version/version.service.js b/src/services/version/version.service.ts similarity index 50% rename from src/services/version/version.service.js rename to src/services/version/version.service.ts index 43688c56..af9a7140 100644 --- a/src/services/version/version.service.js +++ b/src/services/version/version.service.ts @@ -1,9 +1,13 @@ +import debug from 'debug' import { createSwaggerServiceOptions } from 'feathers-swagger' import { docs } from './version.schema' +import { ImpressoApplication } from '../../types' +import { ServiceOptions } from '@feathersjs/feathers' -const { getGitBranch, getGitRevision, getVersion, getFirstAndLastDocumentDates, getNewspaperIndex } = require('./logic') +const log = debug('impresso/services:version') +const { getFirstAndLastDocumentDates, getNewspaperIndex } = require('./logic') -module.exports = function (app) { +module.exports = function (app: ImpressoApplication) { // Initialize our service with any options it requires app.use( '/version', @@ -13,22 +17,25 @@ module.exports = function (app) { const sequelizeConfig = app.get('sequelize') const solr = app.service('cachedSolr') const [firstDate, lastDate] = await getFirstAndLastDocumentDates(solr) - + log('branch:', process.env.GIT_BRANCH, 'revision:', process.env.GIT_REVISION, 'version:', process.env.GIT_TAG) return { solr: { - endpoints: ['search', 'mentions', 'topics', 'images', 'entities'].reduce((acc, d) => { - acc[d] = solrConfig[d].alias - return acc - }, {}), + endpoints: { + search: solrConfig.search.alias, + mentions: solrConfig.mentions.alias, + topics: solrConfig.topics.alias, + images: solrConfig.images.alias, + entities: solrConfig.entities.alias, + }, }, mysql: { endpoint: sequelizeConfig.alias, }, version: app.get('authentication').jwtOptions.issuer, apiVersion: { - branch: await getGitBranch(), - revision: await getGitRevision(), - version: await getVersion(), + branch: process.env.GIT_BRANCH || 'N/A', + revision: process.env.GIT_REVISION || 'N/A', + version: process.env.GIT_TAG || 'N/A', }, documentsDateSpan: { firstDate, lastDate }, newspapers: await getNewspaperIndex(), @@ -39,6 +46,6 @@ module.exports = function (app) { { events: [], docs: createSwaggerServiceOptions({ schemas: {}, docs }), - } + } as ServiceOptions ) }