Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Automatically fetch example config file if it doesn't exist.
Browse files Browse the repository at this point in the history
Update default values
  • Loading branch information
alessiodam committed Jun 18, 2024
1 parent eae40fa commit 3cb15fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 3 additions & 3 deletions config.toml.example
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[server]
host = '0.0.0.0' # on what interface to run the server on
host = '127.0.0.1' # on what interface to run the server on
port = 2052 # on what port to run the server on
online_mode = true # validate users using TINET Authentication
api_key = "change me to use online-mode" # App API Key from TINET (Enable under Experiments)
protect_server = false # protect your server with a password
server_password = "12345678" # password for the server (requires protect_server to be true)

[web]
enable = true # enable the web ui
host = "0.0.0.0" # on what interface to run the web ui on
enable = false # enable the web ui
host = "127.0.0.1" # on what interface to run the web ui on
port = 8000 # on what port to run the web ui on
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::error::Error;
use serde::Deserialize;
use crate::CONFIG_PATH;

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
Expand All @@ -26,7 +27,7 @@ pub struct WebConfig {

impl Config {
pub fn load_config() -> Result<Self, Box<dyn Error>> {
let contents = std::fs::read_to_string("config.toml")
let contents = std::fs::read_to_string(CONFIG_PATH)
.map_err(|e| format!("Failed to read config file: {}", e))?;

toml::from_str(&contents).map_err(|e| format!("Error deserializing config: {}", e).into())
Expand Down
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use tokio::signal;
use std::collections::HashMap;
use tokio::io::AsyncWriteExt;
use tokio::time::{sleep, Duration};
use std::fs;
use reqwest;

mod config;
mod auth;
Expand All @@ -16,14 +18,31 @@ mod web_ui;
use config::Config;
use conn_handler::{handle_connection, ActiveUsers, ChatRooms};

const CONFIG_URL: &str = "https://raw.githubusercontent.com/tkbstudios/netchat-server-rust/master/config.toml.example";
const CONFIG_PATH: &str = "config.toml";

fn init_tracing() {
fmt::init();
}

async fn fetch_and_save_config() -> Result<(), Box<dyn Error + Send + Sync>> {
let response = reqwest::get(CONFIG_URL).await?;
let content = response.text().await?;
fs::write(CONFIG_PATH, content)?;
Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
init_tracing();

if !std::path::Path::new(CONFIG_PATH).exists() {
tracing::warn!("Config file not found, fetching from remote...");
fetch_and_save_config().await.expect("Failed to fetch config file");
tracing::info!("Config file fetched and saved as config.toml. Please edit it carefully before restarting the server.");
return Ok(());
}

let config = Config::load_config().expect("Failed to load config");

tracing::info!(target: "server", "Starting server with online mode: {} on {}:{}", config.server.online_mode, config.server.host, config.server.port);
Expand Down

0 comments on commit 3cb15fe

Please sign in to comment.