Add SERVER_ID and PORT
This commit is contained in:
@@ -1 +1,3 @@
|
|||||||
DATABASE_URL=mysql://root:strong_password@localhost:3307/
|
DATABASE_URL=mysql://root:strong_password@localhost:3307/
|
||||||
|
SERVER_ID=tester
|
||||||
|
PORT=5051
|
||||||
@@ -65,7 +65,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
);"
|
);"
|
||||||
).execute(&db_pool).await?;
|
).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::<u16>().unwrap());
|
||||||
|
|
||||||
// JSON transport is provided by the json_transport tarpc module. It makes it easy
|
// 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.
|
// 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
|
// serve is generated by the service attribute. It takes as input any type implementing
|
||||||
// the generated World trait.
|
// the generated World trait.
|
||||||
.map(|channel| {
|
.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)
|
channel.execute(server.serve()).for_each(spawn)
|
||||||
})
|
})
|
||||||
// Max 10 channels.
|
// Max 10 channels.
|
||||||
|
|||||||
@@ -8,27 +8,18 @@ use crate::types::ErrorCode::*;
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RealmChatServer {
|
pub struct RealmChatServer {
|
||||||
|
pub server_id: String,
|
||||||
pub socket: SocketAddr,
|
pub socket: SocketAddr,
|
||||||
pub db_pool: Pool<MySql>,
|
pub db_pool: Pool<MySql>,
|
||||||
pub typing_users: Vec<(u32, u32)> //NOTE: userid, roomid
|
pub typing_users: Vec<(u32, u32)> //NOTE: userid, roomid
|
||||||
}
|
} //TODO: Cache for auth
|
||||||
|
|
||||||
impl RealmChatServer {
|
|
||||||
pub fn new(socket: SocketAddr, db_pool: Pool<MySql>) -> RealmChatServer {
|
|
||||||
RealmChatServer {
|
|
||||||
socket,
|
|
||||||
db_pool,
|
|
||||||
typing_users: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RealmChat for RealmChatServer {
|
impl RealmChat for RealmChatServer {
|
||||||
async fn test(self, _: Context, name: String) -> String {
|
async fn test(self, _: Context, name: String) -> String {
|
||||||
format!("Hello, {name}!")
|
format!("Hello, {name}!")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_message(self, _: Context, message: Message) -> Result<Message, ErrorCode> {
|
async fn send_message(self, _: Context, auth_token: String, message: Message) -> Result<Message, ErrorCode> {
|
||||||
//TODO: verify authentication somehow for edits and redactions
|
//TODO: verify authentication somehow for edits and redactions
|
||||||
|
|
||||||
let result = match &message.data {
|
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!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn stop_typing(self, _: Context) -> ErrorCode {
|
async fn stop_typing(self, _: Context, auth_token: String) -> ErrorCode {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn keep_typing(self, _: Context) -> ErrorCode {
|
async fn keep_typing(self, _: Context, auth_token: String) -> ErrorCode {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_message_from_id(self, _: Context, id: u32) -> Result<Message, ErrorCode> {
|
async fn get_message_from_id(self, _: Context, auth_token: String, id: u32) -> Result<Message, ErrorCode> {
|
||||||
//TODO: Auth for admin room
|
//TODO: Auth for admin room
|
||||||
let result = sqlx::query(
|
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 = ?"
|
"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<Utc>) -> Result<Vec<Message>, ErrorCode> {
|
async fn get_messages_since(self, _: Context, auth_token: String, time: DateTime<Utc>) -> Result<Vec<Message>, ErrorCode> {
|
||||||
//TODO: Auth for admin rooms
|
//TODO: Auth for admin rooms
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_rooms(self, _: Context) -> Result<Vec<Room>, ErrorCode> {
|
async fn get_rooms(self, _: Context, auth_token: String) -> Result<Vec<Room>, ErrorCode> {
|
||||||
//TODO: Auth for admin rooms!
|
//TODO: Auth for admin rooms!
|
||||||
let result = sqlx::query("SELECT * FROM room").fetch_all(&self.db_pool).await;
|
let result = sqlx::query("SELECT * FROM room").fetch_all(&self.db_pool).await;
|
||||||
let mut rooms: Vec<Room> = Vec::new();
|
let mut rooms: Vec<Room> = Vec::new();
|
||||||
@@ -125,7 +116,7 @@ impl RealmChat for RealmChatServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_room(self, _: Context, roomid: String) -> Result<Room, ErrorCode> {
|
async fn get_room(self, _: Context, auth_token: String, roomid: String) -> Result<Room, ErrorCode> {
|
||||||
//TODO: Auth for admin rooms!
|
//TODO: Auth for admin rooms!
|
||||||
let result = sqlx::query("SELECT * FROM room WHERE room_id = ?").bind(roomid).fetch_one(&self.db_pool).await;
|
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 {
|
impl RealmChatServer {
|
||||||
|
pub fn new(server_id: String, socket: SocketAddr, db_pool: Pool<MySql>) -> RealmChatServer {
|
||||||
|
RealmChatServer {
|
||||||
|
server_id,
|
||||||
|
socket,
|
||||||
|
db_pool,
|
||||||
|
typing_users: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn dbroom_to_room(&self, row: MySqlRow) -> Result<Room, ErrorCode> {
|
fn dbroom_to_room(&self, row: MySqlRow) -> Result<Room, ErrorCode> {
|
||||||
let id: Result<u32, _> = row.try_get("id");
|
let id: Result<u32, _> = row.try_get("id");
|
||||||
let roomid: Result<String, _> = row.try_get("user_id");
|
let roomid: Result<String, _> = row.try_get("user_id");
|
||||||
|
|||||||
@@ -6,19 +6,19 @@ pub trait RealmChat {
|
|||||||
async fn test(name: String) -> String;
|
async fn test(name: String) -> String;
|
||||||
|
|
||||||
//TODO: Any user authorized as themselves
|
//TODO: Any user authorized as themselves
|
||||||
async fn send_message(message: Message) -> Result<Message, ErrorCode>;
|
async fn send_message(auth_token: String, message: Message) -> Result<Message, ErrorCode>;
|
||||||
async fn start_typing() -> ErrorCode;
|
async fn start_typing(auth_token: String) -> ErrorCode;
|
||||||
async fn stop_typing() -> ErrorCode;
|
async fn stop_typing(auth_token: String) -> ErrorCode;
|
||||||
async fn keep_typing() -> ErrorCode; //NOTE: If a keep alive hasn't been received in 5 seconds, stop typing
|
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
|
//NOTE: Any user can call, if they are in the server
|
||||||
async fn get_message_from_id(id: u32) -> Result<Message, ErrorCode>;
|
async fn get_message_from_id(auth_token: String, id: u32) -> Result<Message, ErrorCode>;
|
||||||
async fn get_messages_since(time: DateTime<Utc>) -> Result<Vec<Message>, ErrorCode>;
|
async fn get_messages_since(auth_token: String, time: DateTime<Utc>) -> Result<Vec<Message>, ErrorCode>;
|
||||||
async fn get_rooms() -> Result<Vec<Room>, ErrorCode>;
|
async fn get_rooms(auth_token: String) -> Result<Vec<Room>, ErrorCode>;
|
||||||
async fn get_room(roomid: String) -> Result<Room, ErrorCode>;
|
async fn get_room(auth_token: String, roomid: String) -> Result<Room, ErrorCode>;
|
||||||
async fn get_user(userid: String) -> Result<User, ErrorCode>;
|
async fn get_user(userid: String) -> Result<User, ErrorCode>;
|
||||||
async fn get_users(get_only_online: bool) -> Result<Vec<User>, ErrorCode>;
|
async fn get_users(get_only_online: bool) -> Result<Vec<User>, ErrorCode>;
|
||||||
|
|
||||||
//TODO: Admin access only!
|
//TODO: Admin access only!
|
||||||
// async fn create_room() -> Result<Room, ErrorCode>;
|
// async fn create_room() -> Result<Room, ErrorCode>;
|
||||||
// delete room
|
// delete room
|
||||||
|
|||||||
Reference in New Issue
Block a user