diff --git a/src/books/mod.rs b/src/books/mod.rs index c333300..8fbd08c 100644 --- a/src/books/mod.rs +++ b/src/books/mod.rs @@ -1,14 +1,17 @@ use actix_web::{get, web, Scope}; -pub fn book_scope() -> Scope { +/// scope to handle all reading related pages +pub fn reading_scope() -> Scope { web::scope("/b").service(book_view).service(chapter_view) } +/// route to view info for a specific book #[get("/{book}")] async fn book_view(book: web::Path) -> String { format!("This is the info for {book}") } +/// view for reading a chapter #[get("/{book}/{chapter})")] async fn chapter_view(path: web::Path<(String, String)>) -> String { format!("This is {} of {}", path.0, path.1) diff --git a/src/config.rs b/src/config.rs index 4e74963..f5dc496 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,9 @@ use std::net::{IpAddr, Ipv4Addr}; #[derive(Deserialize, Serialize, Clone, Copy)] pub struct Config { + /// Ip address that the gunnhildr should bind to pub binding_ip: IpAddr, + /// Port that gunnhildr should listen on pub port: u16, } @@ -20,6 +22,7 @@ impl Default for Config { } } +/// Parse and merge all config sources pub fn parse_config() -> Config { Figment::from(Serialized::defaults(Config::default())) .merge(Env::prefixed("HILDR")) diff --git a/src/db/mod.rs b/src/db/mod.rs index e4c4b70..44214b8 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,14 +2,19 @@ use log::{info, warn}; use sqlx::{migrate::MigrateDatabase, PgPool, Postgres, Sqlite, SqlitePool}; +/// Utility for interacting with the database #[derive(Clone)] pub enum DataBase { + /// Used for Sqlite database Sqlite(sqlx::Pool), + /// Used for Postgres database Postgres(sqlx::Pool), } impl DataBase { + /// Database backed by SQLite pub async fn sqlite() -> Self { + // Check if db exists, if not create it. if !sqlx::Sqlite::database_exists("sqlite:gunnhildr.db") .await .expect("failed to connect to db") @@ -23,6 +28,7 @@ impl DataBase { let pool = SqlitePool::connect("sqlite:gunnhildr.db").await.unwrap(); + // run migrations sqlx::migrate!("migrations/sqlite") .run(&pool) .await @@ -32,7 +38,9 @@ impl DataBase { Self::Sqlite(pool) } + /// Database backed by Postgres pub async fn postgres(url: &str) -> Self { + // check if database exists and create one if not if !sqlx::Postgres::database_exists(url) .await .expect("failed to connect to db") @@ -45,6 +53,8 @@ impl DataBase { } let pool = PgPool::connect("url").await.unwrap(); + + // run migrations sqlx::migrate!("migrations/postgres") .run(&pool) .await diff --git a/src/main.rs b/src/main.rs index 39eafd1..6019417 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod db; #[actix_web::main] async fn main() -> Result<(), std::io::Error> { + // init env logger env_logger::builder() .filter_level(log::LevelFilter::Info) .init(); @@ -19,7 +20,7 @@ async fn main() -> Result<(), std::io::Error> { App::new() .app_data(config) .app_data(db.clone()) - .service(books::book_scope()) + .service(books::reading_scope()) .service(hello) }) .bind((config.binding_ip, config.port))?