diff --git a/auth/.env b/auth/.env new file mode 100644 index 0000000..f8272d9 --- /dev/null +++ b/auth/.env @@ -0,0 +1 @@ +PORT=5052 \ No newline at end of file diff --git a/auth/Cargo.toml b/auth/Cargo.toml new file mode 100644 index 0000000..be380e3 --- /dev/null +++ b/auth/Cargo.toml @@ -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" \ No newline at end of file diff --git a/auth/src/lib.rs b/auth/src/lib.rs new file mode 100644 index 0000000..ce433f0 --- /dev/null +++ b/auth/src/lib.rs @@ -0,0 +1,2 @@ +pub mod server; +pub mod types; \ No newline at end of file diff --git a/auth/src/main.rs b/auth/src/main.rs new file mode 100644 index 0000000..f1269ad --- /dev/null +++ b/auth/src/main.rs @@ -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 + 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::().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(()) +} \ No newline at end of file diff --git a/auth/src/server.rs b/auth/src/server.rs new file mode 100644 index 0000000..794b2ba --- /dev/null +++ b/auth/src/server.rs @@ -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) + } +} \ No newline at end of file diff --git a/auth/src/types.rs b/auth/src/types.rs new file mode 100644 index 0000000..a4478c0 --- /dev/null +++ b/auth/src/types.rs @@ -0,0 +1,5 @@ + +#[tarpc::service] +pub trait RealmAuth { + async fn test(name: String) -> String; +} \ No newline at end of file diff --git a/server/Cargo.toml b/server/Cargo.toml index 1c23f79..672fc30 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -9,9 +9,9 @@ futures = "0.3" tarpc = { version = "0.34.0", features = ["full"] } tokio = { version = "1.0", features = ["macros", "net", "rt-multi-thread"] } tracing = "0.1.40" -clap = { version = "4.5.7", features = ["derive"] } serde = { version = "1.0.203", features = ["derive"] } emojis = "0.6.2" chrono = { version = "0.4.24", features = ["serde"] } sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "mysql", "chrono" ] } -dotenvy = "0.15" \ No newline at end of file +dotenvy = "0.15" +realm_auth = { path = "../auth" } \ No newline at end of file