Skip to content

Commit

Permalink
Fix/version service (#399)
Browse files Browse the repository at this point in the history
* Update configuration.ts

* add env variables from process.env directly and use typescript
  • Loading branch information
danieleguido authored Jul 22, 2024
1 parent 2db2b3f commit 552a061
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 12 deletions.
114 changes: 113 additions & 1 deletion src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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[]
Expand All @@ -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 = {
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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(),
Expand All @@ -39,6 +46,6 @@ module.exports = function (app) {
{
events: [],
docs: createSwaggerServiceOptions({ schemas: {}, docs }),
}
} as ServiceOptions
)
}

0 comments on commit 552a061

Please sign in to comment.