Skip to content

Commit

Permalink
Initial Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Vecchiola committed Jan 23, 2020
1 parent 6e83b5e commit 3ef24bc
Show file tree
Hide file tree
Showing 12 changed files with 660 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
docker-compose.yaml
README.md.
.gitignore
.build_image
Makefile

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.build_image
42 changes: 42 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM node:alpine AS builder

WORKDIR /opt/app

# From: https://hub.docker.com/r/jfyne/node-alpine-yarn/dockerfile
# added: git, openssh

RUN apk add --no-cache --virtual .build-deps \
ca-certificates \
git \
openssh \
wget \
tar && \
cd /usr/local/bin && \
wget https://yarnpkg.com/latest.tar.gz && \
tar zvxf latest.tar.gz && \
ln -s /usr/local/bin/dist/bin/yarn.js /usr/local/bin/yarn.js && \
mkdir -p /opt/app && \
git clone https://github.com/mrin9/RapiDoc.git /opt/app/RapiDoc && \
apk del .build-deps

RUN cd /opt/app/RapiDoc && \
yarn install && \
yarn build

FROM nginx:1.17.6-alpine AS server

COPY --from=builder /opt/app/RapiDoc/dist /usr/share/nginx/html


FROM server AS portal

RUN mkdir /opt/app/
ADD docker /opt/app
RUN mv /opt/app/html/* /usr/share/nginx/html/

ENTRYPOINT [ "/opt/app/boot.sh" ]

ARG commit
LABEL commit=$commit


24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
COMMIT ?= 0000000
DOCKER_IMAGE ?= rapidoc:latest


.PHONY: build
build: Dockerfile docker
docker build --build-arg commit=$(COMMIT) -t $(DOCKER_IMAGE) .
touch .build_image

.build_image: build

.PHONY: start
start: .build_image
docker-compose up

.PHONY: stop
stop:
docker-compose down

.PHONY: clean
clean:
-docker-compose down
-docker rmi $(DOCKER_IMAGE)
-rm .build_image
128 changes: 126 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,126 @@
# rapidoc-docker
Docker Image for the Rapidoc Web Application (Swagger API Documentation).
# RapiDoc OpenAPI Viewer Docker Image

This repository contains the definition Docker Image for [RapiDoc](https://github.com/mrin9/RapiDoc), which is an OpenAPI viewer. The documentation is exposed via a modified `nginx:1.17.6-alpine` image that cotains the production packaging of [RapiDoc](https://github.com/mrin9/RapiDoc).

## Getting Started

```
make start
```

This command will build the RapiDoc image and start the container to expose the API on port 8080.

Other relevant make commands:

```
make build # build the docker image for RapiDoc.
make clean # shuts down the docker network and deletes the image for RapiDoc.
```

## Folder Content

```
|- docker
| |- boot.sh # initialisation script for the container.
| |- defaults.env # default settings for the RapiDoc viewer.
| |- index.html.template # template html page for the RapiDoc viewer.
| |- html # HTML assets (favicon, logo, additional javascript).
|
|- .dockerignore # exclude files from docker context
|- .gitignore # exclude files from git
|- Dockerfile # docker image definition
|- Makefile # make instructions for this folder
|- README.md # this file.
```


## Configuring the Viewer

All the options that can be used to configure the RapiDoc viewer can be used to style the web application by passing environment variables to the container. The default values for these variables are stored in the `docker/defaults.env` file.

**NOTE:** the attribute `spec-file` is not supported and only `spec-url` is used as a pre-built setup.

For more details about the meaning of these attributes please see the [RapiDoc documentation](https://mrin9.github.io/RapiDoc/api.html)

### Specification Configuration

- `TMPL_RAPIDOC_API_MANIFEST_URL` (default: empty): this is the URL to the manifest file. This parameter is utilised in place of `TMPL_RAPIDOC_SPEC_URL` for serving multiple OpenAPI specifications and being able to switch among them. For more details about capability check the section _Serving Multiple OpenAPI Specifications_.
- `TMPL_RAPIDOC_LOGO_URL` (default: `./logo.png`): this is the url of the logo that is shown in the viewer. You can customise it by either replacing the `docker/html/logo.png` file or simply modifing the container to point to another file.
- `TMPL_RAPIDOC_SPEC_URL` (default: `openapi.json`): maps to `spec-url`
- `TMPL_RAPIDOC_SERVER_URL` (default: `TMPL_RAPIDOC_SERVER_URL`): maps to `server-url`
- `TMPL_RAPIDOC_DEFAULT_API_SERVER` (default: empty): maps to `default-api-server`

### UI Appearance
- `TMPL_RAPIDC_HEADING_TEXT` (default: `Platform API Documentation`): maps to `heading-text`
- `TMPL_RAPIDOC_THEME` (default: `light`): maps to `theme`
- `TMPL_RAPIDOC_BG_COLOR` (default: empty): maps to `bg-color`
- `TMPL_RAPIDOC_TEXT_COLOR` (default: empty): maps to `text-color`
- `TMPL_RAPIDOC_HEADER_COLOR` (default: empty): maps to `header-color`
- `TMPL_RAPIDOC_PRIMARY_COLOR` (default: empty): maps to `primary-color`
- `TMPL_RAPIDOC_REGULAR_FONT` (default: empty): maps to `regular-font`
- `TMPL_RAPIDOC_MONO_FONT` (default: empty): maps to `mono-font`
- `TMPL_RAPIDOC_NAV_BG_COLOR` (default: empty): maps to `nav-bg-color`
- `TMPL_RAPIDOC_NAV_TEXT_COLOR` (default: empty): maps to `nav-text-color`
- `TMPL_RAPIDOC_NAV_HOVER_TEXT_COLOR` (default: empty): maps to `nav-hover-text-color`
- `TMPL_RAPIDOC_NAV_HOVER_BG_COLOR` (default: empty): maps to `nav-hover-bg-color`
- `TMPL_RAPICOD_NAV_ACCENT_COLOR` (default: empty): maps to `nav-accent-color`

### Layout
- `TMPL_RAPIDOC_LAYOUT` (default: `row`): maps to `layout`
- `TMPL_RAPIDOC_SORT_TAGS` (default: `false`): maps to `sort-tags`
- `TMPL_RAPIDOC_GOTO_PATH` (default: empty): maps to `goto-path`
- `TMPL_RAPIDOC_RENDER_STYLE` (default: `read`): maps to `render-style`
- `TMPL_RAPIDOC_API_LIST_STYLE` (default: `group-by-tag`): maps to `api-list-style`
- `TMPL_RAPIDOC_SCHEMA_STYLE` (default: `tree`): maps to `schema-style`
- `TMPL_RAPIDOC_SCHEMA_EXPAND_LEVEL` (default: `999`): maps to `schema-expand-level`
- `TMPL_RAPIDOC_SCHEMA_DESCRIPTION_EXPANDED` (default: `true`): maps to `schema-description-expanded`
- `TMPL_RAPIDOC_DEFAULT_SCHEMA_TAB=` (default: `model`): maps to `default-schema-tab`
- `TMPL_RAPIDOC_SHOW_HEADER` (default: `true`): maps to `show-header`
- `TMPL_RAPIDOC_SHOW_INFO` (default: `true`): maps to `show-info`

### Functionalities Switches
- `TMPL_RAPIDOC_ALLOW_AUTHENTICATION` (default: `true`): maps to `allow-authentication`
- `TMPL_RAPIDOC_ALLOW_SEARCH` (default: `true`): maps to `allow-search`
- `TMPL_RAPIDOC_ALLOW_TRY` (default: `false`): maps to `allow-try`
- `TMPL_RAPIDOC_ALLOW_API_LIST_STYLE_SELECTION` (default: `true`): maps to `allow-api-list-style-selection`
- `TMPL_RAPIDOC_ALLOW_SERVER_SELECTION` (default: `false`): maps to `allow-server-selection`
- `TMPL_RAPIDOC_ALLOW_SPEC_URL_LOAD` (default: `false`): maps to `allow-spec-url-load`
- `TMPL_RAPIDOC_ALLOW_SPEC_FILE_LOAD` (default: `false`): maps to `allow-spec-file-load`

### API Key Settings
- `TMPL_RAPIDOC_API_KEY_NAME` (default: `Authorization`): maps to `api-key-name`
- `TMPL_RAPIDOC_API_KEY_LOCATION` (default: `header`): maps to `api-key-location`
- `TMPL_RAPIDOC_API_KEY_VALUE` (default: empty): maps to `api-key-value`


## Modifying the Template

The basis structure of the viewer is modelled upon `docker/index.html.template`. You can alter and modify this HTML template document to further customise the appearance of the API documentation.

## Serving Multiple OpenAPI Specifications

The default template for RapiDoc has been modified to support the capability of serving multiple OpenAPI specification which are pre-configured with the server. This is done by specifing the value of `TMPL_RAPIDOC_API_MANIFEST_URL` to point to a JSON file containing the catalog of API specification available. Here is an example of the manifest file:

```json
{
"default" : "./specs/openapi.json",
"specs" : [{
"title" : "First Spec",
"url" : "./specs/first-openapi.json",
"description" : "First OpenAPI Specification"
},{
"title" : "Second Spec",
"url" : "./specs/second-openapi.json",
"description" : "Second OpenAPI Specification"
},{
"title" : "OpenAPI Spec",
"url" : "./specs/openapi.json",
"description" : "Default OpenAPI Specification"
}]
}
```
This particular file instructs the viewer to create a selective menu in the application (the current template places button in the left corner of the application bar) and to configure the viewer to visualise the default option `./specs/openapi.json`. If the manifest URL is omitted the default behaviour falls back to showin the pre-configured API specification identified with the `TMPL_RAPIDOC_SPEC_URL` environment variable.

In order to modify how the menu is created and looks and feel it is sufficient to alter two files:
- `docker/index.html.template`: for the styling and the basic structure of the menu.
- `docker/htmls/api-selector.js`: for modifying the logic associated to the initial loading and selection of the OpenAPI specification.
18 changes: 18 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.0'

services:

open-api-doc:
image: rapidoc:latest
ports:
- "8080:80"
environment:
- "TMPL_RAPIDOC_API_MANIFEST_URL=./specs/manifest.json"
- "TMPL_RAPIDOC_HEADER_COLOR=#003449"
- "TMPL_RAPIDOC_PRIMARY_COLOR=#00B9FF"
- "TMPL_RAPIDOC_LAYOUT=row"
- "TMPL_RAPIDOC_API_LIST_STYLE=group-by-path"
- "TMPL_RAPIDOC_RENDER_STYLE=view"
volumes:
- ../specs:/usr/share/nginx/html/specs

94 changes: 94 additions & 0 deletions docker/boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/bin/sh

# This script perform a set of environment variable replacements
# to customise the configuration of the RapiDoc application and
# prepare the server to support multiple APIs.


# The script can be invoked with an argument which represents the
# name of the prefix of the names of the environment variables that
# will be considered for the substitution. If there is no prefix
# defined, it will use the default prefix TMPL_

# Checking for the script argument and defaulting to TMPL_
#
S_LIST_PREFIX=${1:-TMPL_}
S_RAPIDOC_TEMPLATE=${2:-/opt/app/index.html.template}
S_RAPIDOC_DEFAULTS=${3:-/opt/app/defaults.env}
S_RAPIDOC_ROOT=/usr/share/nginx/html
S_RAPIDOC_HTML=${S_RAPIDOC_ROOT}/index.html
S_SPECS_ROOT=${S_RAPIDOC_ROOT}/specs

# This function creates the list of environment variables that will be used for the substitution.
# It collects all the environment variables that have a specified prefix and creates another
# environment variable that presents them in a comma separated list (S_LIST).
#
function create_substitution_list() {

# This is pretty hard stuff, let me explain it.
# 1. env: we list all the environment variables
# 2. grep "^${PREFIX}*": we filter out all the variables that do not match the given prefix
# 3. sed 's/=.*//': we remove whatever comes after and including =
# 4. sed 's/^/$/': we prefix the remaining content with $
# 5. tr '\n' ',': we remove new-line characters and replace them with comma
# 6. sed 's/.$//': we remove the last character (which would be a comma)
#
export S_LIST=$(env | grep "^${S_LIST_PREFIX}*" | sed 's/=.*//' | sed 's/^/$/' | tr '\n' ',' | sed 's/.$//')

}

