Skip to content

Commit

Permalink
Add GitHub Gist input support.
Browse files Browse the repository at this point in the history
For example:
  GET /?id={gist_id}
  • Loading branch information
defcronyke committed Jun 5, 2021
1 parent 711c0de commit 3cfecb7
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
RUST_BACKTRACE=full cargo build --verbose
mkdir -p rust-playground-api/rust-playground-api
cp target/debug/main rust-playground-api/rust-playground-api/rust-playground-api
cp asm.sh build.sh run.sh test.sh wasm.sh rust-playground-api/rust-playground-api/
cp gist.sh asm-gist.sh build-gist.sh run-gist.sh test-gist.sh wasm-gist.sh rust-playground-api/rust-playground-api/
chmod 755 rust-playground-api/rust-playground-api/rust-playground-api
- name: rust-playground-api-debug-linux-x86_64
Expand All @@ -48,7 +48,7 @@ jobs:
RUST_BACKTRACE=full cargo build --release --verbose
mkdir -p rust-playground-api/rust-playground-api
cp target/release/main rust-playground-api/rust-playground-api/rust-playground-api
cp asm.sh build.sh run.sh test.sh wasm.sh rust-playground-api/rust-playground-api/
cp gist.sh asm-gist.sh build-gist.sh run-gist.sh test-gist.sh wasm-gist.sh rust-playground-api/rust-playground-api/
strip -s rust-playground-api/rust-playground-api/rust-playground-api
chmod 755 rust-playground-api/rust-playground-api/rust-playground-api
Expand Down
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ debug-linux-x86_64:
- RUST_BACKTRACE=full cargo build --verbose
- mkdir -p rust-playground-api
- cp target/debug/main rust-playground-api/rust-playground-api
- cp asm.sh build.sh run.sh test.sh wasm.sh rust-playground-api/
- cp gist.sh asm-gist.sh build-gist.sh run-gist.sh test-gist.sh wasm-gist.sh rust-playground-api/
- chmod 755 rust-playground-api/rust-playground-api
artifacts:
name: rust-playground-api-debug-linux-x86_64
Expand All @@ -60,7 +60,7 @@ release-linux-x86_64:
- RUST_BACKTRACE=full cargo build --release --verbose
- mkdir -p rust-playground-api
- cp target/release/main rust-playground-api/rust-playground-api
- cp asm.sh build.sh run.sh test.sh wasm.sh rust-playground-api/
- cp gist.sh asm-gist.sh build-gist.sh run-gist.sh test-gist.sh wasm-gist.sh rust-playground-api/
- strip -s rust-playground-api/rust-playground-api
- chmod 755 rust-playground-api/rust-playground-api
artifacts:
Expand Down
16 changes: 11 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ ENV TZ=Etc/UTC \

WORKDIR ./rust-playground-api

COPY ./Cargo.toml ./Cargo.toml

ADD . ./
ADD ./src ./src

RUN cargo build --release

