Skip to content

Commit 3aeed30

Browse files
Devportal alpha release commit
1 parent 1c0c254 commit 3aeed30

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM alpine:latest
2+
3+
RUN apk update && apk upgrade && \
4+
apk add --update --no-cache python3
5+
6+
WORKDIR /deployment
7+
8+
COPY etc/ etc/
9+
COPY src/ src/
10+
COPY templates/ templates/
11+
12+
RUN python3 -m venv /deployment/env/ && \
13+
source /deployment/env/bin/activate && \
14+
# python3 -m pip install --upgrade pip && \
15+
pip3 install --no-cache --upgrade pip setuptools virtualenv && \
16+
pip3 install -r src/requirements.txt
17+
18+
WORKDIR /deployment/src
19+
CMD ["/deployment/env/bin/python3", "./main.py"]

contrib/devportal/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM redocly/cli
2+
3+
WORKDIR /deployment
4+
5+
COPY src/ src/
6+
7+
RUN apk update && \
8+
apk add --update --no-cache bash python3 && \
9+
python3 -m ensurepip && \
10+
pip3 install --no-cache --upgrade pip setuptools virtualenv && \
11+
virtualenv /deployment/env/ && source /deployment/env/bin/activate && \
12+
python3 -m pip install --upgrade pip && \
13+
pip3 install -r /deployment/src/requirements.txt
14+
15+
ENTRYPOINT [ "/deployment/src/start.sh" ]

contrib/devportal/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Developer portal add-on
2+
3+
This is a wrapper for [Redocly CLI](https://redocly.com/docs/cli/) used by the NGINX declarative API to create and publish developer portals
4+
5+
To build:
6+
7+
docker build --no-cache -t <YOUR_TARGET_IMAGE_NAME> .
8+
9+
To run the pre-build image:
10+
11+
docker run --rm -d -p 5001:5000 --name devportal fiorucci/nginx-declarative-api-devportal
12+
13+
To test:
14+
15+
curl -i -w '\n' 127.0.0.1:5001/v1/devportal -X POST -H "Content-Type: application/json" -d @<OPENAPI_JSON_SCHEMA_FILENAME>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
typing
2+
uvicorn
3+
fastapi
4+
uuid

contrib/devportal/src/server.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pathlib
2+
import uvicorn
3+
import subprocess
4+
import json
5+
import uuid
6+
7+
from typing import Any, Dict, AnyStr, List, Union
8+
9+
from fastapi import FastAPI, Request
10+
from fastapi.responses import PlainTextResponse, Response, JSONResponse
11+
12+
app = FastAPI(
13+
title="Redoc connector",
14+
version="1.0.0",
15+
contact={"name": "GitHub", "url": "https://github.com/fabriziofiorucci/NGINX-Declarative-API"}
16+
)
17+
18+
JSONObject = Dict[AnyStr, Any]
19+
JSONArray = List[Any]
20+
JSONStructure = Union[JSONArray, JSONObject]
21+
22+
@app.post("/v1/devportal", status_code=200, response_class=JSONResponse)
23+
def post_devportal(response: Response, request: JSONStructure = None):
24+
if request:
25+
try:
26+
sessionUUID = uuid.uuid4()
27+
apiSchema = json.dumps(request)
28+
tmpFileBase = f"/tmp/{sessionUUID}"
29+
tmpFileSchema = f"{tmpFileBase}.json"
30+
tmpFileDocs = f"{tmpFileBase}.html"
31+
tmpFile = open(tmpFileSchema,"w")
32+
tmpFile.write(apiSchema)
33+
tmpFile.close()
34+
35+
output = subprocess.check_output(f"redocly build-docs '{tmpFileSchema}' --output={tmpFileDocs}", shell=True)
36+
37+
with open(tmpFileDocs, 'r') as file:
38+
devPortal = file.read().replace('\n','')
39+
40+
return JSONResponse (content={'status': 'success','apischema': f'{apiSchema}','devportal': f'{devPortal}'}, status_code=200)
41+
except subprocess.CalledProcessError as e:
42+
return JSONResponse (content={'status': str(e)}, status_code=400)
43+
finally:
44+
schemaFile = pathlib.Path(tmpFileSchema)
45+
if schemaFile.is_file():
46+
schemaFile.unlink()
47+
48+
docsFile = pathlib.Path(tmpFileDocs)
49+
if docsFile.is_file():
50+
docsFile.unlink()
51+
else:
52+
return JSONResponse (content={'status': 'invalid body'}, status_code=400)
53+
54+
if __name__ == '__main__':
55+
uvicorn.run("server:app", host='0.0.0.0', port=5000)

contrib/devportal/src/start.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# https://redocly.com/docs/cli/commands/build-docs/
4+
5+
# Disable telemetry collection
6+
# https://github.com/Redocly/redocly-cli/#data-collection
7+
export REDOCLY_TELEMETRY=off
8+
9+
/deployment/env/bin/python /deployment/src/server.py

0 commit comments

Comments
 (0)