Skip to content

Commit b268b93

Browse files
committed
Docker works now, it didn't before because im stupid
1 parent 5675d86 commit b268b93

File tree

7 files changed

+41
-53
lines changed

7 files changed

+41
-53
lines changed

Dockerfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
FROM rust:latest AS builder
2-
WORKDIR /home/bot
2+
WORKDIR /bot
33

4-
COPY . .
4+
COPY Cargo.toml .
5+
COPY Cargo.lock .
6+
COPY .sqlx ./.sqlx
7+
COPY src ./src
58
RUN cargo build --release
69

710
FROM debian:bookworm
11+
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
812
WORKDIR /bot
9-
COPY --from=builder /home/bot/target/release/Trackr .
10-
CMD ["sudo ./Trackr"]
13+
COPY config.json .
14+
COPY --from=builder /bot/target/release/Trackr .
15+
CMD ["./Trackr"]

src/commands.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
33
#[allow(dead_code)]
44
const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
55

6-
use crate::utils::agencies::get as get_agency;
7-
86
/// Show this help menu
97
#[poise::command(prefix_command, track_edits, slash_command, category = "Misc")]
108
pub async fn help(
@@ -70,34 +68,4 @@ pub async fn support(ctx: Context<'_>) -> Result<(), Error> {
7068
)
7169
.await?;
7270
Ok(())
73-
}
74-
75-
/// Track a transit agency
76-
#[poise::command(prefix_command, track_edits, slash_command, category = "Tracking")]
77-
pub async fn track(
78-
ctx: Context<'_>,
79-
#[description = "The name of the transit agency to check the status of"] agency_name: String,
80-
) -> Result<(), Error> {
81-
let agency_name_upper = agency_name.to_uppercase();
82-
let Some(agency) = get_agency(agency_name_upper) else {
83-
poise::send_reply(
84-
ctx,
85-
poise::CreateReply::default()
86-
.ephemeral(false)
87-
.content(format!("Could not find agency `{}`", agency_name)),
88-
)
89-
.await?;
90-
return Ok(());
91-
};
92-
poise::send_reply(
93-
ctx,
94-
poise::CreateReply::default()
95-
.ephemeral(false)
96-
.content(format!(
97-
"Pretending to track {}\nVisit them at {}",
98-
agency.name, agency.url
99-
)),
100-
)
101-
.await?;
102-
Ok(())
103-
}
71+
}

src/dev_commands.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::{Context, Error};
2-
use sqlx;
32
use poise::CreateReply;
3+
use sqlx::{self, query};
4+
use tracing_subscriber::fmt::format;
5+
use sqlx::Row;
46

57
/// Top level command for development commands. Owner only
68
#[poise::command(
@@ -43,14 +45,25 @@ pub async fn stop(ctx: Context<'_>) -> Result<(), Error> {
4345
hide_in_help,
4446
category = "Dev"
4547
)]
46-
pub async fn sql(ctx: Context<'_>, query: String) -> Result<(), Error> {
47-
sqlx::query(
48-
query.as_str()
49-
)
50-
.execute(&ctx.data().db.pool)
51-
.await
52-
.map_err(|e| format!("Query execution failed: {}", e))?;
53-
48+
pub async fn sql(ctx: Context<'_>, #[rest] query: String) -> Result<(), Error> {
49+
let result = "";
50+
// todo for this: write structs for all db tables
51+
if query.to_uppercase().contains("SELECT"){
52+
let r = sqlx::query(
53+
query.as_str()
54+
)
55+
.fetch_one(&ctx.data().db.pool)
56+
.await
57+
.map_err(|e| format!("Query execution failed: {}", e))?;
58+
} else {
59+
sqlx::query(
60+
query.as_str()
61+
)
62+
.execute(&ctx.data().db.pool)
63+
.await
64+
.map_err(|e| format!("Query execution failed: {}", e))?;
65+
}
66+
poise::send_reply(ctx, poise::CreateReply::default().content(result)).await?;
5467
Ok(())
5568
}
5669

src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ async fn main() {
4949
// this is a list of all commands from the commands.rs file
5050
commands: vec![
5151
commands::help(), // Public commands \/
52-
commands::track(),
5352
commands::about(),
5453
commands::support(),
5554
dev_commands::dev(), // Dev commands \/
5655
dev_commands::track(),
57-
], // dev only commands (subcommands are NOT included because then they would be runnable on their own)
56+
],
5857
prefix_options: poise::PrefixFrameworkOptions {
5958
prefix: Some(config.prefix.clone()),
6059
// tracks messages that are edited within the last hour
@@ -106,7 +105,7 @@ async fn main() {
106105
println!("Logged in as {}", _ready.user.name);
107106
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
108107
Ok(Data {
109-
db: Client::new().await?,
108+
db: Client::new().await.expect("Failed to connect to database"),
110109
config,
111110
})
112111
})

src/utils/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Client {
1010
impl Client {
1111
pub async fn new() -> Result<Self, sqlx::Error> {
1212
let secrets = get_secrets();
13-
let pool = PgPool::connect(&secrets.database_url).await?;
13+
let pool = PgPool::connect(&secrets.database_url).await.expect("Could not connect to PostgreSQL. Check the database URL");
1414
Ok(Self { pool })
1515
}
1616

@@ -34,7 +34,7 @@ impl Client {
3434
)
3535
.fetch_one(&self.pool)
3636
.await?;
37-
37+
3838
Ok(user.id)
3939
}
4040
}

src/utils/db/schema.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ CREATE TABLE agencies (
2929
short_name VARCHAR(50) NOT NULL,
3030
long_name VARCHAR(100) NOT NULL,
3131
api_url TEXT NOT NULL,
32+
key_required BOOLEAN DEFAULT TRUE,
3233
key_env_name VARCHAR(100) NOT NULL,
34+
auth_header_name VARCHAR(100) NOT NULL,
3335
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
3436
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
3537
);

src/utils/secrets.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct Secrets {
1010
pub mbta_api_key: String,
1111
}
1212
pub fn get() -> Secrets {
13-
let file = File::open("secrets.json").expect("file should open read only"); // TODO: convert to env because docker
14-
return serde_json::from_reader(file).expect("file should be proper JSON");
13+
let secrets_path: String = std::env::var("SECRETS").expect("No SECRETS environment variable declared");
14+
let file = File::open(secrets_path).expect("Secrets file, as declared by the SECRETS environment variable, was not found");
15+
return serde_json::from_reader(file).expect("Secrets file should be proper JSON");
1516
}

0 commit comments

Comments
 (0)