Initial commit

This commit is contained in:
Daniel Flanagan 2024-01-05 00:45:43 -06:00
commit 7694035e31
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
8 changed files with 2544 additions and 0 deletions

3
.cargo/config.toml Normal file
View file

@ -0,0 +1,3 @@
[env]
DATABASE_URL = "sqlite://./fam.db"
RUST_LOG = "trace"

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
/.direnv
*.db

2305
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

17
Cargo.toml Normal file
View file

@ -0,0 +1,17 @@
[package]
name = "homeman"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
axum = "0.7.3"
chrono = "0.4.31"
maud = { git = "https://github.com/lambda-fairy/maud.git", rev="320add8", features = ["axum"] }
serde = { version = "1.0.194", features = ["derive"] }
sqlx = { version = "0.7.3", features = ["chrono", "sqlx-sqlite", "sqlite", "runtime-tokio"] }
tokio = { version = "1.35.1", features = ["net", "full"] }
tower-http = { version = "0.5.0", features = ["fs", "trace"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

27
flake.lock Normal file
View file

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1701718080,
"narHash": "sha256-6ovz0pG76dE0P170pmmZex1wWcQoeiomUZGggfH9XPs=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2c7f3c0fb7c08a0814627611d9d7d45ab6d75335",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "2c7f3c0fb7c08a0814627611d9d7d45ab6d75335",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

28
flake.nix Normal file
View file

@ -0,0 +1,28 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs?rev=2c7f3c0fb7c08a0814627611d9d7d45ab6d75335";
outputs = {
self,
nixpkgs,
}: let
inherit (self) outputs;
supportedSystems = ["x86_64-linux"];
forEachSupportedSystem = nixpkgs.lib.genAttrs supportedSystems;
in {
devShells = forEachSupportedSystem (system: let
pkgs = import nixpkgs {inherit system;};
in {
rust-dev = pkgs.mkShell {
buildInputs = with pkgs; [
sqlite
cargo
rustc
rustfmt
rustPackages.clippy
rust-analyzer
];
};
default = outputs.devShells.${system}.rust-dev;
});
};
}

160
src/main.rs Normal file
View file

@ -0,0 +1,160 @@
use std::sync::Arc;
use axum::{extract::State, routing::get, Router};
use chrono::prelude::*;
use maud::{html, Markup, DOCTYPE};
use sqlx::{Pool, Sqlite};
use tower_http::services::ServeDir;
use tracing::{debug, info, instrument, trace};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
struct AppState {
dbpool: Pool<Sqlite>,
}
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "example_static_file_server=debug,tower_http=debug".into()),
)
.with(tracing_subscriber::fmt::layer())
.init();
let dbpool = sqlx::SqlitePool::connect("fam.db").await.unwrap();
// let countries = sqlx::query!(
// "
// SELECT country, COUNT(*) as count
// FROM users
// GROUP BY country
// WHERE organization = ?
// ",
// organization
// )
// .fetch_all(&dbpool) // -> Vec<{ country: String, count: i64 }>
// .await?;
let _ = sqlx::query!(
r#"
create table if not exists 'family_members' (avatar_url text not null, name text not null);
create table if not exists 'todo' (family_member_name text, 'text' text, done_at datetime);
"#
)
.fetch_all(&dbpool)
.await
.unwrap();
// let countries = sqlx::query!(
// "
// SELECT country, COUNT(*) as count
// FROM users
// GROUP BY country
// WHERE organization = ?
// ",
// organization
// )
// .fetch_all(&dbpool) // -> Vec<{ country: String, count: i64 }>
// .await?;
let state = Arc::new(AppState { dbpool });
debug!("Debug showing");
trace!("Trace showing");
// build our application with a route
let app = Router::new()
.with_state(state)
.route("/", get(hello_world))
.route("/admin", get(admin))
.nest_service("/assets", ServeDir::new("assets"));
let bind = "0.0.0.0:3000";
let listener = tokio::net::TcpListener::bind(bind).await.unwrap();
info!("Listening on {bind}");
axum::serve(listener, app).await.unwrap();
}
#[derive(sqlx::FromRow)]
struct FamilyMember {
avatar_url: String,
name: String,
}
#[derive(sqlx::FromRow)]
struct Todo {
emoji: char,
text: String,
done_at: Option<DateTime<Local>>,
}
fn head() -> Markup {
html! {
(DOCTYPE)
head {
meta charset="utf-8" {}
meta name="viewport" content="width=device-width, initial-scale=1" {}
link rel="stylesheet" href="/assets/style.css" {}
// TODO: add htmx
title { "Homeman" }
}
}
}
#[instrument]
async fn hello_world() -> Markup {
trace!("Hello world page accessed");
html! {
(head())
body {
h1 class="title" { "Flanagan Family" }
h1 class="title" { "1:05pm" }
h1 class="title" { "To-Do" }
}
}
}
async fn admin(State(state): State<AppState>) -> Markup {
// query data and show it
// let countries = sqlx::query!(
// "
// SELECT country, COUNT(*) as count
// FROM users
// GROUP BY country
// WHERE organization = ?
// ",
// organization
// )
// .fetch_all(&dbpool) // -> Vec<{ country: String, count: i64 }>
// .await?;
trace!("Hello world page accessed");
let family_members: Vec<FamilyMember> = sqlx::query_as!(
FamilyMember,
"select name, avatar_url from 'family_members';"
)
.fetch_all(&state.dbpool)
.await
.unwrap();
html! {
(head())
body {
h1 class="title" { "Flanagan Family ADMIN" }
h1 class="title" { "Members" }
ul {
@for fam in &family_members {
li { (fam.name) }
}
}
form hx-post="/admin/new-list" {
label {
"Name"
input id="name" name="name" type="text" placeholder="John Smith" {}
}
button type="submit" {"Add Member"}
}
}
}
}
async fn add_todo_list() {}