Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: read monitors from config file #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/log
/target
/server_config.yml
1 change: 1 addition & 0 deletions phira-mp-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ anyhow = { version = "1.0", features = ["backtrace"] }
phira-mp-common = { path = "../phira-mp-common" }
reqwest = { version = "0.11.18", features = ["json"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_yaml = "0.9"
tap = "1.0.1"
tokio = "*"
tracing = "0.1.37"
Expand Down
11 changes: 9 additions & 2 deletions phira-mp-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{vacant_entry, IdMap, Room, SafeMap, Session, User};
use anyhow::Result;
use phira_mp_common::RoomId;
use serde::Deserialize;
use std::sync::Arc;
use std::{fs, sync::Arc};
use tokio::{net::TcpListener, sync::mpsc, task::JoinHandle};
use tracing::{info, warn};
use uuid::Uuid;
Expand All @@ -13,6 +13,11 @@ pub struct Chart {
pub name: String,
}

#[derive(Debug, Deserialize)]
pub struct ServerConfig{
pub monitors: Vec<i32>
}

#[derive(Debug, Deserialize)]
pub struct Record {
pub id: i32,
Expand All @@ -30,6 +35,7 @@ pub struct Record {
}

pub struct ServerState {
pub config: ServerConfig,
pub sessions: IdMap<Arc<Session>>,
pub users: SafeMap<i32, Arc<User>>,

Expand All @@ -41,14 +47,15 @@ pub struct ServerState {
pub struct Server {
state: Arc<ServerState>,
listener: TcpListener,

lost_con_handle: JoinHandle<()>,
}

impl From<TcpListener> for Server {
fn from(listener: TcpListener) -> Self {
let (lost_con_tx, mut lost_con_rx) = mpsc::channel(16);
let config: ServerConfig = serde_yaml::from_str(&fs::read_to_string("server_config.yml").unwrap_or("monitors : [2,]".to_string())).unwrap();
let state = Arc::new(ServerState {
config,
sessions: IdMap::default(),
users: SafeMap::default(),

Expand Down
3 changes: 1 addition & 2 deletions phira-mp-server/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use tracing::{debug, debug_span, error, info, trace, warn, Instrument};
use uuid::Uuid;

const HOST: &str = "https://api.phira.cn";
const MONITORS: &[i32] = &[2, 143245];

pub struct User {
pub id: i32,
Expand Down Expand Up @@ -71,7 +70,7 @@ impl User {
}

pub fn can_monitor(&self) -> bool {
MONITORS.contains(&self.id)
self.server.config.monitors.contains(&self.id)
}

pub async fn set_session(&self, session: Weak<Session>) {
Expand Down