# This function is used to dump the content of a file into the console for
# the purpose of debugging the outcome of a substitution. It accepts one
# argument that is the path to th file to show.
#
function show_content {

echo ""
echo "[BEGIN: $1]"

cat $1

echo "[END: $1]"
echo ""
}

echo "0. Initialising ..."
echo " - Prefix : ${S_LIST_PREFIX}"
echo " - Template : ${S_RAPIDOC_TEMPLATE}"
echo " - Defaults : ${S_RAPIDOC_DEFAULTS}"
source ${S_RAPIDOC_DEFAULTS}
echo ""

echo "2. Processing HTML index file .."

create_substitution_list

echo " - Substitution List : $S_LIST"

if [ "${S_LIST}" == "" ]; then
echo " - [WARN] Substitution list is empty, skipping substitution."
echo " - cp ${S_RAPIDOC_TEMPLATE} ${S_RAPIDOC_HTML}"
cp ${S_RAPIDOC_TEMPLATE} ${S_RAPIDOC_HTML}

else

echo " - envsubst ${S_LIST} < ${S_RAPIDOC_TEMPLATE} > ${S_RAPIDOC_HTML}"
envsubst ${S_LIST} < ${S_RAPIDOC_TEMPLATE} > ${S_RAPIDOC_HTML}
fi

