Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Sep 2, 2024
1 parent 6db3dc5 commit 98f9405
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
4 changes: 2 additions & 2 deletions build/mirrorUnpkg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs')
import { writeFile } from 'node:fs/promises'

const files = [
'swagger-ui.css',
Expand All @@ -10,6 +10,6 @@ Promise.all(
files.map(f =>
fetch('https://unpkg.com/swagger-ui-dist@4.5.0/' + f)
.then(res => res.text())
.then(text => fs.promises.writeFile('www_root/' + f, text))
.then(text => writeFile('www_root/' + f, text))
)
)
15 changes: 8 additions & 7 deletions build/openAPIDoc.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const fs = require('fs')
const swaggerJsdoc = require('swagger-jsdoc')
const norchVersion = require('../package.json').version
import { readFileSync, writeFileSync } from 'node:fs'
import swaggerJsdoc from 'swagger-jsdoc'

const version = JSON.parse(readFileSync('./package.json', 'utf8')).version

const swaggerDefinition = {
openapi: '3.0.1',
info: {
title: 'Norch',
description:
'This is the API documentation for `norch@' +
norchVersion +
version +
'`. You can use this page to get familiar with what Norch can do. If you want to try out the API calls on a server other than `localhost`, fill out the server variable below in the input box labeled `URL`',
contact: {
email: 'fergus@norch.io'
Expand All @@ -17,7 +18,7 @@ const swaggerDefinition = {
// name: 'MIT',
// url: 'https://github.com/fergiemcdowall/search-index/blob/master/LICENSE'
// },
version: norchVersion
version
},
externalDocs: {
description: 'Find out more about Swagger',
Expand Down Expand Up @@ -281,8 +282,8 @@ const swaggerDefinition = {
}
}

fs.writeFileSync(
'www_root/openapi-norch-' + norchVersion + '.json',
writeFileSync(
'www_root/openapi-norch-' + version + '.json',
JSON.stringify(
swaggerJsdoc({
swaggerDefinition,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"scripts": {
"build-openapi-docs": "node build/mirrorUnpkg.js && node build/openAPIDoc.js && npm run lint",
"lint": "standard --fix src build",
"lint": "standard --fix src build test",
"test": "export SANDBOX=test/sandbox && rm -rf $SANDBOX && mkdir $SANDBOX && date && tape test/*-test.js && npm run lint",
"test-cli": "./bin/norch -p 9090 -i test/sandbox/norch-cli & sleep 5s && bats test/test.bats && kill $!",
"start": "./bin/norch"
Expand Down
20 changes: 17 additions & 3 deletions src/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class API {
.DELETE(...this.params(req, 'ID'))
.then(idxRes => this.sendJSONResponse(idxRes, res))

// TODO: DELETE_RAW?

/**
* @openapi
* /DICTIONARY:
Expand Down Expand Up @@ -137,6 +139,8 @@ export class API {
.DOCUMENTS(...this.params(req, 'ID'))
.then(b => this.sendJSONResponse(b, res))

// TODO: DOCUMENT_VECTORS?

/**
* @openapi
* /EXPORT:
Expand Down Expand Up @@ -514,17 +518,27 @@ export class API {
]).then(([LAST_UPDATED, CREATED, DOCUMENT_COUNT]) =>
this.sendJSONResponse(
{
IS_ALIVE: true,
READY: true,
DOCUMENT_COUNT,
// Update these lines when https://github.com/fergiemcdowall/search-index/issues/600
// is rolled out
CREATED: new Date(CREATED),
LAST_UPDATED: new Date(LAST_UPDATED)
},
res
)
)

/**
* @openapi
* /READY:
* get:
* tags: [READ]
* summary: Is index ready?
* description: |
* Returns `{ READY: true }` when index is ready
* responses:
* 200:
* description: Index is ready
*/
READY = (req, res) =>
this.ready
? this.sendJSONResponse({ READY: true }, res)
Expand Down
2 changes: 1 addition & 1 deletion src/Norch.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { resolve, dirname } from 'path'
import figlet from 'figlet'

export class Norch {
constructor(ops = {}) {
constructor (ops = {}) {
const defaultConfigFile = JSON.parse(
readFileSync('./src/defaultConfig.json', 'utf8')
)
Expand Down
12 changes: 7 additions & 5 deletions test/general-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ test('start a norch', async t => {
await fetch('http://localhost:3030/STATUS')
.then(res => res.json())
.then(json =>
t.isEquivalent(
['IS_ALIVE', 'DOCUMENT_COUNT', 'CREATED', 'LAST_UPDATED'],
Object.keys(json)
)
t.isEquivalent(Object.keys(json), [
'READY',
'DOCUMENT_COUNT',
'CREATED',
'LAST_UPDATED'
])
)
.catch(t.error)

.catch(t.error)
await fetch('http://localhost:3030/PUT', {
method: 'POST',
body: JSON.stringify([
Expand Down
2 changes: 1 addition & 1 deletion test/run-from-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test('start a norch', async t => {
.then(res => res.json())
.then(json =>
t.isEquivalent(
['IS_ALIVE', 'DOCUMENT_COUNT', 'CREATED', 'LAST_UPDATED'],
['READY', 'DOCUMENT_COUNT', 'CREATED', 'LAST_UPDATED'],
Object.keys(json)
)
)
Expand Down
16 changes: 15 additions & 1 deletion www_root/openapi-norch-1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@
},
"responses": {
"200": {
"description": "Successfully imported"
"description": "Successfully written"
}
}
}
Expand Down Expand Up @@ -774,6 +774,20 @@
}
}
}
},
"/READY": {
"get": {
"tags": [
"READ"
],
"summary": "Is index ready?",
"description": "Returns `{ READY: true }` when index is ready\n",
"responses": {
"200": {
"description": "Index is ready"
}
}
}
}
}
}

0 comments on commit 98f9405

Please sign in to comment.