Skip to content

Commit

Permalink
update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cycle-five committed Jul 5, 2024
1 parent be5ecda commit 65086c2
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crack-voting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ categories = ["multimedia::audio"]
homepage = "https://cracktun.es/"
# The official main repo is sr.ht, this is needed for the CI/CD pipeline.
#repository = "https://git.sr.ht/~cycle-five/cracktunes"
#repository = "https://github.com/cycle-five/cracktunes/crack-voting"
repository = "https://github.com/cycle-five/cracktunes"
workspace = ".."

Expand All @@ -29,3 +28,4 @@ serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
tokio = { workspace = true }
tracing = { workspace = true }
sqlx = { workspace = true }
25 changes: 19 additions & 6 deletions crack-voting/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ RUN apk add --no-cache build-base musl-dev openssl-dev openssl
WORKDIR /app

# Copy over the Cargo.toml files to the shell project
COPY . .
# COPY Cargo.toml ./Cargo.lock crack-voting ./
COPY ./Cargo.toml ./Cargo.lock ./
RUN mkdir -p /app/crack-{voting,bf,core,gpt,osint}
RUN mkdir -p /app/cracktunes
COPY ./crack-voting/Cargo.toml ./crack-voting/
COPY ./crack-bf/Cargo.toml ./crack-bf/
COPY ./crack-core/Cargo.toml ./crack-core/
COPY ./crack-gpt/Cargo.toml ./crack-gpt/
COPY ./crack-osint/Cargo.toml ./crack-osint/
COPY ./cracktunes/Cargo.toml ./cracktunes/

# # Build and cache the dependencies
# RUN mkdir -p crack-voting/src && echo "fn main() {}" > crack-voting/src/main.rs
# RUN cargo fetch
# RUN cargo build -p crack-voting --release
# RUN rm crack-voting/src/main.rs
RUN mkdir -p crack-voting/src && echo "fn main() {}" > crack-voting/src/main.rs
RUN mkdir -p cracktunes/src && echo "fn main() {}" > cracktunes/src/main.rs
RUN mkdir -p crack-bf/src && echo "" > crack-bf/src/lib.rs
RUN mkdir -p crack-core/src && echo "" > crack-core/src/lib.rs
RUN mkdir -p crack-gpt/src && echo "" > crack-gpt/src/lib.rs
RUN mkdir -p crack-osint/src && echo "" > crack-osint/src/lib.rs
RUN cargo fetch
RUN cargo build -p crack-voting --release
RUN rm crack-voting/src/main.rs
COPY . .

# Copy the actual code files and build the application
# COPY ./crack-voting/src ./crack-voting/
Expand Down
45 changes: 39 additions & 6 deletions crack-voting/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use dbl::types::Webhook;
use lazy_static::lazy_static;
use std::env;
use warp::body::BodyDeserializeError;
use warp::http::StatusCode;
use warp::path;
use warp::{reject, Filter, Rejection, Reply};
use warp::{body::BodyDeserializeError, http::StatusCode, path, reject, Filter, Rejection, Reply};

#[derive(Debug, serde::Deserialize, serde::Serialize, Clone, PartialEq, Eq)]
/// NewClass for the Webhook to store in the database.
#[derive(Debug, serde::Deserialize, serde::Serialize, sqlx::FromRow, Clone, PartialEq, Eq)]
pub struct CrackedWebhook {
webhook: Webhook,
created_at: chrono::DateTime<chrono::Utc>,
Expand All @@ -27,7 +25,8 @@ impl std::fmt::Display for Unauthorized {
impl std::error::Error for Unauthorized {}

lazy_static! {
static ref WEBHOOK_SECRET: String = env::var("WEBHOOK_SECRET").expect("missing secret");
static ref WEBHOOK_SECRET: String =
env::var("WEBHOOK_SECRET").unwrap_or("missing secret".to_string());
}

/// Get the webhook secret from the environment.
Expand Down Expand Up @@ -86,3 +85,37 @@ async fn custom_error(err: Rejection) -> Result<impl Reply, Rejection> {
Err(err)
}
}

#[cfg(test)]
mod test {
use super::*;
use warp::http::StatusCode;

#[tokio::test]
async fn test_bad_req() {
let res = warp::test::request()
.method("POST")
.path("/dbl/webhook")
.reply(&get_webhook())
.await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn test_authorized() {
let res = warp::test::request()
.method("POST")
.path("/dbl/webhook")
.header("authorization", get_secret())
.json(&Webhook {
bot: dbl::types::BotId(1),
user: dbl::types::UserId(3),
kind: dbl::types::WebhookType::Test,
is_weekend: false,
query: Some("test".to_string()),
})
.reply(&get_webhook())
.await;
assert_eq!(res.status(), StatusCode::OK);
}
}
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ services:
- ./.env:/app/.env:ro
environment:
- DATABASE_URL=postgresql://postgres:mysecretpassword@crack_postgres:5432/postgres
- WEBHOOK_SECRET=${WEBHOOK_SECRET:-asdfasdf}
links:
- crack_postgres
ports:
Expand Down
2 changes: 1 addition & 1 deletion scripts/lint_test_build.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ cargo +nightly fmt --all -- --check --profile=$PROFILE
cargo +nightly clippy --profile=$PROFILE --all -- -D clippy::all -D warnings
cargo +nightly test --profile=$PROFILE
cargo +nightly tarpaulin --profile=$PROFILE --verbose --workspace --timeout 120 --out xml
cargo +nightly build --profile=$PROFILE
cargo +nightly build --profile=$PROFILE --workspace
17 changes: 6 additions & 11 deletions scripts/lint_test_build.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#!/bin/sh
cargo +nightly fmt --all -- --check
cargo +nightly clippy --all -- -D clippy::all -D warnings
RES1=$?
cargo tarpaulin --verbose --workspace --timeout 120 --out xml
RES2=$?
export PROFILE=release
cargo +nightly fmt --all -- --check --profile=$PROFILE
cargo +nightly clippy --profile=$PROFILE --workspace -- -D clippy::all -D warnings
cargo +nightly test --profile=$PROFILE --workspace
cargo +nightly tarpaulin --profile=$PROFILE --verbose --workspace --timeout 120 --out xml
cargo +nightly build --profile=$PROFILE --workspace

if [ ${RES1} = 0 ] && [ ${RES2} = 0 ]; then
echo "Building..."
else
echo "Something broke, still building..."
fi
cargo build --profile=release-with-debug
4 changes: 4 additions & 0 deletions scripts/lint_test_build_crack_voting.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
cargo clippy -p crack-voting --release -- -D clippy::all -D warnings
cargo test -p crack-voting --release
cargo build -p crack-voting --release

0 comments on commit 65086c2

Please sign in to comment.