Start Auth Library

Auth will talk to pocketbase
This commit is contained in:
2024-06-29 21:45:05 -04:00
Unverified
parent 818bec067f
commit 58b38f3d2d
7 changed files with 91 additions and 2 deletions

45
auth/src/main.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::env;
use std::future::Future;
use std::net::{IpAddr, Ipv6Addr};
use dotenvy::dotenv;
use futures::{future, StreamExt};
use tarpc::server::{BaseChannel, Channel};
use tarpc::server::incoming::Incoming;
use tarpc::tokio_serde::formats::Json;
use realm_auth::server::RealmAuthServer;
use realm_auth::types::RealmAuth;
async fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
tokio::spawn(fut);
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv().ok();
let server_addr = (IpAddr::V6(Ipv6Addr::LOCALHOST), env::var("PORT").expect("PORT must be set").parse::<u16>().unwrap());
// JSON transport is provided by the json_transport tarpc module. It makes it easy
// to start up a serde-powered json serialization strategy over TCP.
let mut listener = tarpc::serde_transport::tcp::listen(&server_addr, Json::default).await?;
tracing::info!("Listening on port {}", listener.local_addr().port());
listener.config_mut().max_frame_length(usize::MAX);
listener
// Ignore accept errors.
.filter_map(|r| future::ready(r.ok()))
.map(BaseChannel::with_defaults)
// Limit channels to 1 per IP.
.max_channels_per_key(1, |t| t.transport().peer_addr().unwrap().ip())
// serve is generated by the service attribute. It takes as input any type implementing
// the generated World trait.
.map(|channel| {
let server = RealmAuthServer::new(channel.transport().peer_addr().unwrap());
channel.execute(server.serve()).for_each(spawn)
})
// Max 10 channels.
.buffer_unordered(10)
.for_each(|_| async {})
.await;
Ok(())
}