add simple documentation

This commit is contained in:
Milim 2024-12-07 10:38:16 +01:00
parent 307419d37f
commit 0e50b214ed
No known key found for this signature in database
4 changed files with 19 additions and 2 deletions

View file

@ -1,14 +1,17 @@
use actix_web::{get, web, Scope}; 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) web::scope("/b").service(book_view).service(chapter_view)
} }
/// route to view info for a specific book
#[get("/{book}")] #[get("/{book}")]
async fn book_view(book: web::Path<String>) -> String { async fn book_view(book: web::Path<String>) -> String {
format!("This is the info for {book}") format!("This is the info for {book}")
} }
/// view for reading a chapter
#[get("/{book}/{chapter})")] #[get("/{book}/{chapter})")]
async fn chapter_view(path: web::Path<(String, String)>) -> String { async fn chapter_view(path: web::Path<(String, String)>) -> String {
format!("This is {} of {}", path.0, path.1) format!("This is {} of {}", path.0, path.1)

View file

@ -7,7 +7,9 @@ use std::net::{IpAddr, Ipv4Addr};
#[derive(Deserialize, Serialize, Clone, Copy)] #[derive(Deserialize, Serialize, Clone, Copy)]
pub struct Config { pub struct Config {
/// Ip address that the gunnhildr should bind to
pub binding_ip: IpAddr, pub binding_ip: IpAddr,
/// Port that gunnhildr should listen on
pub port: u16, pub port: u16,
} }
@ -20,6 +22,7 @@ impl Default for Config {
} }
} }
/// Parse and merge all config sources
pub fn parse_config() -> Config { pub fn parse_config() -> Config {
Figment::from(Serialized::defaults(Config::default())) Figment::from(Serialized::defaults(Config::default()))
.merge(Env::prefixed("HILDR")) .merge(Env::prefixed("HILDR"))

View file

@ -2,14 +2,19 @@
use log::{info, warn}; use log::{info, warn};
use sqlx::{migrate::MigrateDatabase, PgPool, Postgres, Sqlite, SqlitePool}; use sqlx::{migrate::MigrateDatabase, PgPool, Postgres, Sqlite, SqlitePool};
/// Utility for interacting with the database
#[derive(Clone)] #[derive(Clone)]
pub enum DataBase { pub enum DataBase {
/// Used for Sqlite database
Sqlite(sqlx::Pool<Sqlite>), Sqlite(sqlx::Pool<Sqlite>),
/// Used for Postgres database
Postgres(sqlx::Pool<Postgres>), Postgres(sqlx::Pool<Postgres>),
} }
impl DataBase { impl DataBase {
/// Database backed by SQLite
pub async fn sqlite() -> Self { pub async fn sqlite() -> Self {
// Check if db exists, if not create it.
if !sqlx::Sqlite::database_exists("sqlite:gunnhildr.db") if !sqlx::Sqlite::database_exists("sqlite:gunnhildr.db")
.await .await
.expect("failed to connect to db") .expect("failed to connect to db")
@ -23,6 +28,7 @@ impl DataBase {
let pool = SqlitePool::connect("sqlite:gunnhildr.db").await.unwrap(); let pool = SqlitePool::connect("sqlite:gunnhildr.db").await.unwrap();
// run migrations
sqlx::migrate!("migrations/sqlite") sqlx::migrate!("migrations/sqlite")
.run(&pool) .run(&pool)
.await .await
@ -32,7 +38,9 @@ impl DataBase {
Self::Sqlite(pool) Self::Sqlite(pool)
} }
/// Database backed by Postgres
pub async fn postgres(url: &str) -> Self { pub async fn postgres(url: &str) -> Self {
// check if database exists and create one if not
if !sqlx::Postgres::database_exists(url) if !sqlx::Postgres::database_exists(url)
.await .await
.expect("failed to connect to db") .expect("failed to connect to db")
@ -45,6 +53,8 @@ impl DataBase {
} }
let pool = PgPool::connect("url").await.unwrap(); let pool = PgPool::connect("url").await.unwrap();
// run migrations
sqlx::migrate!("migrations/postgres") sqlx::migrate!("migrations/postgres")
.run(&pool) .run(&pool)
.await .await

View file

@ -7,6 +7,7 @@ mod db;
#[actix_web::main] #[actix_web::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> Result<(), std::io::Error> {
// init env logger
env_logger::builder() env_logger::builder()
.filter_level(log::LevelFilter::Info) .filter_level(log::LevelFilter::Info)
.init(); .init();
@ -19,7 +20,7 @@ async fn main() -> Result<(), std::io::Error> {
App::new() App::new()
.app_data(config) .app_data(config)
.app_data(db.clone()) .app_data(db.clone())
.service(books::book_scope()) .service(books::reading_scope())
.service(hello) .service(hello)
}) })
.bind((config.binding_ip, config.port))? .bind((config.binding_ip, config.port))?