Start Auth Library
Auth will talk to pocketbase
This commit is contained in:
2
auth/src/lib.rs
Normal file
2
auth/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod server;
|
||||
pub mod types;
|
||||
45
auth/src/main.rs
Normal file
45
auth/src/main.rs
Normal 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
22
auth/src/server.rs
Normal 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
5
auth/src/types.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
#[tarpc::service]
|
||||
pub trait RealmAuth {
|
||||
async fn test(name: String) -> String;
|
||||
}
|
||||
Reference in New Issue
Block a user