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};
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>) -> 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)

View file

@ -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"))

View file

@ -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<Sqlite>),
/// Used for Postgres database
Postgres(sqlx::Pool<Postgres>),
}
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

View file

@ -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))?