Skip to content

Commit

Permalink
Render html from openapi spec generated by typespec (#707)
Browse files Browse the repository at this point in the history
Render html from openapi spec generated by typespec
  • Loading branch information
munishchouhan authored Jan 29, 2025
1 parent 4068c61 commit d3dfee9
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 9 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/typespec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ on:
- '**'
paths :
- 'typespec/**'
- VERSION
pull_request:
types: [opened, reopened, synchronize]
paths:
- 'typespec/**'
- VERSION

permissions:
contents: read
Expand All @@ -30,11 +32,26 @@ jobs:
node-version : '20.9.0'

- name : Install tsp
run : npm install -g @typespec/compiler
run : npm install -g @typespec/compiler@0.64.0

- name : Validate tsp files
run : |
cd typespec
tsp install
tsp compile .
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{secrets.TOWER_CI_AWS_ACCESS}}
aws-secret-access-key: ${{secrets.TOWER_CI_AWS_SECRET}}
aws-region: eu-west-1

- name : Login to Amazon ECR
id : login-ecr
uses : aws-actions/amazon-ecr-login@v1

- name: Release OpenAPI docs
if: "contains(github.event.head_commit.message, '[release]')"
run: |
bash typespec/tag-and-push-openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ class ServiceInfoController {
: HttpResponse.badRequest()
}

@Get(uri = "/openapi")
HttpResponse getOpenAPI() {
HttpResponse.redirect(URI.create("/openapi/"))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Wave, containers provisioning service
* Copyright (c) 2024, Seqera Labs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package io.seqera.wave.controller

import spock.lang.Specification

import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus

/**
*
* @author Munish Chouhan <[email protected]>
*/
class ServiceInfoControllerTest extends Specification {

def 'should redirect to /openapi/'() {
given:
def controller = new ServiceInfoController()

when:
HttpResponse response = controller.getOpenAPI()

then:
response.status == HttpStatus.MOVED_PERMANENTLY
response.header('Location') == '/openapi/'
}

}
10 changes: 10 additions & 0 deletions typespec/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM nginxinc/nginx-unprivileged:alpine

# Copy index.html and openapi.yaml to the Nginx html directory
COPY index.html /usr/share/nginx/html/openapi/index.html
COPY tsp-output/@typespec/openapi3/openapi.yaml /usr/share/nginx/html/openapi/openapi.yaml

# Expose port 8080
EXPOSE 8080

CMD ["nginx", "-g", "daemon off;"]
28 changes: 28 additions & 0 deletions typespec/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wave API Documentation</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css">
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js"></script>
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
const ui = SwaggerUIBundle({
url: "openapi.yaml",
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "BaseLayout"
});
window.ui = ui;
};
</script>
</body>
</html>
1 change: 1 addition & 0 deletions typespec/main.tsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi3";
import "@typespec/versioning";
import "./routes.tsp";
12 changes: 6 additions & 6 deletions typespec/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "wave",
"version": "1.8.2",
"version": "1.16.7",
"type": "module",
"dependencies": {
"@typespec/compiler": "latest",
"@typespec/http": "latest",
"@typespec/rest": "latest",
"@typespec/openapi3": "latest"
"@typespec/compiler": "0.64.0",
"@typespec/http": "0.64.0",
"@typespec/openapi3": "0.64.0",
"@typespec/rest": "0.64.0"
},
"private": true
}
}
11 changes: 9 additions & 2 deletions typespec/routes.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import "./models/models.tsp";

using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.Versioning;

@versioned(wave.Versions)
@service({
title: "Wave service",
title: "Wave service"
})
@server("https://wave.seqera.io", "wave endopint")
@server("https://wave.dev-tower.net", "wave dev endpoint")
@server("https://wave.stage-seqera.io", "wave stage endpoint")
@server("https://wave.seqera.io", "wave prod endpoint")
namespace wave {
enum Versions {
v1: "0.0.0",
}
@route("/v1alpha2/container")
interface ContainerService {

Expand Down
39 changes: 39 additions & 0 deletions typespec/tag-and-push-openapi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
#
# Wave, containers provisioning service
# Copyright (c) 2023-2024, Seqera Labs
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#

# Tag and and push the the GitHub repo and Docker images

set -e
set -x

SED=sed
[[ $(uname) == Darwin ]] && SED=gsed

RELEASE=${RELEASE:-$(git show -s --format='%s' | $SED -rn 's/.*\[(release)\].*/\1/p')}

if [[ $RELEASE ]]; then
TAG=v$(cat VERSION)
version=$(cat VERSION)

cd typespec
sed -i "s/version: 0.0.0/version: $version/" "tsp-output/@typespec/openapi3/openapi.yaml"

docker build -t 195996028523.dkr.ecr.eu-west-1.amazonaws.com/wave/openapi:$TAG .
docker push 195996028523.dkr.ecr.eu-west-1.amazonaws.com/wave/openapi:$TAG
fi
3 changes: 3 additions & 0 deletions typespec/tspconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
emit:
- "@typespec/openapi3"
options:
"@typespec/openapi3":
output-file: openapi.yaml

0 comments on commit d3dfee9

Please sign in to comment.