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

1
auth/.env Normal file
View File

@@ -0,0 +1 @@
PORT=5052

14
auth/Cargo.toml Normal file
View File

@@ -0,0 +1,14 @@
[package]
name = "realm_auth"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0"
futures = "0.3"
tarpc = { version = "0.34.0", features = ["full"] }
tokio = { version = "1.0", features = ["macros", "net", "rt-multi-thread"] }
tracing = "0.1.40"
serde = { version = "1.0.203", features = ["derive"] }
chrono = { version = "0.4.24", features = ["serde"] }
dotenvy = "0.15"

2
auth/src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod server;
pub mod types;

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(())
}

22
auth/src/server.rs Normal file
View File

@@ -0,0 +1,22 @@
use std::net::SocketAddr;
use tarpc::context::Context;
use crate::types::RealmAuth;
#[derive(Clone)]
pub struct RealmAuthServer {
pub socket: SocketAddr,
}
impl RealmAuthServer {
pub fn new(socket: SocketAddr) -> RealmAuthServer {
RealmAuthServer {
socket,
}
}
}
impl RealmAuth for RealmAuthServer {
async fn test(self, context: Context, name: String) -> String {
format!("Hello {}", name)
}
}

5
auth/src/types.rs Normal file
View File

@@ -0,0 +1,5 @@
#[tarpc::service]
pub trait RealmAuth {
async fn test(name: String) -> String;
}