RUN rm src/*.rs || true; \
Expand Down Expand Up @@ -42,11 +47,12 @@ RUN groupadd $APP_USER && \
mkdir -p ${APP}

COPY --from=build /rust-playground-api/target/release/main ${APP}/main
COPY --from=build /rust-playground-api/asm.sh ${APP}/
COPY --from=build /rust-playground-api/build.sh ${APP}/
COPY --from=build /rust-playground-api/run.sh ${APP}/
COPY --from=build /rust-playground-api/test.sh ${APP}/
COPY --from=build /rust-playground-api/wasm.sh ${APP}/
COPY --from=build /rust-playground-api/gist.sh ${APP}/
COPY --from=build /rust-playground-api/asm-gist.sh ${APP}/
COPY --from=build /rust-playground-api/build-gist.sh ${APP}/
COPY --from=build /rust-playground-api/run-gist.sh ${APP}/
COPY --from=build /rust-playground-api/test-gist.sh ${APP}/
COPY --from=build /rust-playground-api/wasm-gist.sh ${APP}/
COPY --from=build /rust-playground-api/src/example1/main.rs ${APP}/src/example1/main.rs

RUN chown -R $APP_USER:$APP_USER ${APP}
Expand Down
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

---

v0.0.1 - "Hello, world! version" - 2021 by `Jeremy Carter <[email protected]>`
v0.1.0 - 2021 by `Jeremy Carter <[email protected]>`

GitLab: [https://gitlab.com/defcronyke/rust-playground-api](https://gitlab.com/defcronyke/rust-playground-api)
GitHub: [https://github.com/defcronyke/rust-playground-api](https://github.com/defcronyke/rust-playground-api)
Expand Down Expand Up @@ -60,17 +60,38 @@ cd rust-playground-api

---

## Usage

1. Make a GitHub `secret` or `public` Gist, with your desired `Rust` program code in it:
[https://gist.github.com](https://gist.github.com)

2. Get the Gist's ID from its URL, for example the `id` value as below:
https://gist.github.com/{username}/{id}

3. Run the `Rust` code by using the `id` in your GET request, for example:
[http://localhost:8080/?id=1ea016619193533f9ac6cd1d8ae22d58](http://localhost:8080/?id=1ea016619193533f9ac6cd1d8ae22d58)

---

## API routes available

- Run the code: GET / [http://localhost:8080](http://localhost:8080)
_Use your desired "GitHub Gist ID" for the `id` GET query parameter in the example requests below. The `Rust` code in that Gist will be used for
the query._

- Run the code:
GET /?id=1ea016619193533f9ac6cd1d8ae22d58 [http://localhost:8080/?id=1ea016619193533f9ac6cd1d8ae22d58](http://localhost:8080/?id=1ea016619193533f9ac6cd1d8ae22d58)

- Run the test suite: GET /test [http://localhost:8080/test](http://localhost:8080/test)
- Run the test suite:
GET /test?id=1ea016619193533f9ac6cd1d8ae22d58 [http://localhost:8080/test?id=1ea016619193533f9ac6cd1d8ae22d58](http://localhost:8080/test?id=1ea016619193533f9ac6cd1d8ae22d58)

- Build the code: GET /build [http://localhost:8080/build](http://localhost:8080/build)
- Build the code:
GET /build?id=1ea016619193533f9ac6cd1d8ae22d58 [http://localhost:8080/build?id=1ea016619193533f9ac6cd1d8ae22d58](http://localhost:8080/build?id=1ea016619193533f9ac6cd1d8ae22d58)

- Build and output WebAssembly of the code: GET /wasm [http://localhost:8080/wasm](http://localhost:8080/wasm)
- Build and output WebAssembly of the code:
GET /wasm?id=1ea016619193533f9ac6cd1d8ae22d58 [http://localhost:8080/wasm?id=1ea016619193533f9ac6cd1d8ae22d58](http://localhost:8080/wasm?id=1ea016619193533f9ac6cd1d8ae22d58)

- Build and output Assembly of the code: GET /asm [http://localhost:8080/asm](http://localhost:8080/asm)
- Build and output Assembly of the code:
GET /asm?id=1ea016619193533f9ac6cd1d8ae22d58 [http://localhost:8080/asm?id=1ea016619193533f9ac6cd1d8ae22d58](http://localhost:8080/asm?id=1ea016619193533f9ac6cd1d8ae22d58)

---

Expand Down
20 changes: 20 additions & 0 deletions asm-gist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# Depends on the 'jq' tool.

rust_playground_api_asm_gist() {
input_str="$@"

if [ $# -eq 0 ]; then
input_str="1ea016619193533f9ac6cd1d8ae22d58"
fi

input_str2="$(printf '%s' "$(./gist.sh "$input_str")")"
input_str2="${input_str2%\"}"
input_str2="${input_str2#\"}"

input_param="$(printf '%b' "$input_str2"|jq -sRr @uri)"

curl -sL -H 'Content-Type: application/json' -X POST -d '{"channel":"nightly","mode":"debug","edition":"2018","crateType":"bin","tests":false,"code":"'"$input_str2"'","target":"asm","assemblyFlavor":"att","demangleAssembly":"demangle","processAssembly":"filter","backtrace":false}' -H "Referrer: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&code=$input_param" https://play.rust-lang.org/compile
}

rust_playground_api_asm_gist "$@"
20 changes: 20 additions & 0 deletions build-gist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# Depends on the 'jq' tool.

rust_playground_api_build_gist() {
input_str="$@"

if [ $# -eq 0 ]; then
input_str="1ea016619193533f9ac6cd1d8ae22d58"
fi

input_str2="$(printf '%s' "$(./gist.sh "$input_str")")"
input_str2="${input_str2%\"}"
input_str2="${input_str2#\"}"

input_param="$(printf '%b' "$input_str2"|jq -sRr @uri)"

curl -sL -H 'Content-Type: application/json' -X POST -d '{"channel":"nightly","mode":"debug","edition":"2018","crateType":"lib","tests":false,"code":"'"$input_str2"'","backtrace":false}' -H "Referrer: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&code=$input_param" https://play.rust-lang.org/execute
}

rust_playground_api_build_gist "$@"
13 changes: 13 additions & 0 deletions gist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
# Depends on the 'jq' tool.
#
# Example: ./gist.sh 1ea016619193533f9ac6cd1d8ae22d58

rust_playground_api_gist() {
input_str="$@"

curl -sL -H 'Accept: application/vnd.github.v3+json.raw' "https://api.github.com/gists/$input_str" | \
jq '.files | to_entries | .[0].value.content'
}

rust_playground_api_gist "$@"
20 changes: 20 additions & 0 deletions run-gist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
# Depends on the 'jq' tool.

rust_playground_api_run_gist() {
input_str="$@"

if [ $# -eq 0 ]; then
input_str="1ea016619193533f9ac6cd1d8ae22d58"
fi

input_str2="$(printf '%s' "$(./gist.sh "$input_str")")"
input_str2="${input_str2%\"}"
input_str2="${input_str2#\"}"

input_param="$(printf '%b' "$input_str2"|jq -sRr @uri)"

curl -sL -H 'Content-Type: application/json' -X POST -d '{"channel":"nightly","mode":"debug","edition":"2018","crateType":"bin","tests":false,"code":"'"$input_str2"'","backtrace":false}' -H "Referrer: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&code=$input_param" https://play.rust-lang.org/execute
}

rust_playground_api_run_gist "$@"
Loading

0 comments on commit 3cfecb7

Please sign in to comment.