if [ "${S_DEBUG}" == "-d" ]; then

show_content ${S_RAPIDOC_HTML}
fi



echo ""


echo "3. Yelding Control to NginX"

nginx -g "daemon off;"



53 changes: 53 additions & 0 deletions docker/defaults.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Location of the OpenAPI spec to load (both JSON and YAML formats accepted)
#
export TMPL_RAPIDOC_API_MANIFEST_URL="${TMPL_RAPIDOC_API_MANIFEST_URL}"
export TMPL_RAPIDOC_LOGO_URL="${TMPL_RAPIDOC_LOGO_URL:-./logo.png}"
export TMPL_RAPIDOC_SPEC_URL="${TMPL_RAPIDOC_SPEC_URL:-openapi.json}"
export TMPL_RAPIDOC_SERVER_URL="${TMPL_RAPIDOC_SERVER_URL}"
export TMPL_RAPIDOC_DEFAULT_API_SERVER="${TMPL_RAPIDOC_DEFAULT_API_SERVER}"

# Theming and basic text
#
export TMPL_RAPIDC_HEADING_TEXT="${TMPL_RAPIDC_HEADING_TEXT:-Platform API Documentation}"
export TMPL_RAPIDOC_THEME="${TMPL_RAPIDOC_THEME:-light}"
export TMPL_RAPIDOC_BG_COLOR="${TMPL_RAPIDOC_BG_COLOR}"
export TMPL_RAPIDOC_TEXT_COLOR="${TMPL_RAPIDOC_TEXT_COLOR}"
export TMPL_RAPIDOC_HEADER_COLOR="${TMPL_RAPIDOC_HEADER_COLOR}"
export TMPL_RAPIDOC_PRIMARY_COLOR="${TMPL_RAPIDOC_PRIMARY_COLOR}"
export TMPL_RAPIDOC_REGULAR_FONT="${TMPL_RAPIDOC_REGULAR_FONT}"
export TMPL_RAPIDOC_MONO_FONT="${TMPL_RAPIDOC_MONO_FONT}"
export TMPL_RAPIDOC_NAV_BG_COLOR="${TMPL_RAPIDOC_NAV_BG_COLOR}"
export TMPL_RAPIDOC_NAV_TEXT_COLOR="${TMPL_RAPIDOC_NAV_TEXT_COLOR}"
export TMPL_RAPIDOC_NAV_HOVER_BG_COLOR="${TMPL_RAPIDOC_NAV_HOVER_BG_COLOR}"
export TMPL_RAPIDOC_NAV_HOVER_TEXT_COLOR="${TMPL_RAPIDOC_NAV_HOVER_TEXT_COLOR}"
export TMPL_RAPICOD_NAV_ACCENT_COLOR="${TMPL_RAPICOD_NAV_ACCENT_COLOR}"

