Skip to content

Commit

Permalink
Wop. Now templates are in the loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Jul 31, 2024
1 parent 3e243d6 commit f8ddea7
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 65 deletions.
2 changes: 2 additions & 0 deletions source/Main/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ pub struct Scene {
pub name: String,
pub sitename: Option<String>,
pub stylefile: Option<String>,
pub script: Option<String>,
pub templates: Templates,
}
impl Default for Scene {
Expand All @@ -270,6 +271,7 @@ impl Default for Scene {
name: String::from("default"),
sitename: Some(String::from("My Cynthia Site")),
stylefile: None,
script: None,
templates: Templates {
post: String::from("post"),
page: String::from("page"),
Expand Down
42 changes: 35 additions & 7 deletions source/Main/externalpluginservers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@
// This module will be a testing ground for a new system that will be more reliable and more secure.
// More specifically: The plugins will attach to js again, but inside of a controlled environment.

#[derive(Debug)]
pub(crate) struct EPSCommunicationData {
/// The sender to the (NodeJS) external plugin server not to be used directly.
sender: tokio::sync::mpsc::Sender<EPSRequest>,
/// The responses from the external plugin servers
response_queue: Vec<Option<EPSResponse>>,
/// The IDs that have been sent to the external plugin servers but have not been returned yet.
unreturned_ids: Vec<EPSCommunicationsID>,
}

impl EPSCommunicationData {
pub(crate) fn new(sender: tokio::sync::mpsc::Sender<EPSRequest>) -> Self {
Self {
sender,
response_queue: vec![],
unreturned_ids: vec![],
}
}
}

use std::process::Command;
use std::sync::Arc;

Expand All @@ -35,11 +55,17 @@ pub(crate) struct EPSRequest {
#[serde(tag = "for")]
pub(crate) enum EPSRequestBody {
Close,
Test { test: String },
Test {
test: String,
},
WebRequest {
page_id: String,
headers: Vec<(String, String)>, // Name, Value
method: String,
},
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct EPSResponse {
struct EPSResponse {
id: EPSCommunicationsID,
pub(crate) body: EPSResponseBody,
}
Expand Down Expand Up @@ -141,11 +167,14 @@ async fn and_now(res: EPSResponse, _server_context_mutex: Arc<Mutex<ServerContex
debug!("Added response to external plugin server queue.");
// panic!("The function runs! Finally! It runs!");
}

/**
This function sends a request over mpsc to the externalpluginservers::main function, then periodically locks the server mutex and checks if a corresponding response (matched by `id`) is added, if not, it will try again.
It is recommended to use this function instead of other methods of sending requests to the external plugin server.
*/
pub(crate) async fn contact_eps(
_server_context_mutex: Data<Arc<Mutex<ServerContext>>>,
req: EPSRequestBody,
) -> EPSResponse {
) -> EPSResponseBody {
let random_id = {
let mut d: EPSCommunicationsID;
loop {
Expand Down Expand Up @@ -189,7 +218,6 @@ pub(crate) async fn contact_eps(
panic!("Failed to send request to external plugin server.");
}
};
// This function sends a request over mpsc to the externalpluginservers::main function, then periodically locks the server mutex and checks if a corresponding response (matched by `id`) is added, if not, it will try again.
// After sending, check for received responses.
let mut wait = tokio::time::interval(tokio::time::Duration::from_micros(60));
loop {
Expand All @@ -215,7 +243,7 @@ pub(crate) async fn contact_eps(
// Match! Return the response and remove it from the vector.
drop(wait);
// Remove it from the unreturned vec
let p = o.take().unwrap();
let p = o.take().unwrap().body;
drop(server_context);
{
let mut server_context = _server_context_mutex.lock().await;
Expand Down
52 changes: 36 additions & 16 deletions source/Main/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

use std::fs::File;
use std::option::Option;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -49,19 +48,44 @@ struct ServerContext {
cache: CynthiaCache,
request_count: u64,
start_time: u128,
external_plugin_server: EPSCommunicationMemory,
external_plugin_server: EPSCommunicationData,
}
trait LockCallback {
async fn lock_callback<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut MutexGuard<ServerContext>) -> T;
}
impl LockCallback for Mutex<ServerContext> {
async fn lock_callback<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut MutexGuard<ServerContext>) -> T,
{
let mut s = self.lock().await;
f(&mut s)
}
}
impl LockCallback for Arc<Mutex<ServerContext>> {
async fn lock_callback<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut MutexGuard<ServerContext>) -> T,
{
let mut s = self.lock().await;
f(&mut s)
}
}
impl LockCallback for Data<Arc<Mutex<ServerContext>>> {
async fn lock_callback<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut MutexGuard<ServerContext>) -> T,
{
let mut s = self.lock().await;
f(&mut s)
}
}

type EPSCommunicationsID = u32;

#[derive(Debug)]
struct EPSCommunicationMemory {
/// The sender to the (NodeJS) external plugin server not to be used directly.
pub(crate) sender: tokio::sync::mpsc::Sender<externalpluginservers::EPSRequest>,
/// The responses from the external plugin servers
pub(crate) response_queue: Vec<Option<externalpluginservers::EPSResponse>>,
/// The IDs that have been sent to the external plugin servers but have not been returned yet.
pub(crate) unreturned_ids: Vec<EPSCommunicationsID>,
}
use crate::externalpluginservers::EPSCommunicationData;

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -270,11 +294,7 @@ async fn start() {
cache: vec![],
request_count: 0,
start_time: 0,
external_plugin_server: EPSCommunicationMemory {
sender: to_eps_s,
response_queue: vec![],
unreturned_ids: vec![],
},
external_plugin_server: EPSCommunicationData::new(to_eps_s),
};
let _ = &server_context.tell(format!(
"Logging to {}",
Expand Down
19 changes: 17 additions & 2 deletions source/Main/publications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3, see the LICENSE file for more information.
*/

use std::{fs, process};
use std::path::Path;
use std::{fs, process};

use jsonc_parser::parse_to_serde_value;
use log::{error, warn};
Expand Down Expand Up @@ -120,6 +120,7 @@ pub(crate) enum CynthiaPublication {
title: String,
description: Option<String>,
thumbnail: Option<String>,
dates: CynthiaPublicationDates,
#[serde(alias = "content")]
pagecontent: PublicationContent,
#[serde(alias = "scene")]
Expand All @@ -131,9 +132,12 @@ pub(crate) enum CynthiaPublication {
id: String,
title: String,
short: Option<String>,
dates: CynthiaPublicationDates,
thumbnail: Option<String>,
category: Option<String>,
author: Option<Author>,
#[serde(alias = "content")]
pagecontent: PublicationContent,
postcontent: PublicationContent,
#[serde(alias = "scene")]
#[serde(alias = "scene-override")]
scene_override: Option<String>,
Expand Down Expand Up @@ -169,6 +173,11 @@ impl CynthiaPublication {
}
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct CynthiaPublicationDates {
pub(crate) altered: u64,
pub(crate) published: u64,
}
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) enum PostListFilter {
#[default]
#[serde(alias = "latest")]
Expand Down Expand Up @@ -215,6 +224,12 @@ impl ContentType {
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct Author {
pub(crate) name: Option<String>,
pub(crate) thumbnail: Option<String>,
pub(crate) link: Option<String>,
}
pub(crate) fn read_published_jsonc() -> CynthiaPublicationList {
if Path::new("./cynthiaFiles/published.yaml").exists() {
let file = "./cynthiaFiles/published.yaml".to_owned();
Expand Down
Loading

0 comments on commit f8ddea7

Please sign in to comment.