From 71f93377940e79e0c64cfce0eaf4f052976a12e7 Mon Sep 17 00:00:00 2001 From: liyunze <50455574+liyunze-coding@users.noreply.github.com> Date: Mon, 10 Nov 2025 15:12:21 +1100 Subject: [PATCH] dockerized successfully --- Dockerfile | 36 ++++++++++++++++++ docker-compose.yml | 5 +++ src/commands.rs | 2 +- src/commands/weather.rs | 58 ----------------------------- src/main.rs | 2 +- test.json | 82 ----------------------------------------- 6 files changed, 43 insertions(+), 142 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml delete mode 100644 src/commands/weather.rs delete mode 100644 test.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6acb427 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,36 @@ +FROM rust:trixie AS builder + +WORKDIR /usr/src/dsec_bot + +# Copy manifest files first to leverage Docker layer caching for dependencies +COPY Cargo.toml Cargo.lock ./ + +# Create a dummy main to cache dependency compilation +RUN mkdir -p src && echo 'fn main() {}' > src/main.rs + +# Build dependencies (this layer will be cached) +RUN cargo build --release + +# Remove dummy and copy real source +RUN rm -rf src +COPY . . + +# Build the actual application +RUN cargo build --release + +# Use a small image for the final build +FROM debian:bookworm-slim + +# Set the working directory +WORKDIR /usr/local/bin + +# Install CA certificates for TLS/HTTPS support +RUN apt-get update && \ + apt-get install -y --no-install-recommends ca-certificates && \ + rm -rf /var/lib/apt/lists/* + +# Copy the application binary from the builder stage +COPY --from=builder /usr/src/dsec_bot/target/release/dsec_bot . + +# Specify the command to run the application +CMD ["./dsec_bot"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ebb4f14 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,5 @@ +services: + dsec_bot: + build: . + env_file: + - .env \ No newline at end of file diff --git a/src/commands.rs b/src/commands.rs index 17c5417..068e3c9 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,3 +1,3 @@ // this file is to let main.rs know the existence of the "commands" folder pub mod info; -pub mod weather; +// pub mod weather; diff --git a/src/commands/weather.rs b/src/commands/weather.rs deleted file mode 100644 index 13ea229..0000000 --- a/src/commands/weather.rs +++ /dev/null @@ -1,58 +0,0 @@ -use crate::{Context, Error}; -use poise::CreateReply; -use serenity::{all::CreateEmbed, json::Value}; - -async fn get_weather(location: String) -> Result { - let weather_api_key = std::env::var("WEATHER_TOKEN").expect("missing WEATHER_TOKEN"); - - let request_url = format!( - "https://api.weatherapi.com/v1/current.json?key={key}&q={location}", - key = weather_api_key, - location = location - ); - - // retrieve weather data - let response = reqwest::get(request_url).await?.text().await?; - - // process weather data - - Ok(response) -} - -/// Shows weather information -#[poise::command(slash_command)] -pub async fn weather( - ctx: Context<'_>, - #[description = "Location (City or Country)"] location: String, -) -> Result<(), Error> { - let weather_response = get_weather(location).await?; - let value: Value = serde_json::from_str(&weather_response)?; - - let location_name = (&value["location"]["name"]).as_str().unwrap(); - let location_region = (&value["location"]["region"]).as_str().unwrap(); - let location_country = (&value["location"]["country"]).as_str().unwrap(); - - let weather_condition = (&value["current"]["condition"]["text"]).as_str().unwrap(); - let weather_temp = &value["current"]["temp_c"]; - let weather_feels_like = &value["current"]["feelslike_c"]; - let weather_wind_kph = &value["current"]["wind_kph"]; - let weather_humidity = &value["current"]["humidity"]; - let weather_cloud = &value["current"]["cloud"]; - - let weather_icon = (&value["current"]["condition"]["icon"]).as_str().unwrap(); - - let embed = CreateEmbed::new() - .field("Name", format!("{}", location_name), true) - .field("Region", format!("{}", location_region), true) - .field("Country", format!("{}", location_country), true) - .field("Condition", format!("{}", weather_condition), true) - .field("Temperature", format!("{} °C", weather_temp), true) - .field("Feels like", format!("{} °C", weather_feels_like), true) - .field("wind", format!("{} kph", weather_wind_kph), true) - .field("humidity", format!("{}%", weather_humidity), true) - .field("cloud", format!("{}%", weather_cloud), true) - .thumbnail(format!("https:{}", weather_icon)); - - ctx.send(CreateReply::default().embed(embed)).await?; - Ok(()) -} diff --git a/src/main.rs b/src/main.rs index fecf1d9..2da79f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ async fn main() { commands::info::ping(), commands::info::userinfo(), commands::info::serverinfo(), - commands::weather::weather(), + // commands::weather::weather(), ], ..Default::default() }) diff --git a/test.json b/test.json deleted file mode 100644 index ab01383..0000000 --- a/test.json +++ /dev/null @@ -1,82 +0,0 @@ -[ - { - "location": { - "name": "Melbourne", - "region": "Victoria", - "country": "Australia", - "lat": -37.8167, - "lon": 144.9667, - "tz_id": "Australia/Melbourne", - "localtime_epoch": 1762687144, - "localtime": "2025-11-09 22:19" - }, - "current": { - "last_updated_epoch": 1762686900, - "last_updated": "2025-11-09 22:15", - "temp_c": 12.3, - "is_day": 0, - "condition": { - "text": "Partly cloudy", - "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png", - "code": 1003 - }, - "wind_mph": 5.4, - "wind_kph": 8.6, - "wind_degree": 263, - "wind_dir": "W", - "pressure_mb": 1017.0, - "pressure_in": 30.03, - "precip_mm": 0.03, - "precip_in": 0.0, - "humidity": 71, - "cloud": 75, - "feelslike_c": 11.6, - "feelslike_f": 52.9, - "vis_km": 10.0, - "vis_miles": 6.0, - "uv": 0.0, - "gust_mph": 9.4, - "gust_kph": 15.1 - } - }, - { - "location": { - "name": "Canberra", - "region": "Australian Capital Territory", - "country": "Australia", - "lat": -35.283, - "lon": 149.217, - "tz_id": "Australia/Sydney", - "localtime_epoch": 1762687326, - "localtime": "2025-11-09 22:22" - }, - "current": { - "last_updated_epoch": 1762686900, - "last_updated": "2025-11-09 22:15", - "temp_c": 12.3, - "is_day": 0, - "condition": { - "text": "Partly cloudy", - "icon": "//cdn.weatherapi.com/weather/64x64/night/116.png", - "code": 1003 - }, - "wind_mph": 4.9, - "wind_kph": 7.9, - "wind_degree": 74, - "wind_dir": "ENE", - "pressure_mb": 1015.0, - "pressure_in": 29.97, - "precip_mm": 0.0, - "precip_in": 0.0, - "humidity": 71, - "cloud": 50, - "feelslike_c": 11.7, - "feelslike_f": 53.1, - "vis_km": 10.0, - "vis_miles": 6.0, - "uv": 0.0, - "gust_mph": 7.5, - "gust_kph": 12.1 - } - } -]