# Layout and rendering options
#
export TMPL_RAPIDOC_LAYOUT="${TMPL_RAPIDOC_LAYOUT:-row}"
export TMPL_RAPIDOC_SORT_TAGS="${TMPL_RAPIDOC_SORT_TAGS:-false}"
export TMPL_RAPIDOC_GOTO_PATH="${TMPL_RAPIDOC_GOTO_PATH}"
export TMPL_RAPIDOC_RENDER_STYLE="${TMPL_RAPIDOC_RENDER_STYLE:-read}"
export TMPL_RAPIDOC_SCHEMA_STYLE="${TMPL_RAPIDOC_SCHEMA_STYLE:-tree}"
export TMPL_RAPIDOC_API_LIST_STYLE="${TMPL_RAPIDOC_API_LIST_STYLE:-group-by-tag}"
export TMPL_RAPIDOC_SCHEMA_EXPAND_LEVEL="${TMPL_RAPIDOC_SCHEMA_EXPAND_LEVEL:-999}"
export TMPL_RAPIDOC_SCHEMA_DESCRIPTION_EXPANDED="${TMPL_RAPIDOC_SCHEMA_DESCRIPTION_EXPANDED:-true}"
export TMPL_RAPIDOC_DEFAULT_SCHEMA_TAB="${TMPL_RAPIDOC_DEFAULT_SCHEMA_TAB:-model}"
export TMPL_RAPIDOC_SHOW_HEADER="${TMPL_RAPIDOC_SHOW_HEADER:-true}"
export TMPL_RAPIDOC_SHOW_INFO="${TMPL_RAPIDOC_SHOW_INFO:-true}"

