Extract constants and add config loader + example appsettings for niom-signaling; wire main to config
This commit is contained in:
parent
2021a4fab0
commit
ffad23446a
2
.gitignore
vendored
2
.gitignore
vendored
@ -19,4 +19,4 @@ target
|
|||||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||||
#.idea/
|
.idea/
|
||||||
|
|||||||
5
appsettings.example.json
Normal file
5
appsettings.example.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"server": {
|
||||||
|
"bind": "127.0.0.1:3478"
|
||||||
|
}
|
||||||
|
}
|
||||||
24
src/config.rs
Normal file
24
src/config.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub struct ServerOptions {
|
||||||
|
pub bind: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
|
pub struct Config {
|
||||||
|
pub server: ServerOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn from_file<P: AsRef<Path>>(p: P) -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
let s = std::fs::read_to_string(p)?;
|
||||||
|
let cfg: Config = serde_json::from_str(&s)?;
|
||||||
|
Ok(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_default() -> Result<Self, Box<dyn std::error::Error>> {
|
||||||
|
Self::from_file("appsettings.json")
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/constants.rs
Normal file
3
src/constants.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
// Central constants for niom-signaling
|
||||||
|
pub const DEFAULT_BIND: &str = "127.0.0.1:3478";
|
||||||
|
pub const DEFAULT_HOST_URL: &str = "http://localhost:3478";
|
||||||
19
src/main.rs
19
src/main.rs
@ -3,6 +3,10 @@ use futures_util::StreamExt;
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
|
mod constants;
|
||||||
|
mod config;
|
||||||
|
use crate::config::Config;
|
||||||
|
use crate::constants::*;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
@ -25,15 +29,26 @@ async fn main() -> std::io::Result<()> {
|
|||||||
let app_state = AppState {
|
let app_state = AppState {
|
||||||
broadcaster: Arc::new(tx)
|
broadcaster: Arc::new(tx)
|
||||||
};
|
};
|
||||||
|
// Try to load config, otherwise fall back to defaults
|
||||||
|
let cfg = match Config::load_default() {
|
||||||
|
Ok(c) => {
|
||||||
|
println!("Loaded config from appsettings.json");
|
||||||
|
c
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
println!("No config found, using defaults");
|
||||||
|
Config { server: crate::config::ServerOptions { bind: DEFAULT_BIND.to_string() } }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
println!("Server is running on http://localhost:3478");
|
println!("Server is running on {}", DEFAULT_HOST_URL);
|
||||||
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
App::new()
|
App::new()
|
||||||
.app_data(web::Data::new(app_state.clone()))
|
.app_data(web::Data::new(app_state.clone()))
|
||||||
.route("/ws", web::get().to(websocket_handler))
|
.route("/ws", web::get().to(websocket_handler))
|
||||||
})
|
})
|
||||||
.bind("127.0.0.1:3478")?
|
.bind(&cfg.server.bind)?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user