diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a24bc5e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +.idea/ +target/ diff --git a/.github/workflows/publish-to-gpr.yml b/.github/workflows/publish-to-gpr.yml new file mode 100644 index 0000000..9cd7025 --- /dev/null +++ b/.github/workflows/publish-to-gpr.yml @@ -0,0 +1,22 @@ +name: Publish Docker image to GitHub Package Registry +on: push +jobs: + build: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/master' # Only on master + + steps: + - name: Copy Repo Files + uses: actions/checkout@master + + #This Action Emits 2 Variables, IMAGE_SHA_NAME and IMAGE_URL + #which you can reference in subsequent steps + - name: Publish Docker Image to GPR + uses: machine-learning-apps/gpr-docker-publish@master + id: docker + with: + IMAGE_NAME: 'discord-vocodes' + DOCKERFILE_PATH: 'Dockerfile' + BUILD_CONTEXT: '.' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fe953a9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +# Unsure if I want to create minimal images yet that run under Alpine Linux. +# I have instructions for that here: https://hub.docker.com/r/echelon/testcontainer/dockerfile +FROM ubuntu:xenial as build +WORKDIR /tmp +RUN apt-get update \ + && apt-get install -y \ + build-essential \ + curl \ + libsodium-dev \ + libssl-dev \ + libssl1.0.0 \ + pkg-config \ + unzip \ + wget +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \ + | sh -s -- --default-toolchain stable -y + +COPY Cargo.lock . +COPY Cargo.toml . +COPY src/ ./src +COPY script/ ./script + +RUN $HOME/.cargo/bin/cargo fetch + +#RUN $HOME/.cargo/bin/cargo build +RUN $HOME/.cargo/bin/cargo build --release + +# Final image +FROM ubuntu:xenial +WORKDIR / +# SSL certs are required to make requests +COPY --from=build /etc/ssl /etc/ssl +# Shared libs are necessary +COPY --from=build /lib/x86_64-linux-gnu/libssl.so.1.0.0 /lib/x86_64-linux-gnu +COPY --from=build /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 /lib/x86_64-linux-gnu +#COPY --from=build /tmp/target/debug/discord-vocodes / +COPY --from=build /tmp/target/release/discord-vocodes / +COPY --from=build /tmp/script / + +CMD LD_LIBRARY_PATH=/usr/lib /discord-vocodes + diff --git a/script/launch.sh b/script/launch.sh new file mode 100755 index 0000000..8ea1a9f --- /dev/null +++ b/script/launch.sh @@ -0,0 +1,13 @@ +#!/bin/sh + +# We can't run this as a CronJob since it isn't guaranteed to be scheduled +# to the same nodes. (Maybe there's a way for Kubernetes to do that?) + +mkdir -p "${DOWNLOAD_DIR}" +mkdir -p "${TEMP_DIR}" + +while :; do + ./discord-vocodes + echo "Sleeping for ${SLEEP_TIMEOUT} seconds at $(date)" + sleep "${SLEEP_TIMEOUT}" +done diff --git a/src/vocodes_api.rs b/src/vocodes_api.rs new file mode 100644 index 0000000..4cda4cd --- /dev/null +++ b/src/vocodes_api.rs @@ -0,0 +1,36 @@ +use hyper::Body; +use hyper::Client as HyperClient; +use hyper::Request; +use hyper_tls::HttpsConnector; + +const VOCODES_SPEAK_ENDPOINT : &'static str = "http://mumble.stream/speak"; + +pub async fn fetch(text: &str, speaker: &str) -> Result, Box> { + let mut request = format!("{{\"speaker\": \"{}\", \"text\": \"{}\"}}", speaker, text); + + println!("Req: {}", request); + + let https = HttpsConnector::new(); + let client = HyperClient::builder() + .build::<_, hyper::Body>(https); + + let req = Request::builder() + .uri(VOCODES_SPEAK_ENDPOINT) + .method("POST") + .header("Origin", "https://vo.codes") + .header("User-Agent", "Vocodes Discord Bot") + .header("Connection", "keep-alive") + .header("Content-Type", "application/json") + .body(Body::from(request))?; + + let resp = client.request(req).await?; + + println!("Status: {}", resp.status()); + + let bytes = hyper::body::to_bytes(resp.into_body()).await?; + let result = bytes.to_vec(); + + println!("Length: {}", result.len()); + + Ok(result) +}