diff --git a/server/.env b/server/.env index 9fa7b5a..7d738f3 100644 --- a/server/.env +++ b/server/.env @@ -1 +1,3 @@ -DATABASE_URL=mysql://root:strong_password@localhost:3307/ \ No newline at end of file +DATABASE_URL=mysql://root:strong_password@localhost:3307/ +SERVER_ID=tester +PORT=5051 \ No newline at end of file diff --git a/server/src/main.rs b/server/src/main.rs index 976c1c1..9bd11e5 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -65,7 +65,7 @@ async fn main() -> anyhow::Result<()> { );" ).execute(&db_pool).await?; - let server_addr = (IpAddr::V6(Ipv6Addr::LOCALHOST), 5051); + 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. @@ -81,7 +81,7 @@ async fn main() -> anyhow::Result<()> { // serve is generated by the service attribute. It takes as input any type implementing // the generated World trait. .map(|channel| { - let server = RealmChatServer::new(channel.transport().peer_addr().unwrap(), db_pool.clone()); + let server = RealmChatServer::new(env::var("SERVER_ID").expect("SERVER_ID must be set"), channel.transport().peer_addr().unwrap(), db_pool.clone()); channel.execute(server.serve()).for_each(spawn) }) // Max 10 channels. diff --git a/server/src/server.rs b/server/src/server.rs index bb578c9..2fbea6f 100644 --- a/server/src/server.rs +++ b/server/src/server.rs @@ -8,27 +8,18 @@ use crate::types::ErrorCode::*; #[derive(Clone)] pub struct RealmChatServer { + pub server_id: String, pub socket: SocketAddr, pub db_pool: Pool, pub typing_users: Vec<(u32, u32)> //NOTE: userid, roomid -} - -impl RealmChatServer { - pub fn new(socket: SocketAddr, db_pool: Pool) -> RealmChatServer { - RealmChatServer { - socket, - db_pool, - typing_users: Vec::new(), - } - } -} +} //TODO: Cache for auth impl RealmChat for RealmChatServer { async fn test(self, _: Context, name: String) -> String { format!("Hello, {name}!") } - async fn send_message(self, _: Context, message: Message) -> Result { + async fn send_message(self, _: Context, auth_token: String, message: Message) -> Result { //TODO: verify authentication somehow for edits and redactions let result = match &message.data { @@ -70,19 +61,19 @@ impl RealmChat for RealmChatServer { } } - async fn start_typing(self, _: Context) -> ErrorCode { //TODO: auth for all of these + async fn start_typing(self, _: Context, auth_token: String) -> ErrorCode { //TODO: auth for all of these todo!() } - async fn stop_typing(self, _: Context) -> ErrorCode { + async fn stop_typing(self, _: Context, auth_token: String) -> ErrorCode { todo!() } - async fn keep_typing(self, _: Context) -> ErrorCode { + async fn keep_typing(self, _: Context, auth_token: String) -> ErrorCode { todo!() } - async fn get_message_from_id(self, _: Context, id: u32) -> Result { + async fn get_message_from_id(self, _: Context, auth_token: String, id: u32) -> Result { //TODO: Auth for admin room let result = sqlx::query( "SELECT * FROM message INNER JOIN room ON message.room = room.id INNER JOIN user ON message.user = user.id WHERE message.id = ?" @@ -98,12 +89,12 @@ impl RealmChat for RealmChatServer { } } - async fn get_messages_since(self, _: Context, time: DateTime) -> Result, ErrorCode> { + async fn get_messages_since(self, _: Context, auth_token: String, time: DateTime) -> Result, ErrorCode> { //TODO: Auth for admin rooms todo!() } - async fn get_rooms(self, _: Context) -> Result, ErrorCode> { + async fn get_rooms(self, _: Context, auth_token: String) -> Result, ErrorCode> { //TODO: Auth for admin rooms! let result = sqlx::query("SELECT * FROM room").fetch_all(&self.db_pool).await; let mut rooms: Vec = Vec::new(); @@ -125,7 +116,7 @@ impl RealmChat for RealmChatServer { } } - async fn get_room(self, _: Context, roomid: String) -> Result { + async fn get_room(self, _: Context, auth_token: String, roomid: String) -> Result { //TODO: Auth for admin rooms! let result = sqlx::query("SELECT * FROM room WHERE room_id = ?").bind(roomid).fetch_one(&self.db_pool).await; @@ -172,6 +163,15 @@ impl RealmChat for RealmChatServer { } impl RealmChatServer { + pub fn new(server_id: String, socket: SocketAddr, db_pool: Pool) -> RealmChatServer { + RealmChatServer { + server_id, + socket, + db_pool, + typing_users: Vec::new(), + } + } + fn dbroom_to_room(&self, row: MySqlRow) -> Result { let id: Result = row.try_get("id"); let roomid: Result = row.try_get("user_id"); diff --git a/server/src/types.rs b/server/src/types.rs index 714ca99..1e95148 100644 --- a/server/src/types.rs +++ b/server/src/types.rs @@ -6,19 +6,19 @@ pub trait RealmChat { async fn test(name: String) -> String; //TODO: Any user authorized as themselves - async fn send_message(message: Message) -> Result; - async fn start_typing() -> ErrorCode; - async fn stop_typing() -> ErrorCode; - async fn keep_typing() -> ErrorCode; //NOTE: If a keep alive hasn't been received in 5 seconds, stop typing + async fn send_message(auth_token: String, message: Message) -> Result; + async fn start_typing(auth_token: String) -> ErrorCode; + async fn stop_typing(auth_token: String) -> ErrorCode; + async fn keep_typing(auth_token: String) -> ErrorCode; //NOTE: If a keep alive hasn't been received in 5 seconds, stop typing //NOTE: Any user can call, if they are in the server - async fn get_message_from_id(id: u32) -> Result; - async fn get_messages_since(time: DateTime) -> Result, ErrorCode>; - async fn get_rooms() -> Result, ErrorCode>; - async fn get_room(roomid: String) -> Result; + async fn get_message_from_id(auth_token: String, id: u32) -> Result; + async fn get_messages_since(auth_token: String, time: DateTime) -> Result, ErrorCode>; + async fn get_rooms(auth_token: String) -> Result, ErrorCode>; + async fn get_room(auth_token: String, roomid: String) -> Result; async fn get_user(userid: String) -> Result; async fn get_users(get_only_online: bool) -> Result, ErrorCode>; - + //TODO: Admin access only! // async fn create_room() -> Result; // delete room