Skip to content

Commit a7193f2

Browse files
authored
include invocation for swagger-ui tool (#17)
* include invocation for swagger-ui tool * fix readme
1 parent 6ba859d commit a7193f2

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenAPI
22

3-
[![Build Status](https://github.com/JuliaComputing/OpenAPI.jl/workflows/CI/badge.svg)](https://github.com/JuliaComputing/OpenAPI.jl/actions?query=workflow%3ACI+branch%3Amaster)
3+
[![Build Status](https://github.com/JuliaComputing/OpenAPI.jl/workflows/CI/badge.svg)](https://github.com/JuliaComputing/OpenAPI.jl/actions?query=workflow%3ACI+branch%3Amain)
44
[![codecov.io](http://codecov.io/github/JuliaComputing/OpenAPI.jl/coverage.svg?branch=main)](http://codecov.io/github/JuliaComputing/OpenAPI.jl?branch=main)
55

66
This is the Julia library needed with code generated by the [OpenAPI generator](https://openapi-generator.tech/).
@@ -208,6 +208,28 @@ The Petstore is a common example that most OpenAPI implementations use to test a
208208
- Client: [docs](test/client/petstore_v3/petstore/README.md), [implementation](test/client/petstore_v3)
209209
- Server: [docs](test/server/petstore_v3/petstore/README.md), [implementation](test/server/petstore_v3)
210210

211+
## Swagger UI
212+
213+
[Swagger UI](https://swagger.io/tools/swagger-ui/) allows visualization and interaction with the API’s resources without having any of the implementation logic in place. OpenAPI.jl includes convenience methods to launch Swagger UI from Julia.
214+
215+
Use `OpenAPI.swagger_ui` to open Swagger UI. It uses the standard `swaggerapi/swagger-ui` docker image and requires docker engine to be installed.
216+
217+
```julia
218+
OpenAPI.swagger_ui(
219+
spec::String; # the OpenAPI specification to use
220+
port::Int=8080, # port to use
221+
use_sudo::Bool=false # whether to use sudo while invoking docker
222+
)
223+
```
224+
225+
To stop the Swagger UI container, use `OpenAPI.stop_swagger_ui`.
226+
227+
```julia
228+
OpenAPI.stop_swagger_ui(;
229+
use_sudo::Bool=false # whether to use sudo while invoking docker
230+
)
231+
```
232+
211233
## TODO
212234

213235
Not all OpenAPI features are supported yet, e.g.:

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- "src/tools.jl"

src/OpenAPI.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ include("val.jl")
1111
include("json.jl")
1212
include("client.jl")
1313
include("server.jl")
14+
include("tools.jl")
1415

1516
end # module

src/tools.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const SWAGGER_UI_IMAGE = "swaggerapi/swagger-ui"
2+
3+
docker_cmd(; use_sudo::Bool=false) = use_sudo ? `sudo docker` : `docker`
4+
5+
function stop_swagger_ui(; use_sudo::Bool=false)
6+
docker = docker_cmd(; use_sudo=use_sudo)
7+
find_cmd = `$docker ps -a -q -f ancestor=$SWAGGER_UI_IMAGE`
8+
container_id = strip(String(read(find_cmd)))
9+
10+
if !isempty(container_id)
11+
stop_cmd = `$docker stop $container_id`
12+
stop_res = strip(String(read(stop_cmd)))
13+
14+
if stop_res == container_id
15+
@debug("Stopped Swagger UI container")
16+
elseif isempty(stop_res)
17+
@debug("Swagger UI container not running")
18+
else
19+
@error("Failed to stop Swagger UI container: $stop_res")
20+
return false
21+
end
22+
23+
container_id = strip(String(read(find_cmd)))
24+
if !isempty(container_id)
25+
rm_cmd = `$docker rm $container_id`
26+
rm_res = strip(String(read(rm_cmd)))
27+
28+
if rm_res == container_id
29+
@debug("Removed Swagger UI container")
30+
elseif isempty(rm_res)
31+
@debug("Swagger UI container not found")
32+
else
33+
@error("Failed to remove Swagger UI container: $rm_res")
34+
return false
35+
end
36+
end
37+
38+
return true
39+
else
40+
@debug("Swagger UI container not found")
41+
end
42+
43+
return false
44+
end
45+
46+
function swagger_ui(spec::String; port::Int=8080, use_sudo::Bool=false)
47+
docker = docker_cmd(; use_sudo=use_sudo)
48+
stop_swagger_ui(; use_sudo=use_sudo)
49+
cmd = `$docker run -d --rm -p $port:8080 -e SWAGGER_JSON=/tmp/spec.json -v $spec:/tmp/spec.json $SWAGGER_UI_IMAGE`
50+
run(cmd)
51+
return "http://localhost:$port"
52+
end

0 commit comments

Comments
 (0)