Skip to content
This repository has been archived by the owner on Feb 20, 2024. It is now read-only.

Commit

Permalink
Add configration of EXTERNAL_URL (#67)
Browse files Browse the repository at this point in the history
* Add configration of EXTERNAL_URL

* Switch to origin/path_prefix

* Add routes logging
  • Loading branch information
mattdean-digicatapult authored Jul 21, 2022
1 parent ca138c6 commit 7b91e11
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Inteli OpenAPI service for interacting with the DSCP (Digital Supply-Chain Platf
| :--------------------------- | :------: | :--------------------: | :----------------------------------------------------------------------------------- |
| SERVICE_TYPE | N | `info` | Logging level. Valid values are [`trace`, `debug`, `info`, `warn`, `error`, `fatal`] |
| PORT | N | `80` | The port for the API to listen on |
| EXTERNAL_ORIGIN | N | | The origin from which the OpenAPI service is accessible. If not provided the value will default to `http://localhost:${PORT}` |
| EXTERNAL_PATH_PREFIX | N | | A path prefix from which this service is served |
| LOG_LEVEL | N | `info` | Logging level. Valid values are [`trace`, `debug`, `info`, `warn`, `error`, `fatal`] |
| API_VERSION | N | `package.json version` | API version |
| API_MAJOR_VERSION | N | `v1` | API major version |
Expand Down
10 changes: 8 additions & 2 deletions app/api-v1/api-doc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const { PORT, API_VERSION, API_MAJOR_VERSION, AUTH_TYPE } = require('../env')
const { PORT, API_VERSION, API_MAJOR_VERSION, AUTH_TYPE, EXTERNAL_ORIGIN, EXTERNAL_PATH_PREFIX } = require('../env')

let url = EXTERNAL_ORIGIN || `http://localhost:${PORT}`
if (EXTERNAL_PATH_PREFIX) {
url = `${url}/${EXTERNAL_PATH_PREFIX}`
}
url = `${url}/${API_MAJOR_VERSION}`

const securitySchemes =
AUTH_TYPE === 'JWT'
Expand All @@ -21,7 +27,7 @@ const apiDoc = {
},
servers: [
{
url: `http://localhost:${PORT}/${API_MAJOR_VERSION}`,
url,
},
],
components: {
Expand Down
2 changes: 2 additions & 0 deletions app/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const vars = envalid.cleanEnv(
FILE_UPLOAD_SIZE_LIMIT_BYTES: envalid.num({ default: 1024 * 1024 * 100, devDefault: 1024 * 1024 * 10 }),
IDENTITY_SERVICE_HOST: envalid.host({ devDefault: 'localhost' }),
IDENTITY_SERVICE_PORT: envalid.port({ devDefault: 3002 }),
EXTERNAL_ORIGIN: envalid.str({ default: '' }),
EXTERNAL_PATH_PREFIX: envalid.str({ default: '' }),
},
{
strict: true,
Expand Down
23 changes: 20 additions & 3 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ const multer = require('multer')
const path = require('path')
const bodyParser = require('body-parser')
const compression = require('compression')
const { PORT, API_VERSION, API_MAJOR_VERSION, FILE_UPLOAD_SIZE_LIMIT_BYTES, AUTH_TYPE } = require('./env')
const {
PORT,
API_VERSION,
API_MAJOR_VERSION,
FILE_UPLOAD_SIZE_LIMIT_BYTES,
AUTH_TYPE,
EXTERNAL_PATH_PREFIX,
} = require('./env')
const logger = require('./utils/Logger')
const v1ApiDoc = require('./api-v1/api-doc')
const v1DscpApiService = require('./api-v1/services/dscpApiService')
Expand Down Expand Up @@ -68,16 +75,26 @@ async function createHttpServer() {
swaggerOptions: {
urls: [
{
url: `http://localhost:${PORT}/${API_MAJOR_VERSION}/api-docs`,
url: `${v1ApiDoc.servers[0].url}/api-docs`,
name: 'Inteli API Service',
},
],
},
}

app.use(`/${API_MAJOR_VERSION}/swagger`, swaggerUi.serve, swaggerUi.setup(null, options))
app.use(
EXTERNAL_PATH_PREFIX ? `/${EXTERNAL_PATH_PREFIX}/${API_MAJOR_VERSION}/swagger` : `/${API_MAJOR_VERSION}/swagger`,
swaggerUi.serve,
swaggerUi.setup(null, options)
)
app.use(handleErrors)

logger.trace('Registered Express routes: %s', {
toString: () => {
return JSON.stringify(app._router.stack.map(({ route }) => route && route.path).filter((p) => !!p))
},
})

return { app }
}

Expand Down
4 changes: 2 additions & 2 deletions helm/inteli-api/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apiVersion: v2
name: inteli-api
appVersion: '1.29.0'
appVersion: '1.29.1'
description: A Helm chart for inteli-api
version: '1.29.0'
version: '1.29.1'
type: application
maintainers:
- name: digicatapult
Expand Down
6 changes: 6 additions & 0 deletions helm/inteli-api/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ metadata:
{{- include "inteli-api.labels" . | nindent 4 }}
data:
port: {{ .Values.config.port | quote }}
{{- if .Values.config.externalOrigin }}
externalOrigin: {{ .Values.config.externalOrigin }}
{{- end }}
{{- if .Values.config.externalPathPrefix }}
externalPathPrefix: {{ .Values.config.externalPathPrefix }}
{{- end }}
dscpApiHost: {{ .Values.config.dscpApiHost }}
dscpApiPort: {{ .Values.config.dscpApiPort | quote }}
logLevel: {{ .Values.config.logLevel }}
Expand Down
14 changes: 14 additions & 0 deletions helm/inteli-api/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ spec:
configMapKeyRef:
name: {{ include "inteli-api.fullname" . }}-config
key: port
{{- if .Values.config.externalOrigin }}
- name: EXTERNAL_ORIGIN
valueFrom:
configMapKeyRef:
name: {{ include "inteli-api.fullname" . }}-config
key: externalOrigin
{{- end }}
{{- if .Values.config.externalPathPrefix }}
- name: EXTERNAL_PATH_PREFIX
valueFrom:
configMapKeyRef:
name: {{ include "inteli-api.fullname" . }}-config
key: externalPathPrefix
{{- end }}
- name: LOG_LEVEL
valueFrom:
configMapKeyRef:
Expand Down
4 changes: 3 additions & 1 deletion helm/inteli-api/values.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
config:
port: 80
# externalOrigin: "http://localhost:3000" # Overrides the server url in the openapi spec
# externalPathPrefix: "alice/inteli-api" # Path prefix to be applied to served API routes
dscpApiHost: dscp-api
dscpApiPort: 80
logLevel: info
Expand Down Expand Up @@ -39,7 +41,7 @@ service:
image:
repository: digicatapult/inteli-api
pullPolicy: IfNotPresent
tag: 'v1.29.0'
tag: 'v1.29.1'
pullSecrets: []

postgresql:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@digicatapult/inteli-api",
"version": "1.29.0",
"version": "1.29.1",
"description": "Insert repo description",
"main": "app/index.js",
"scripts": {
Expand Down

0 comments on commit 7b91e11

Please sign in to comment.