This commit is contained in:
OMGeeky
2024-04-21 23:23:45 +02:00
commit e979cc13f2
7 changed files with 1449 additions and 0 deletions

1
.dockerignore Normal file
View File

@@ -0,0 +1 @@
target

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
/.idea

61
.run/Dockerfile.run.xml Normal file
View File

@@ -0,0 +1,61 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Dockerfile" type="docker-deploy" factoryName="dockerfile" server-name="Docker">
<deployment type="dockerfile">
<settings>
<option name="imageTag" value="twba-code-receiver" />
<option name="buildArgs">
<list>
<DockerEnvVarImpl>
<option name="name" value="PROGNAME" />
<option name="value" value="twba-code-receiver" />
</DockerEnvVarImpl>
</list>
</option>
<option name="buildKitEnabled" value="true" />
<option name="containerName" value="twba-code-receiver" />
<option name="envVars">
<list>
<DockerEnvVarImpl>
<option name="name" value="TWBA_CONFIG" />
<option name="value" value="/twba/configs/config.toml" />
</DockerEnvVarImpl>
</list>
</option>
<option name="portBindings">
<list>
<DockerPortBindingImpl>
<option name="containerPort" value="3000" />
<option name="hostPort" value="7443" />
</DockerPortBindingImpl>
</list>
</option>
<option name="commandLineOptions" value="--restart=unless-stopped" />
<option name="showCommandPreview" value="true" />
<option name="sourceFilePath" value="Dockerfile" />
<option name="volumeBindings">
<list>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/twba/configs/" />
<option name="hostPath" value="/twba/" />
<option name="readOnly" value="true" />
</DockerVolumeBindingImpl>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/twba/tmp/" />
<option name="hostPath" value="/var/tmp/twba/" />
</DockerVolumeBindingImpl>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/twba/data/" />
<option name="hostPath" value="/twba/data/" />
</DockerVolumeBindingImpl>
<DockerVolumeBindingImpl>
<option name="containerPath" value="/etc/ssl/certs" />
<option name="hostPath" value="/etc/ssl/certs" />
<option name="readOnly" value="true" />
</DockerVolumeBindingImpl>
</list>
</option>
</settings>
</deployment>
<method v="2" />
</configuration>
</component>

1247
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

15
Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "twba-code-receiver"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
twba-backup-config = { version = "0.1.3", git = "https://github.com/OMGeeky/backup_config.git" }
hyper = { version = "0.14", features = ["full", "server"] }
tokio = { version = "1", features = ["full"] }
url = "2.2.2"
lazy_static = "1.4"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

44
Dockerfile Normal file
View File

@@ -0,0 +1,44 @@
FROM lukemathwalker/cargo-chef:latest-rust-1 AS chef
WORKDIR /app
FROM chef AS planner
COPY /src ./src
COPY /Cargo.toml .
COPY /Cargo.lock .
RUN cargo chef prepare --recipe-path recipe.json
FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
# Build dependencies - this is the caching Docker layer!
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/home/root/app/target \
cargo chef cook --release --locked --recipe-path recipe.json
# Build application
COPY /src ./src
COPY /Cargo.toml .
COPY /Cargo.lock .
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/home/root/app/target \
cargo build --release --locked
# We do not need the Rust toolchain to run the binary!
FROM debian:bookworm AS runtime
WORKDIR /app
ARG PROGNAME
RUN apt-get update && apt-get install -y libssl-dev coreutils
# Create a script to run the command and sleep for one hour after the command is done
RUN echo "#!/bin/bash \n \
echo \"Running command: '$PROGNAME'\" \n \
# Run your command \n \
$PROGNAME \n \
echo \"Done with normal command. Sleeping for one hour\" \n \
# Sleep for one hour \n \
sleep 3600 \n \
echo \"Done with sleep. Exiting\" \
" > /app/entrypoint.sh
# Make the script executable
RUN chmod +x /app/entrypoint.sh
COPY --from=builder /app/target/release/$PROGNAME /usr/local/bin/$PROGNAME
CMD ["/app/entrypoint.sh"]

79
src/main.rs Normal file
View File

@@ -0,0 +1,79 @@
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use hyper::{Method, StatusCode};
use lazy_static::lazy_static;
use std::collections::HashMap;
use std::convert::Infallible;
use tokio::fs::write;
use tracing::{error, info, trace};
use twba_backup_config::Conf;
use url::Url;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_env_filter("warn,twba_code_receiver=trace,twba_backup_config=info")
.init();
let make_svc =
make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(handle_request)) });
let addr = ([0, 0, 0, 0], 3000).into();
let server = Server::bind(&addr).serve(make_svc);
if let Err(e) = server.await {
error!("server error: {}", e);
}
}
async fn handle_request(req: Request<Body>) -> Result<Response<Body>, Infallible> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/googleapi/auth") => auth_get(req).await,
other => {
error!("404: {:?} {:?}", other.0, other.1);
let mut not_found = Response::default();
*not_found.status_mut() = StatusCode::NOT_FOUND;
Ok(not_found)
}
}
}
async fn auth_get(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let url = format!("http://localhost{}", req.uri());
trace!("auth get request with url: '{}'", url);
let url = Url::parse(&url).unwrap();
let params: HashMap<_, _> = url.query_pairs().into_owned().collect();
if let Some(code) = params.get("code") {
info!("Code received: '{}'", code);
let write_res = write_to_file(code.to_string()).await;
match write_res {
Ok(_) => {
info!("Code written to file");
Ok(Response::new(Body::from("Code written to file")))
}
Err(e) => {
error!("Error writing code to file: {e:?}");
Ok(Response::new(Body::from(
"Error writing code to file: {e:?}",
)))
}
}
} else {
error!("No code provided");
Ok(Response::new(Body::from("No code provided")))
}
}
lazy_static! {
static ref CONF: Conf = twba_backup_config::get_default_builder()
.load()
.expect("Failed to load config");
static ref AUTH_CODE_PATH: String = CONF.google.path_auth_code.clone();
}
async fn write_to_file(code: String) -> std::io::Result<()> {
let path = AUTH_CODE_PATH.to_string();
println!("writing code '{}' to file: '{}'", code, path);
trace!("writing code '{}' to file: '{}'", code, path);
write(path, code).await
}