mirror of
https://github.com/hulthe/inkopslista.git
synced 2026-09-18 18:03:39 +02:00
Compare commits
10
Commits
axum
..
e34c857a62
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e34c857a62 | ||
|
|
766d740018 | ||
|
|
7cc77d41a2 | ||
|
|
8515872469 | ||
|
|
fe37e3721a | ||
|
|
1e2dbcc25b | ||
|
|
cc65586b98 | ||
|
|
1635ded760 | ||
|
|
525db95867 | ||
|
|
e4e1c49922 |
@@ -0,0 +1,29 @@
|
||||
name: APK Builder
|
||||
run-name: Build Android APK
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
build-apk:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: docker.nubo.sh/inkopslista-builder:latest
|
||||
strategy:
|
||||
matrix:
|
||||
arch: [aarch64, x86_64]
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- run: echo "SHA_SHORT=$(echo ${{ gitea.sha }} | head -c 8)" >> $GITEA_ENV
|
||||
- run: echo "OUT_NAME=inkopslista.${{ env.SHA_SHORT }}.${{ matrix.arch }}.apk" >> $GITEA_ENV
|
||||
|
||||
- run: cargo apk build --lib --release -p inkopslista --target ${{ matrix.arch }}-linux-android
|
||||
- run: mv "$CARGO_TARGET_DIR/release/apk/inkopslista.apk" "$OUT_NAME"
|
||||
|
||||
- uses: https://gitea.com/actions/gitea-upload-artifact@v4
|
||||
with:
|
||||
name : "${{ env.OUT_NAME }}"
|
||||
path: "${{ env.OUT_NAME }}"
|
||||
if-no-files-found: error
|
||||
overwrite: true
|
||||
retention-days: 1 # TODO
|
||||
@@ -0,0 +1,23 @@
|
||||
name: Gitea Actions Demo
|
||||
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
Explore-Gitea-Actions:
|
||||
runs-on: ubuntu-latest
|
||||
container:
|
||||
image: docker.nubo.sh/inkopslista-builder:latest
|
||||
steps:
|
||||
- run: cargo --version
|
||||
- run: rustup target list --installed
|
||||
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
|
||||
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
|
||||
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4
|
||||
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
|
||||
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
|
||||
- name: List files in the repository
|
||||
run: |
|
||||
ls ${{ gitea.workspace }}
|
||||
- run: echo "🍏 This job's status is ${{ job.status }}."
|
||||
Generated
+719
-184
File diff suppressed because it is too large
Load Diff
+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"
|
||||
|
||||
+1
-1
@@ -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()?;
|
||||
|
||||
+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