Compare commits
2
Commits
master
..
0d1ce85853
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d1ce85853 | ||
|
|
57be975287 |
No files matched your search
Generated
+963
-401
File diff suppressed because it is too large.
Load diff
@@ -5,12 +5,3 @@ members = [
|
||||
"srv",
|
||||
"gui",
|
||||
]
|
||||
|
||||
# Enable max optimizations for dependencies, but not for our code:
|
||||
# Very helpful when developing in Slint.
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
|
||||
# Enable only a small amount of optimization in debug mode
|
||||
[profile.dev]
|
||||
opt-level = 1
|
||||
+2
-1
@@ -63,7 +63,8 @@ RUN wasm-pack build --release --target web
|
||||
FROM scratch
|
||||
|
||||
ENV RUST_LOG="info"
|
||||
ENV BIND="0.0.0.0:8000"
|
||||
ENV ROCKET_ADDRESS="0.0.0.0"
|
||||
ENV ROCKET_PORT="8000"
|
||||
|
||||
VOLUME ["/data"]
|
||||
ENV DB_PATH="/data/db.sqlite"
|
||||
|
||||
@@ -49,6 +49,7 @@ features = [
|
||||
"backend-android-activity-06",
|
||||
]
|
||||
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
tokio = { version = "1.48.0", default-features = false, features = ["fs", "rt-multi-thread"] }
|
||||
xdg = "3.0.0"
|
||||
|
||||
+1
-3
@@ -71,7 +71,7 @@ async fn put_category(
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let url: Url = format!(
|
||||
"{address}/api/favorites/{itemname}/category",
|
||||
"{address}/api/favourites/{itemname}/category",
|
||||
address = conf.lock().unwrap().address
|
||||
)
|
||||
.parse()?;
|
||||
@@ -93,7 +93,6 @@ fn update_slint_list(new_list: Vec<Item>, list: &VecModel<ui::SItem>) {
|
||||
name: item.name.to_shared_string(),
|
||||
checked: item.data.checked,
|
||||
amount: item.data.amount as i32,
|
||||
just_added: false,
|
||||
})
|
||||
.for_each(|item| list.push(item));
|
||||
}
|
||||
@@ -197,7 +196,6 @@ pub fn run() -> Result<(), anyhow::Error> {
|
||||
checked: false,
|
||||
name: item.clone(),
|
||||
amount: 1,
|
||||
just_added: true,
|
||||
});
|
||||
let conf = conf.clone();
|
||||
io.spawn(async move || {
|
||||
|
||||
+10
-29
@@ -113,23 +113,6 @@ component PlusMinus inherits HorizontalLayout {
|
||||
|
||||
component GoodListItemAdd inherits Rectangle {
|
||||
in-out property <SItem> item;
|
||||
// Animate when this item appears.
|
||||
transform-scale: item.just-added ? 90% : 100%;
|
||||
opacity: item.just-added ? 0 : 100%;
|
||||
init => {
|
||||
self.transform-scale = 100%;
|
||||
self.opacity = 100%;
|
||||
item.just-added = false;
|
||||
}
|
||||
animate transform-scale {
|
||||
duration: 500ms;
|
||||
easing: ease-out-bounce;
|
||||
}
|
||||
animate opacity {
|
||||
duration: 300ms;
|
||||
easing: ease-out;
|
||||
}
|
||||
|
||||
background: Palette.alternate-background;
|
||||
border-radius: 8pt;
|
||||
border-width: 10pt;
|
||||
@@ -186,7 +169,7 @@ component GoodsListAdd {
|
||||
|
||||
for item in items: VerticalLayout {
|
||||
GoodListItemAdd {
|
||||
item <=> item;
|
||||
item: item;
|
||||
}
|
||||
|
||||
Rectangle {
|
||||
@@ -203,21 +186,19 @@ component GoodButtons inherits ScrollView {
|
||||
|
||||
property <int> count-x: floor(root.width / (button-min-width + button-spacing));
|
||||
property <int> count-y: ceil(State.favorites.length / count-x);
|
||||
property <length> button-width: ((root.width + button-spacing) / count-x) - button-spacing;
|
||||
property <length> button-width: ((self.width + button-spacing) / count-x) - button-spacing;
|
||||
|
||||
content-height: (count-y * (button-height + button-spacing));
|
||||
mouse-drag-pan-enabled: true;
|
||||
vertical-scrollbar-policy: always-off;
|
||||
|
||||
FlexboxLayout {
|
||||
spacing: button-spacing;
|
||||
for item in State.favorites: Button {
|
||||
width: root.button-width;
|
||||
height: root.button-height;
|
||||
text: item.name;
|
||||
clicked => {
|
||||
State.add-to-list(item.name);
|
||||
}
|
||||
for item[idx] in State.favorites: Button {
|
||||
x: floor(mod(idx, count-x)) * (root.button-width + root.button-spacing);
|
||||
y: floor(idx / count-x) * (root.button-height + root.button-spacing);
|
||||
width: root.button-width;
|
||||
height: root.button-height;
|
||||
text: item.name;
|
||||
clicked => {
|
||||
State.add-to-list(item.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,6 @@ export struct SItem {
|
||||
name: string,
|
||||
checked: bool,
|
||||
amount: int,
|
||||
|
||||
/// Whether the item was just added by the user.
|
||||
/// Determines whether the add-animation plays.
|
||||
just_added: bool,
|
||||
}
|
||||
|
||||
export struct SButtonItem{
|
||||
|
||||
+1
-3
@@ -4,11 +4,9 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
rocket = { version = "0.5.1", features = ["json"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
inkopslista-lib = { path = "../lib" }
|
||||
sqlite = "0.37.0"
|
||||
clap = { version = "4.6.6", features = ["derive", "env"] }
|
||||
axum = { version = "0.8.9", features = ["http1"] }
|
||||
tower-http = { version = "0.7.0", features = ["fs", "trace", "compression-gzip"] }
|
||||
tokio = { version = "1.53.1", features = ["full"] }
|
||||
+56
-49
@@ -1,25 +1,32 @@
|
||||
use axum::extract::{Path, State};
|
||||
use axum::response::Html;
|
||||
use axum::routing::method_routing::{get, put};
|
||||
use axum::{Json, Router};
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
|
||||
use clap::Parser;
|
||||
use inkopslista_lib::{ButtonItem, Item, ItemData};
|
||||
use rocket::{
|
||||
State,
|
||||
fs::{FileServer, NamedFile},
|
||||
serde::json::Json,
|
||||
shield::Shield,
|
||||
};
|
||||
use sqlite::ConnectionThreadSafe;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use tower_http::services::ServeDir;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// The directory where the web files for the gui is stored.
|
||||
fn www_path() -> &'static std::path::Path {
|
||||
std::path::Path::new("./www")
|
||||
fn www_path() -> &'static Path {
|
||||
Path::new("./www")
|
||||
}
|
||||
|
||||
async fn index() -> Html<&'static str> {
|
||||
Html(include_str!("../../gui/index.html"))
|
||||
/// Serve `index.html` from [`www_path`].
|
||||
#[get("/")]
|
||||
async fn index() -> NamedFile {
|
||||
NamedFile::open(www_path().join("index.html"))
|
||||
.await
|
||||
.expect("index.html")
|
||||
}
|
||||
|
||||
async fn get_list(State(db): State<Arc<ConnectionThreadSafe>>) -> Json<Vec<Item>> {
|
||||
#[get("/api/list")]
|
||||
fn get_list(db: &State<ConnectionThreadSafe>) -> Json<Vec<Item>> {
|
||||
// TODO errorhandling
|
||||
let query = "SELECT * FROM list;";
|
||||
let list = db
|
||||
@@ -40,7 +47,8 @@ async fn get_list(State(db): State<Arc<ConnectionThreadSafe>>) -> Json<Vec<Item>
|
||||
Json(list)
|
||||
}
|
||||
|
||||
async fn get_favorites(State(db): State<Arc<ConnectionThreadSafe>>) -> Json<Vec<ButtonItem>> {
|
||||
#[get("/api/favorites")]
|
||||
fn get_favorites(db: &State<ConnectionThreadSafe>) -> Json<Vec<ButtonItem>> {
|
||||
let opt = Opt::parse();
|
||||
|
||||
let query = format!("WITH favorites as (SELECT itemname, COUNT(date_added) day_count
|
||||
@@ -67,30 +75,24 @@ async fn get_favorites(State(db): State<Arc<ConnectionThreadSafe>>) -> Json<Vec<
|
||||
Json(list)
|
||||
}
|
||||
|
||||
async fn put_category(
|
||||
State(db): State<Arc<ConnectionThreadSafe>>,
|
||||
Path(item): Path<String>,
|
||||
category: String,
|
||||
) {
|
||||
#[put("/api/favourites/<itemname>/category", data = "<category>")]
|
||||
fn put_category(db: &State<ConnectionThreadSafe>, itemname: &str, category: &str) {
|
||||
let query = "INSERT INTO categories VALUES (?, ?) ON CONFLICT DO UPDATE SET category = ?";
|
||||
let mut statement = db.prepare(query).unwrap();
|
||||
statement.bind((1, item.as_str())).unwrap();
|
||||
statement.bind((2, category.as_str())).unwrap();
|
||||
statement.bind((3, category.as_str())).unwrap();
|
||||
statement.bind((1, itemname)).unwrap();
|
||||
statement.bind((2, category)).unwrap();
|
||||
statement.bind((3, category)).unwrap();
|
||||
while let Ok(sqlite::State::Row) = statement.next() {}
|
||||
}
|
||||
|
||||
async fn put_list(
|
||||
State(db): State<Arc<ConnectionThreadSafe>>,
|
||||
Path(item): Path<String>,
|
||||
Json(data): Json<ItemData>,
|
||||
) {
|
||||
#[put("/api/list/<item>", data = "<data>")]
|
||||
fn put_list(item: &str, data: Json<ItemData>, db: &State<ConnectionThreadSafe>) {
|
||||
// TODO: errorhandling
|
||||
// TODO: transaction
|
||||
let query =
|
||||
"INSERT INTO list VALUES(?, ?, ?) ON CONFLICT DO UPDATE SET checked = ?, amount = ?;";
|
||||
let mut statement = db.prepare(query).unwrap();
|
||||
statement.bind((1, item.as_str())).unwrap();
|
||||
let data = data.into_inner();
|
||||
statement.bind((1, item)).unwrap();
|
||||
statement.bind((2, data.checked as i64)).unwrap();
|
||||
statement.bind((3, data.amount)).unwrap();
|
||||
statement.bind((4, data.checked as i64)).unwrap();
|
||||
@@ -98,45 +100,50 @@ async fn put_list(
|
||||
while let Ok(sqlite::State::Row) = statement.next() {}
|
||||
let query = "INSERT INTO adds VALUES(?, strftime('%s','now')) ON CONFLICT DO NOTHING";
|
||||
let mut statement = db.prepare(query).unwrap();
|
||||
statement.bind((1, item.as_str())).unwrap();
|
||||
statement.bind((1, item)).unwrap();
|
||||
while let Ok(sqlite::State::Row) = statement.next() {}
|
||||
}
|
||||
|
||||
async fn delete_item(State(db): State<Arc<ConnectionThreadSafe>>, Path(item): Path<String>) {
|
||||
#[delete("/api/list/<item>")]
|
||||
fn delete_item(item: &str, db: &State<ConnectionThreadSafe>) {
|
||||
// TODO: Eror handling
|
||||
let query = "DELETE FROM list WHERE itemname = ?";
|
||||
let mut statement = db.prepare(query).unwrap();
|
||||
statement.bind((1, item.as_str())).unwrap();
|
||||
statement.bind((1, item)).unwrap();
|
||||
while let Ok(sqlite::State::Row) = statement.next() {}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Opt {
|
||||
#[clap(long, env = "BIND", default_value = "127.0.0.1:8000")]
|
||||
bind: SocketAddr,
|
||||
#[clap(long, env = "DB_PATH")]
|
||||
db_path: PathBuf,
|
||||
#[clap(long, env = "NUM_DAYS", default_value = "5")]
|
||||
num_days: u32,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
let opt = Opt::parse();
|
||||
let db = sqlite::Connection::open_thread_safe(opt.db_path).unwrap();
|
||||
db.execute(include_str!("db/init.sql"))
|
||||
.expect("unable to initialize database");
|
||||
let db = Arc::new(db);
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/api/list", get(get_list))
|
||||
.route("/api/list/{item}", put(put_list).delete(delete_item))
|
||||
.route("/api/favorites", get(get_favorites))
|
||||
.route("/api/favorites/{item}/category", put(put_category))
|
||||
.fallback_service(ServeDir::new(www_path()))
|
||||
.with_state(db);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(opt.bind).await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
rocket::build()
|
||||
.attach(
|
||||
Shield::default()
|
||||
// Allow content to be served through iframes
|
||||
.disable::<rocket::shield::Frame>(),
|
||||
)
|
||||
.manage(db)
|
||||
.mount(
|
||||
"/",
|
||||
routes![
|
||||
index,
|
||||
get_list,
|
||||
put_list,
|
||||
delete_item,
|
||||
get_favorites,
|
||||
put_category
|
||||
],
|
||||
)
|
||||
.mount("/", FileServer::from(www_path()))
|
||||
}
|
||||
Reference in new issue
Block a user