Add actor_id to NodeConfig
This commit is contained in:
parent
ba87ccf734
commit
fa6591ab87
6 changed files with 99 additions and 15 deletions
|
@ -2,12 +2,12 @@ use std::{net::IpAddr, path::PathBuf};
|
||||||
|
|
||||||
use clap::Args;
|
use clap::Args;
|
||||||
|
|
||||||
|
use crate::config::PartialServerConfig;
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
pub struct ServerArgs {
|
pub struct ServerArgs {
|
||||||
#[arg(long)]
|
#[command(flatten)]
|
||||||
listen_ip: IpAddr,
|
server_config: PartialServerConfig,
|
||||||
#[arg(long)]
|
|
||||||
port: i32,
|
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
config: PathBuf,
|
config: PathBuf,
|
||||||
}
|
}
|
|
@ -2,4 +2,12 @@ mod node;
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
pub use node::NodeConfig;
|
pub use node::NodeConfig;
|
||||||
pub use server::ServerConfig;
|
pub use server::{
|
||||||
|
PartialServerConfig,
|
||||||
|
ServerConfig,
|
||||||
|
DEFAULT_LISTEN_IPS,
|
||||||
|
DEFAULT_PORT,
|
||||||
|
DEFAULT_PARTIAL_SERVER_CONFIG,
|
||||||
|
DEFAULT_SERVER_CONFIG
|
||||||
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use automerge::ActorId;
|
||||||
use libp2p::identity::{self, Keypair};
|
use libp2p::identity::{self, Keypair};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
@ -5,6 +6,8 @@ use serde::{Deserialize, Serialize};
|
||||||
pub struct NodeConfig {
|
pub struct NodeConfig {
|
||||||
#[serde(with = "keypair")]
|
#[serde(with = "keypair")]
|
||||||
secret: Keypair,
|
secret: Keypair,
|
||||||
|
#[serde(with = "actor_id")]
|
||||||
|
actor_id: ActorId
|
||||||
}
|
}
|
||||||
|
|
||||||
mod keypair {
|
mod keypair {
|
||||||
|
@ -26,28 +29,49 @@ mod keypair {
|
||||||
let vec = BASE64_STANDARD.decode(base64).unwrap();
|
let vec = BASE64_STANDARD.decode(base64).unwrap();
|
||||||
Ok(Keypair::from_protobuf_encoding(&vec).unwrap())
|
Ok(Keypair::from_protobuf_encoding(&vec).unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PartialNodeConfig {
|
mod actor_id {
|
||||||
pub secret: Option<Vec<u8>>,
|
use automerge::ActorId;
|
||||||
|
use base64::{prelude::BASE64_STANDARD, Engine};
|
||||||
|
use serde::{Deserialize, Deserializer, Serializer};
|
||||||
|
|
||||||
|
pub fn serialize<S>(actor_id: &ActorId, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where S: Serializer
|
||||||
|
{
|
||||||
|
let bytes = actor_id.to_bytes();
|
||||||
|
let base64 = BASE64_STANDARD.encode(bytes);
|
||||||
|
serializer.serialize_str(&base64)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<ActorId, D::Error>
|
||||||
|
where D: Deserializer<'de>
|
||||||
|
{
|
||||||
|
let base64 = String::deserialize(deserializer)?;
|
||||||
|
let vec = BASE64_STANDARD.decode(base64).unwrap();
|
||||||
|
Ok(ActorId::from(vec))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use automerge::ActorId;
|
||||||
use libp2p::identity;
|
use libp2p::identity;
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn parse_ed25519() {
|
async fn serialize_deserialize() {
|
||||||
let keypair = identity::Keypair::generate_ed25519();
|
let keypair = identity::Keypair::generate_ed25519();
|
||||||
|
let actor_id = ActorId::random();
|
||||||
let config = NodeConfig {
|
let config = NodeConfig {
|
||||||
secret: keypair.clone(),
|
secret: keypair.clone(),
|
||||||
|
actor_id: actor_id.clone(),
|
||||||
};
|
};
|
||||||
let string = toml::to_string(&config).unwrap();
|
let string = toml::to_string(&config).unwrap();
|
||||||
println!("Parsed config: {}", &string);
|
println!("Parsed config: {}", &string);
|
||||||
let parsed_config: NodeConfig = toml::from_str(&string).unwrap();
|
let parsed_config: NodeConfig = toml::from_str(&string).unwrap();
|
||||||
assert_eq!(keypair.public(), parsed_config.secret.public())
|
assert_eq!(keypair.public(), parsed_config.secret.public());
|
||||||
|
assert_eq!(actor_id, parsed_config.actor_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,55 @@
|
||||||
use std::{collections::HashSet, net::IpAddr};
|
use std::{collections::HashSet, net::{IpAddr, Ipv4Addr}, str::FromStr, sync::LazyLock};
|
||||||
|
|
||||||
|
use automerge::hydrate::List;
|
||||||
|
use clap::Args;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
use crate::error::Error;
|
||||||
|
|
||||||
|
pub static DEFAULT_LISTEN_IPS: &[IpAddr] = &[IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))];
|
||||||
|
pub static DEFAULT_PORT: u16 = 8080;
|
||||||
|
pub static DEFAULT_SERVER_CONFIG: LazyLock<ServerConfig> = LazyLock::new(|| {
|
||||||
|
ServerConfig{
|
||||||
|
listen_ips: Vec::from(DEFAULT_LISTEN_IPS),
|
||||||
|
port: DEFAULT_PORT
|
||||||
|
}
|
||||||
|
});
|
||||||
|
pub static DEFAULT_PARTIAL_SERVER_CONFIG: LazyLock<PartialServerConfig> = LazyLock::new(|| {
|
||||||
|
PartialServerConfig::from((*DEFAULT_SERVER_CONFIG).clone())
|
||||||
|
});
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
pub struct ServerConfig {
|
pub struct ServerConfig {
|
||||||
listen_ips: HashSet<IpAddr>,
|
listen_ips: Vec<IpAddr>,
|
||||||
port: i32,
|
port: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<PartialServerConfig> for ServerConfig {
|
||||||
|
type Error = Error;
|
||||||
|
fn try_from(config: PartialServerConfig) -> Result<ServerConfig, Self::Error>{
|
||||||
|
Ok(ServerConfig {
|
||||||
|
listen_ips: config.listen_ips.ok_or(Error::MissingConfig("listen_ips".to_string()))?,
|
||||||
|
port: config.port.ok_or(Error::MissingConfig("port".to_string()))?
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Args, Debug, Deserialize, Serialize)]
|
||||||
|
pub struct PartialServerConfig {
|
||||||
|
#[arg(long)]
|
||||||
|
listen_ips: Option<Vec<IpAddr>>,
|
||||||
|
#[arg(long)]
|
||||||
|
port: Option<u16>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ServerConfig> for PartialServerConfig {
|
||||||
|
fn from(config: ServerConfig) -> PartialServerConfig {
|
||||||
|
PartialServerConfig {
|
||||||
|
listen_ips: Some(config.listen_ips),
|
||||||
|
port: Some(config.port)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
5
lazy-supplements/src/error.rs
Normal file
5
lazy-supplements/src/error.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("mandatory config `{0}` is missing")]
|
||||||
|
MissingConfig(String),
|
||||||
|
}
|
|
@ -1,2 +1,3 @@
|
||||||
pub mod cli;
|
pub mod cli;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod error;
|
Loading…
Add table
Reference in a new issue