Skip to content

Commit

Permalink
Simplify JSON response (openshift-knative#10)
Browse files Browse the repository at this point in the history
The simplified response is so it contains a short JSON, and the
additional data is delivered via headers:

```
HTTP/1.1 200 OK
content-length: 52
Content-Type: application/json
Server: Quarkus/2.13.5.Final-redhat-00002 Java/17.0.5
X-Config: {"sink":"http://localhost:38859","greet":"Welcome","delay":0}
X-Version: feature-event-receiver-8540b9b

{
  "artifact":"knative-showcase",
  "greeting":"Welcome"
}
```
  • Loading branch information
cardil authored Mar 3, 2023
1 parent 8540b9b commit 9f326aa
Show file tree
Hide file tree
Showing 58 changed files with 649 additions and 494 deletions.
4 changes: 0 additions & 4 deletions expressjs/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
K_SINK=http://localhost:31111
PORT=8080
GREETING=Hello
DELAY=0
CONTAINER_BASENAME=quay.io/openshift-knative/
87 changes: 87 additions & 0 deletions expressjs/package-lock.json

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

1 change: 1 addition & 0 deletions expressjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"express-handlebars": "^6.0.6",
"express-prom-bundle": "^6.6.0",
"git-describe": "^4.1.1",
"joi": "^17.8.3",
"morgan": "^1.10.0",
"prom-client": "^14.1.1",
"semver": "^7.3.8"
Expand Down
8 changes: 6 additions & 2 deletions expressjs/src/domain/entity/project.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const semver = require('semver')

class Platform {
constructor({ node, npm }) {
constructor({ node, express }) {

/**
* @type {string}
Expand All @@ -11,7 +11,11 @@ class Platform {
/**
* @type {string}
*/
this.npm = npm
this.express = express
}

toString() {
return `Express/${this.express} Node/${this.node}`
}
}

Expand Down
2 changes: 1 addition & 1 deletion expressjs/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(async () => {
const app = await require('./app').createApp()

const port = process.env.PORT
const port = require('./services/config').port()

app.listen(port, () => {
console.log(`Listening at http://localhost:${port}/`)
Expand Down
76 changes: 54 additions & 22 deletions expressjs/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ const { oapi } = require('../middlewares/openapi')
const { resolveProject } = require('../services/project')
const config = require('../services/config')

/**
* @param {express.Express} app
*/
module.exports = app => {
const project = resolveProject()
app.get('/', getDoc(), async (req, res) => {
try {
const project = await resolveProject()
const idx = new Index({
artifact: project.artifact,
greeting: config.greet(),
})
addResponseHeaders(res, project)
if (shouldRenderJson(req)) {
return res.json(await project)
}
const data = {
project: await project,
config,
return res.json(idx)
}
const data = { project, config }
res.render('index', data)
} catch (err) {
console.error(err)
Expand All @@ -23,7 +28,13 @@ module.exports = app => {

app.options('/', optionsDoc(), async (_, res) => {
try {
res.json(await project)
const project = await resolveProject()
const idx = new Index({
artifact: project.artifact,
greeting: config.greet(),
})
addResponseHeaders(res, project)
res.json(idx)
} catch (err) {
console.error(err)
res.status(500)
Expand All @@ -32,6 +43,38 @@ module.exports = app => {
})
}

class Index {
constructor({ artifact, greeting }) {

/**
* @type {string}
*/
this.artifact = artifact

/**
* @type {string}
*/
this.greeting = greeting
}
}

/**
* Adds response headers
*
* @param {*} res the response object
* @param {Project} project holds project info
*/
function addResponseHeaders(res, project) {
const { sink, greet, delay } = config
res.header('Server', project.platform)
.header('X-Version', project.version)
.header('X-Config', JSON.stringify({
sink: sink(),
greet: greet(),
delay: delay(),
}))
}

function shouldRenderJson(req) {
const ua = req.header('user-agent')
if (ua && ua.startsWith('Mozilla')) {
Expand Down Expand Up @@ -67,23 +110,12 @@ function optionsDoc() {
schema: {
type: 'object',
properties: {
version: { type: 'string' },
artifact: { type: 'string' },
platform: {
type: 'object',
properties: {
node: { type: 'string' },
npm: { type: 'string' }
}
}
greeting: { type: 'string' },
artifact: { type: 'string' }
},
example: {
version: 'v1.3.1',
artifact: '@openshift/knative-showcase',
platform: {
node: 'v12.19.0',
npm: 'v6.14.8'
}
greeting: 'Welcome',
artifact: 'knative-showcase'
}
}
}
Expand Down
55 changes: 44 additions & 11 deletions expressjs/src/services/config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
const Joi = require('joi')

function sink() {
return process.env.K_SINK
return process.env.K_SINK || 'http://localhost:31111'
}

function greeting() {
return process.env.GREETING
function greet() {
return process.env.GREET || 'Welcome'
}

function delay() {
let d = parseInt(process.env.DELAY, 10)
if (isNaN(d) || d < 0) {
return 0
}
return d
return parseInt(process.env.DELAY || '0', 10)
}

function port() {
return process.env.PORT || 8080
}

module.exports = {
const config = {
sink,
greeting,
delay
greet,
delay,
port
}

const schema = Joi.object({
sink: Joi.string()
.uri({ scheme: /https?/ })
.required(),

greet: Joi.string()
.pattern(new RegExp('^[A-Z][a-z]+$'))
.required(),

delay: Joi.number()
.min(0)
.required(),

port: Joi.number()
.min(0)
.required()
})

const { error } = schema.validate({
sink: config.sink(),
greet: config.greet(),
delay: config.delay(),
port: config.port()
})
if (error) {
throw error
}

module.exports = config
6 changes: 3 additions & 3 deletions expressjs/src/services/greeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Notifier } = require('./notifier')
let number = 0

class Greeter {
constructor({ sink, greeting, delay }) {
constructor({ sink, greet, delay }) {

/**
* @type {Notifier}
Expand All @@ -14,7 +14,7 @@ class Greeter {
/**
* @type {() => string}
*/
this.greeting = greeting
this.greet = greet

/**
* @type {() => Number}
Expand All @@ -28,7 +28,7 @@ class Greeter {
*/
async hello(who) {
const hello = new Hello({
greeting: this.greeting(),
greeting: this.greet(),
who,
number: ++number
})
Expand Down
Loading

0 comments on commit 9f326aa

Please sign in to comment.