mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
Feature/remove postgres (#2570)
* wip: move postgres data to patchdb * wip * wip * wip * complete notifications and clean up warnings * fill in user agent * move os tor bindings to single call
This commit is contained in:
@@ -1,29 +1,84 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use imbl_value::InternedString;
|
||||
use models::HostId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::net::host::multi::MultiHost;
|
||||
use crate::net::forward::AvailablePorts;
|
||||
use crate::net::host::address::HostAddress;
|
||||
use crate::net::host::binding::{BindInfo, BindOptions};
|
||||
use crate::prelude::*;
|
||||
|
||||
pub mod multi;
|
||||
pub mod address;
|
||||
pub mod binding;
|
||||
|
||||
pub enum Host {
|
||||
Multi(MultiHost),
|
||||
// Single(SingleHost),
|
||||
// Static(StaticHost),
|
||||
#[derive(Debug, Deserialize, Serialize, HasModel)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
#[model = "Model<Self>"]
|
||||
pub struct Host {
|
||||
pub kind: HostKind,
|
||||
pub bindings: BTreeMap<u16, BindInfo>,
|
||||
pub addresses: BTreeSet<HostAddress>,
|
||||
pub primary: Option<HostAddress>,
|
||||
}
|
||||
impl AsRef<Host> for Host {
|
||||
fn as_ref(&self) -> &Host {
|
||||
self
|
||||
}
|
||||
}
|
||||
impl Host {
|
||||
pub fn new(kind: HostKind) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
bindings: BTreeMap::new(),
|
||||
addresses: BTreeSet::new(),
|
||||
primary: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct BindOptions {
|
||||
scheme: InternedString,
|
||||
preferred_external_port: u16,
|
||||
add_ssl: Option<AddSslOptions>,
|
||||
secure: bool,
|
||||
ssl: bool,
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum HostKind {
|
||||
Multi,
|
||||
// Single,
|
||||
// Static,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct AddSslOptions {
|
||||
scheme: InternedString,
|
||||
preferred_external_port: u16,
|
||||
#[serde(default)]
|
||||
add_x_forwarded_headers: bool,
|
||||
#[derive(Debug, Default, Deserialize, Serialize, HasModel)]
|
||||
#[model = "Model<Self>"]
|
||||
pub struct HostInfo(BTreeMap<HostId, Host>);
|
||||
|
||||
impl Map for HostInfo {
|
||||
type Key = HostId;
|
||||
type Value = Host;
|
||||
fn key_str(key: &Self::Key) -> Result<impl AsRef<str>, Error> {
|
||||
Ok(key)
|
||||
}
|
||||
fn key_string(key: &Self::Key) -> Result<InternedString, Error> {
|
||||
Ok(key.clone().into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Model<HostInfo> {
|
||||
pub fn add_binding(
|
||||
&mut self,
|
||||
available_ports: &mut AvailablePorts,
|
||||
kind: HostKind,
|
||||
id: &HostId,
|
||||
internal_port: u16,
|
||||
options: BindOptions,
|
||||
) -> Result<(), Error> {
|
||||
self.upsert(id, || Host::new(kind))?
|
||||
.as_bindings_mut()
|
||||
.mutate(|b| {
|
||||
let info = if let Some(info) = b.remove(&internal_port) {
|
||||
info.update(available_ports, options)?
|
||||
} else {
|
||||
BindInfo::new(available_ports, options)?
|
||||
};
|
||||
b.insert(internal_port, info);
|
||||
Ok(())
|
||||
}) // TODO: handle host kind change
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user