# Component visibility
#
export TMPL_RAPIDOC_ALLOW_AUTHENTICATION="${TMPL_RAPIDOC_ALLOW_AUTHENTICATION:-true}"
export TMPL_RAPIDOC_ALLOW_SEARCH="${TMPL_RAPIDOC_ALLOW_SEARCH:-true}"
export TMPL_RAPIDOC_ALLOW_TRY="${TMPL_RAPIDOC_ALLOW_TRY:-false}"
export TMPL_RAPIDOC_ALLOW_API_LIST_STYLE_SELECTION="${TMPL_RAPIDOC_ALLOW_API_LIST_STYLE_SELECTION:-true}"
export TMPL_RAPIDOC_ALLOW_SPEC_FILE_LOAD="${TMPL_RAPIDOC_ALLOW_SPEC_FILE_LOAD:-false}"
export TMPL_RAPIDOC_ALLOW_SPEC_URL_LOAD="${TMPL_RAPIDOC_ALLOW_SPEC_URL_LOAD:-false}"
export TMPL_RAPIDOC_ALLOW_SERVER_SELECTION="${TMPL_RAPIDOC_ALLOW_SERVER_SELECTION:-false}"

# API Key options
#
export TMPL_RAPIDOC_API_KEY_NAME="${TMPL_RAPIDOC_API_KEY_NAME:-Authorization}"
export TMPL_RAPIDOC_API_KEY_LOCATION="${TMPL_RAPIDOC_API_KEY_LOCATION:-header}"
export TMPL_RAPIDOC_API_KEY_VALUE="${TMPL_RAPIDOC_API_KEY_VALUE}"
Loading

0 comments on commit 3ef24bc

Please sign in to comment.