feat(server): started friend & reworked deps

This commit is contained in:
Maieul BOYER 2026-02-08 19:35:09 +01:00
parent bd8135e2ad
commit 83433186f3
Signed by: maix
SSH key fingerprint: SHA256:iqCzqFFF5KjRixmDExqbAltCIj9ndlBWIGJf3t9Ln9g
7 changed files with 328 additions and 7 deletions

View file

@ -5,7 +5,16 @@ edition = "2024"
publish = false
[dependencies]
axum = "0.8.8"
axum = { version = "0.8.8", features = ["macros"] }
axum-extra = { version = "0.12.5", features = ["routing"] }
tower-http = {version = "0.6.2", features = ["trace"] }
tracing-subscriber = { version = "0.3.19", features=["env-filter"] }
serde = { workspace = true }
smol_str = { workspace = true }
tokio = { workspace = true }
minijinja = { workspace = true }
tracing = {workspace = true }
log = {workspace = true }
froxy-templates = { workspace = true }
froxy-scraper = { workspace = true }

71
server/src/friends.rs Normal file
View file

@ -0,0 +1,71 @@
/*
{ f_front.py
/friends
}
{ f_friends.py
/friends/add/<add>
/friends/remove/<remove>
/friends/set_relation/<who>/<int:relation>
}
*/
use axum::extract::{Path, State};
use axum::routing::get;
use axum::{Router, http::StatusCode, response::Html};
use axum_extra::routing::RouterExt;
#[derive(serde::Deserialize, Debug, Clone)]
struct FriendList(#[serde(deserialize_with = "comma_list_deser")] Vec<String>);
fn comma_list_deser<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::Deserialize;
let s = String::deserialize(deserializer)?;
Ok(s.split(',')
.map(|v| v.to_string())
.filter(|s| !s.is_empty())
.collect())
}
pub fn router() -> Router<crate::GlobalState> {
Router::<crate::GlobalState>::new()
.route_with_tsr("/friends", get(friend_page))
.route_with_tsr("/friends/add/{names}", get(add_friend))
.route_with_tsr("/friends/remove/{names}", get(remove_friend))
.route_with_tsr("/friends/set_relation/{who}/{relation}", get(set_relation))
}
#[axum::debug_handler]
async fn friend_page(State(state): State<crate::GlobalState>) -> Html<String> {
log::info!("Serving friends_page");
Html(String::new())
}
#[axum::debug_handler]
async fn add_friend(
State(state): State<crate::GlobalState>,
Path((friends,)): Path<(FriendList,)>,
) -> StatusCode {
log::debug!("adding friends: {friends:?}");
StatusCode::OK
}
#[axum::debug_handler]
async fn remove_friend(
State(state): State<crate::GlobalState>,
Path((friends,)): Path<(FriendList,)>,
) -> StatusCode {
log::debug!("removing friends: {friends:?}");
StatusCode::OK
}
#[axum::debug_handler]
pub async fn set_relation(
State(state): State<crate::GlobalState>,
Path((friend, relation)): Path<(String, i32)>,
) -> StatusCode {
log::debug!("setting relation for {friend} to {relation}");
StatusCode::OK
}

View file

@ -1,4 +1,79 @@
/*
{ f_front.py
[ SEND FROM STATIC DIRECTORY
/static/<path>
/favicon.ico
/manifest.json
/service_worker.json
/apple-touch-icon.png
]
/
/settings
/profile/{login}
/search/<keyword>/<int:friends_only>
/friends
}
{ f_friends.py
/friends/add/<add>
/friends/remove/<remove>
/friends/set_relation/<who>/<int:relation>
}
{ f_issues.py
/addissue/<pc>/<int:issue_type>
}
{ f_locations.py
/goto/<pos>
}
{ f_users.py
/getuser/<login>
/settings/profile [POST]
}
*/
use std::sync::Arc;
use axum::Router;
use log::info;
use tower_http::trace::TraceLayer;
use tracing_subscriber::EnvFilter;
mod friends;
#[derive(Clone, Debug)]
struct GlobalState {
templates: Arc<minijinja::Environment<'static>>,
}
#[tokio::main]
async fn main() {
println!("Hello, world!");
tracing_subscriber::fmt()
// This allows you to use, e.g., `RUST_LOG=info` or `RUST_LOG=debug`
// when running the app to set log levels.
.with_env_filter(
EnvFilter::try_from_default_env()
.or_else(|_| EnvFilter::try_new("froxy_server=error,tower_http=warn"))
.unwrap(),
)
.init();
let mut env = minijinja::Environment::new();
froxy_templates::friends::add_to_context(&mut env);
let global_state = GlobalState {
templates: Arc::new(env),
};
let app = Router::new()
.merge(friends::router())
.with_state(global_state)
.layer(TraceLayer::new_for_http());
let (ip, port) = ("0.0.0.0", 3000);
info!("Starting server on {ip}:{port}");
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind((ip,port)).await.unwrap();
axum::serve(listener, app).await.unwrap();
}