add: littleweb base
This commit is contained in:
parent
07a23f505b
commit
c4de17058b
20 changed files with 457 additions and 8 deletions
154
crates/core/src/model/littleweb.rs
Normal file
154
crates/core/src/model/littleweb.rs
Normal file
|
@ -0,0 +1,154 @@
|
|||
use std::fmt::Display;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Service {
|
||||
pub id: usize,
|
||||
pub created: usize,
|
||||
pub owner: usize,
|
||||
pub name: String,
|
||||
pub files: Vec<ServiceFsEntry>,
|
||||
}
|
||||
|
||||
/// A file type for [`ServiceFsEntry`] structs.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum ServiceFsMime {
|
||||
#[serde(alias = "text/html")]
|
||||
Html,
|
||||
#[serde(alias = "text/css")]
|
||||
Css,
|
||||
#[serde(alias = "text/javascript")]
|
||||
Js,
|
||||
#[serde(alias = "application/json")]
|
||||
Json,
|
||||
#[serde(alias = "text/plain")]
|
||||
Plain,
|
||||
}
|
||||
|
||||
impl Display for ServiceFsMime {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Html => "text/html",
|
||||
Self::Css => "text/css",
|
||||
Self::Js => "text/javascript",
|
||||
Self::Json => "application/json",
|
||||
Self::Plain => "text/plain",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A single entry in the file system of [`Service`].
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ServiceFsEntry {
|
||||
pub name: String,
|
||||
pub mime: ServiceFsMime,
|
||||
pub children: Vec<ServiceFsEntry>,
|
||||
pub content: String,
|
||||
/// SHA-256 checksum of the entry's content.
|
||||
pub checksum: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum DomainTld {
|
||||
Bunny,
|
||||
}
|
||||
|
||||
impl Display for DomainTld {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(match self {
|
||||
Self::Bunny => "bunny",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for DomainTld {
|
||||
fn from(value: &str) -> Self {
|
||||
match value {
|
||||
"bunny" => Self::Bunny,
|
||||
_ => Self::Bunny,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Domain {
|
||||
pub id: usize,
|
||||
pub created: usize,
|
||||
pub owner: usize,
|
||||
pub name: String,
|
||||
pub tld: DomainTld,
|
||||
/// Data about the domain. This can only be configured by the domain's owner.
|
||||
///
|
||||
/// Maximum of 4 entries. Stored in a structure of `(subdomain string, data)`.
|
||||
pub data: Vec<(String, DomainData)>,
|
||||
}
|
||||
|
||||
impl Domain {
|
||||
/// Get the domain's subdomain, name, TLD, and path segments from a string.
|
||||
///
|
||||
/// If no subdomain is provided, the subdomain will be "@". This means that
|
||||
/// domain data entries should use "@" as the root service.
|
||||
pub fn from_str(value: &str) -> (&str, &str, DomainTld, Vec<String>) {
|
||||
// we're reversing this so it's predictable, as there might not always be a subdomain
|
||||
// (we shouldn't have the variable entry be first, there is always going to be a tld)
|
||||
let mut s: Vec<&str> = value.split(".").collect();
|
||||
s.reverse();
|
||||
let mut s = s.into_iter();
|
||||
|
||||
let tld = DomainTld::from(s.next().unwrap());
|
||||
let domain = s.next().unwrap();
|
||||
let subdomain = s.next().unwrap_or("@");
|
||||
|
||||
// get path
|
||||
let no_protocol = value.replace("atto://", "");
|
||||
let mut chars = no_protocol.chars();
|
||||
let mut char = '.';
|
||||
|
||||
while char != '/' {
|
||||
// we need to keep eating characters until we reach the first /
|
||||
// (marking the start of the path)
|
||||
char = chars.next().unwrap();
|
||||
}
|
||||
|
||||
let path: String = chars.collect();
|
||||
|
||||
// return
|
||||
(
|
||||
subdomain,
|
||||
domain,
|
||||
tld,
|
||||
path.split("/").map(|x| x.to_owned()).collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Update an HTML/JS/CSS string with the correct URL for all "atto://" protocol requests.
|
||||
///
|
||||
/// This would not be needed if the JS custom protocol API wasn't awful.
|
||||
pub fn http_assets(input: String) -> String {
|
||||
// this is served over the littleweb api NOT the main api!
|
||||
//
|
||||
// littleweb requests MUST be on another subdomain so cookies are
|
||||
// not shared with custom user HTML (since users can embed JS which can make POST requests)
|
||||
//
|
||||
// the littleweb routes are used by providing the "LITTLEWEB" env var
|
||||
input.replace("\"atto://", "/api/v1/over_http?addr=atto://")
|
||||
}
|
||||
|
||||
/// Get the domain's service ID.
|
||||
pub fn service(&self, subdomain: &str) -> Option<usize> {
|
||||
let s = self.data.iter().find(|x| x.0 == subdomain)?;
|
||||
match s.1 {
|
||||
DomainData::Service(id) => Some(id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum DomainData {
|
||||
/// The ID of the service this domain points to. The first service found will
|
||||
/// always be used. This means having multiple service entires will be useless.
|
||||
Service(usize),
|
||||
/// A text entry with a maximum of 512 characters.
|
||||
Text(String),
|
||||
}
|
|
@ -7,6 +7,7 @@ pub mod communities;
|
|||
pub mod communities_permissions;
|
||||
pub mod journals;
|
||||
pub mod layouts;
|
||||
pub mod littleweb;
|
||||
pub mod moderation;
|
||||
pub mod oauth;
|
||||
pub mod permissions;
|
||||
|
|
|
@ -174,6 +174,8 @@ bitflags! {
|
|||
pub struct SecondaryPermission: u32 {
|
||||
const DEFAULT = 1 << 0;
|
||||
const ADMINISTRATOR = 1 << 1;
|
||||
const MANAGE_DOMAINS = 1 << 2;
|
||||
const MANAGE_SERVICES = 1 << 3;
|
||||
|
||||
const _ = !0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue