Server config
This commit is contained in:
@@ -24,3 +24,6 @@ target
|
||||
# Added by cargo
|
||||
|
||||
/target
|
||||
|
||||
private.key
|
||||
config.json
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::protocol::ServerMeta;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub meta: ServerMeta,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
meta: ServerMeta {
|
||||
name: "New Server".to_string(),
|
||||
description: String::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get() -> anyhow::Result<Self> {
|
||||
let config_path = PathBuf::from("config.json");
|
||||
|
||||
if !config_path.exists() {
|
||||
let config = Config::new();
|
||||
tokio::fs::write(config_path, &serde_json::to_string_pretty(&config)?).await?;
|
||||
Ok(config)
|
||||
} else {
|
||||
Ok(serde_json::from_str(
|
||||
&tokio::fs::read_to_string(config_path).await?,
|
||||
)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod config;
|
||||
pub mod protocol;
|
||||
pub mod server;
|
||||
pub mod signature;
|
||||
|
||||
@@ -14,6 +14,12 @@ pub struct Client {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ClientMeta {}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ServerMeta {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "method")]
|
||||
pub enum ClientMethod {
|
||||
|
||||
+3
-1
@@ -6,16 +6,18 @@ use axum::{
|
||||
};
|
||||
use ed25519_dalek::SigningKey;
|
||||
|
||||
use crate::protocol::Client;
|
||||
use crate::{config::Config, protocol::Client};
|
||||
|
||||
pub struct Server {
|
||||
pub key: SigningKey,
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub async fn new() -> anyhow::Result<Arc<Self>> {
|
||||
Ok(Arc::new(Self {
|
||||
key: crate::signature::get().await?,
|
||||
config: Config::get().await?,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user