more work on identity->chat hello/handshake

This commit is contained in:
sam 2024-01-18 21:44:53 +01:00
parent 1e53661b0a
commit 041531e88a
12 changed files with 96 additions and 40 deletions

21
chat/src/http/hello.rs Normal file
View file

@ -0,0 +1,21 @@
use std::sync::Arc;
use axum::{Extension, Json};
use foxchat::{
http::ApiError,
s2s::http::{HelloRequest, HelloResponse, NodeResponse}, fed,
};
use crate::{app_state::AppState, model::instance::Instance};
pub async fn post_hello(
Extension(state): Extension<Arc<AppState>>,
Json(data): Json<HelloRequest>,
) -> Result<Json<HelloResponse>, ApiError> {
let instance = Instance::get(&state.pool).await?;
let node = fed::get::<NodeResponse>(&instance.private_key, &state.config.domain, &data.host, "/_fox/ident/node", None).await?;
// TODO: validate identity server's signature, probably adapt FoxRequestData.from_request_parts() (or extract it into a separate function? not sure if that's possible though)
todo!()
}

18
chat/src/http/mod.rs Normal file
View file

@ -0,0 +1,18 @@
mod hello;
use crate::{app_state::AppState, config::Config};
use axum::{routing::post, Extension, Router};
use sqlx::{Pool, Postgres};
use std::sync::Arc;
use tower_http::trace::TraceLayer;
pub fn new(pool: Pool<Postgres>, config: Config) -> Router {
let app_state = Arc::new(AppState { pool, config });
let app = Router::new()
.route("/_fox/chat/hello", post(hello::post_hello))
.layer(TraceLayer::new_for_http())
.layer(Extension(app_state));
return app;
}

View file

@ -1,12 +1,15 @@
mod app_state;
mod config;
mod db;
mod fed;
mod app_state;
mod http;
mod model;
use crate::config::Config;
use clap::{Parser, Subcommand};
use eyre::Result;
use std::net::{Ipv4Addr, SocketAddrV4};
use tokio::net::TcpListener;
use tracing::info;
#[derive(Debug, Parser)]
@ -61,5 +64,12 @@ async fn main_web(config: Config) -> Result<()> {
db::init_instance(&pool).await?;
info!("Initialized instance data!");
let port = config.port;
let app = http::new(pool, config);
let listener = TcpListener::bind(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port)).await?;
axum::serve(listener, app).await?;
Ok(())
}