Skip to content

Commit

Permalink
Move a bunch of types to the common crate.
Browse files Browse the repository at this point in the history
Various other fixes/improvements I forgot to commit.
  • Loading branch information
izzyhub committed Nov 26, 2024
1 parent cd275db commit d163c16
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 124 deletions.
478 changes: 403 additions & 75 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
#botifactory-common = { path = "../botifactory-common" }
botifactory-common = { git = "https://github.com/izzyhub/botifactory-common" }

axum = "0.7.5"
config = "0.14.0"
config = "0.14.1"
secrecy = "0.8.0"
serde = "1.0.210"
serde = "1.0.215"
serde-aux = "4.5.0"
sqlx = { version = "0.8.2", features = [
"runtime-tokio-native-tls",
"sqlite",
"time",
] }
sqlx = { version = "0.8.2", features = ["sqlite", "time", "runtime-tokio"] }
rusqlite = "=0.32.1"
tokio = { version = "1.40.0", features = ["full"] }
tracing = "0.1.40"
Expand All @@ -34,7 +33,6 @@ tokio-util = { version = "0.7.12", features = ["io"] }
sha2 = "0.10.8"
axum_typed_multipart = "0.13.2"
tempfile = "3.14.0"

[dev-dependencies]
claim = "0.5"
quickcheck = "1.0.3"
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo install sqlx-cli --features sqlite
RUN cargo build --release --bin botifactory
RUN sqlx database setup

FROM debian:bullseye-slim AS runtime
WORKDIR /app
Expand Down
2 changes: 1 addition & 1 deletion configuration/base.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ log_level = "trace"
host = "0.0.0.0"
release_path = "./releases"
[database]
database_url = "sqlite://develop.db"
url = "file://develop.db"
log_level = "trace"
2 changes: 1 addition & 1 deletion configuration/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ log_level = "trace"
host = "127.0.0.1"
release_path = "./releases"
[database]
database_url = "sqlite://develop.db"
url = "file://develop.db"
log_level = "trace"
2 changes: 1 addition & 1 deletion configuration/production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ log_level = "error"
host = "0.0.0.0"
release_path = "/releases"
[database]
database_url = "sqlite://botifactory.db"
url = "file://botifactory.db"
log_level = "error"
16 changes: 9 additions & 7 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,24 @@ where

#[derive(Debug, serde::Deserialize, PartialEq, Eq)]
pub struct DatabaseSettings {
pub database_url: String,
pub url: String,
#[serde(deserialize_with = "deserialize_log_level")]
pub log_level: LevelFilter,
}

impl Default for DatabaseSettings {
fn default() -> Self {
DatabaseSettings {
database_url: "sqlite://botifactory.db".to_string(),
url: "file://botifactory.db".to_string(),
log_level: LevelFilter::INFO,
}
}
}

impl DatabaseSettings {
pub fn without_db(&self) -> SqliteConnectOptions {
SqliteConnectOptions::from_str(self.database_url.as_str())
println!("w/o db database_url: {}", self.url.as_str());
SqliteConnectOptions::from_str(self.url.as_str())
.expect("Failed to parse databse url")
.journal_mode(SqliteJournalMode::Wal)
.create_if_missing(true)
Expand All @@ -75,8 +76,8 @@ impl DatabaseSettings {
}

pub fn with_db(&self) -> SqliteConnectOptions {
let options = self.without_db().filename(&self.database_url);
options
println!("w/ db database_url: {}", self.url.as_str());
self.without_db().filename(&self.url)
}
}

Expand Down Expand Up @@ -108,8 +109,9 @@ pub fn get_configuration() -> Result<Settings, config::ConfigError> {
)
.add_source(
config::Environment::with_prefix("BOTIFACTORY_APP")
.try_parsing(true)
.separator("_")
//.try_parsing(true)
.convert_case(config::Case::Snake)
.separator("__")
.list_separator(" "),
)
.build()?
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
println!("reading configuration");
let configuration = get_configuration().expect("Failed to read configuration");
println!("read configuration");

let subscriber = get_log_subscriber(
"botifactory".into(),
configuration.application.log_level,
std::io::stdout,
);
init_subscriber(subscriber);
println!("created log sbuscriber");

println!("creating sqlite connection");
let db_pool = sqlx::SqlitePool::connect_with(configuration.database.with_db()).await?;
println!("connected to sqlite");
sqlx::migrate!("./migrations").run(&db_pool).await?;
println!("Ran migrations");

tracing_subscriber::fmt::init();
//tracing_subscriber::fmt::init();
println!("Hello, world!");

run(db_pool, configuration).await;
Expand Down
20 changes: 5 additions & 15 deletions src/routes/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,30 @@ use crate::configuration::Settings;
use crate::routes::error::{APIError, Result};
use axum::extract::Path;
use axum::extract::State;
use botifactory_common::{ChannelJson as Channel, CreateChannel};
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use std::fs::create_dir_all;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Serialize, Deserialize)]
pub struct Channel {
pub id: i64,
pub name: String,
pub project_id: i64,
pub created_at: i64,
pub updated_at: i64,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ChannelBody {
channel: Channel,
}

#[derive(Serialize, Deserialize)]
struct CreateChannel {
channel_name: String,
}

pub fn router() -> Router<(SqlitePool, Arc<Settings>)> {
Router::new()
.route(
"/project/:project_name/:channel_name",
get(show_project_channel),
)
.route("/channel/:channel_id", get(show_channel_by_id))
.route("/project/:project_name/new", post(create_project_channel))
.route(
"/project/:project_name/channel/new",
post(create_project_channel),
)
}

pub async fn show_project_channel(
Expand Down
18 changes: 4 additions & 14 deletions src/routes/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,22 @@ use crate::configuration::Settings;
use crate::routes::error::{APIError, Result};
use axum::extract::Path;
use axum::extract::State;
use botifactory_common::{CreateProject, ProjectJson as Project};
use sqlx::{FromRow, SqlitePool};

Check failure on line 13 in src/routes/project.rs

View workflow job for this annotation

GitHub Actions / Test

unused import: `FromRow`
use std::fs::create_dir_all;
use std::path::PathBuf;
use std::sync::Arc;

#[derive(Serialize, Deserialize, FromRow)]
pub struct Project {
pub id: i64,
pub name: String,
pub created_at: i64,
pub updated_at: i64,
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct ProjectBody {
project: Project,
}

#[derive(Serialize, Deserialize)]
struct CreateProject {
name: String,
}

pub fn router() -> Router<(SqlitePool, Arc<Settings>)> {
Router::new()
.route("/project/:name/", get(show_project))
.route("/project/:id/", get(show_project_by_id))
//.route("/project/:id/", get(show_project_by_id))
.route("/project/new", post(create_project))
}
pub async fn show_project(
Expand All @@ -62,6 +50,7 @@ pub async fn show_project(
Ok(Json(ProjectBody { project }))
}

/*
pub async fn show_project_by_id(
Path(project_id): Path<i64>,
State((db, _settings)): State<(SqlitePool, Arc<Settings>)>,
Expand All @@ -84,6 +73,7 @@ pub async fn show_project_by_id(
Ok(Json(ProjectBody { project }))
}
*/

pub async fn create_project(
State((db, settings)): State<(SqlitePool, Arc<Settings>)>,
Expand Down
1 change: 0 additions & 1 deletion src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::configuration::Settings;
use crate::routes::*;

pub async fn run(db_pool: SqlitePool, settings: Settings) {
tracing_subscriber::fmt::init();
println!("Hello, world!");
let address = format!(
"{}:{}",
Expand Down

0 comments on commit d163c16

Please sign in to comment.