Skip to content

Commit

Permalink
add possibility to have multiple addresses on jequi_proxy, add conten…
Browse files Browse the repository at this point in the history
…t-type detection on jequi_serve_static and change path config
  • Loading branch information
Termack committed Jun 9, 2024
1 parent 0c3a945 commit ad6bb43
Show file tree
Hide file tree
Showing 27 changed files with 355 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
*jequi.pid
expanded.rs
jequi_go.*
61 changes: 61 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ clear:

go_setup:
if ! [ -f /etc/jequi/libjequi.so ]; then \
sudo cp target/debug/libjequi.so /etc/jequi/libjequi.so; \
sudo cp $(PWD)/target/debug/libjequi.so /etc/jequi/libjequi.so; \
fi \
&& cd ./plugins/jequi_go/go/jequi \
&& go generate \
&& go mod edit -replace github.com/handle=$(HANDLER_PATH) \
&& go mod tidy
&& go mod tidy \
&& go generate

go: clear go_setup
cd ./plugins/jequi_go/go/jequi && LIB_DIR=$(LIB_DIR) go build -o $(LIB_DIR)/$(LIB_NAME).so -buildmode=c-shared
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ IMPORTANT: Make a better implementation for reading body and http2 and implement
Add Websocket
HTTP2 and some other things in http1 (like keepalive, chunked, gzip)
Check test coverage
Better way to load go code
Organize plugins that are becoming too big
More tests (read body async, http2, some other things)
Reload isn't working correctly when remove or add a new plugin (example: comment static_files_path and uncomment proxy_address)
Add proxy and jequi_go proxy tests
Expand Down
2 changes: 1 addition & 1 deletion api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub unsafe extern "C" fn get_request_body(req: *mut Request) -> *const c_char {
#[no_mangle]
pub unsafe extern "C" fn get_request_uri(req: *mut Request) -> *const c_char {
let req: &mut Request = unsafe { get_object_from_pointer(req) };
CString::new(req.uri.as_str()).unwrap().into_raw()
CString::new(req.uri.raw()).unwrap().into_raw()
}

#[no_mangle]
Expand Down
24 changes: 12 additions & 12 deletions conf.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
tls_active: true
proxy_address: "www.google.com"
ssl_certificate: jequi/test/leaf-cert.pem
ssl_key: jequi/test/leaf-cert.key
go_library_path: "jequi_go.so"
static_files_path: "test/"
go_library_path: "./jequi_go.so"
host:
jequi.com:
ssl_certificate: jequi/test/leaf-cert.pem
ssl_key: jequi/test/leaf-cert.key
http2: true
chunk_size: 4000
uri:
/api:
go_library_path: "jequi_go.so"
/app:
static_files_path: "test/host/uri/"
go_library_path: "jequi_go.so"
proxy_address: "www.google.com"
path:
/api/:
go_library_path: "./jequi_go.so"
/app/:
static_files_path: "test/host/path/"
go_library_path: "./jequi_go.so"
proxy_address: ["www.google.com","https://github.com"]
# static_files_path: "test/"
uri:
/api/v2:
go_library_path: "jequi_go.so"
path:
/api/v2/:
go_library_path: "./jequi_go.so"
115 changes: 77 additions & 38 deletions jequi/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use std::{any::Any, collections::HashMap, sync::Arc};
use core::panic;
use std::{
any::Any,
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
};

use serde::Deserialize;
use serde_yaml::from_reader;
Expand Down Expand Up @@ -48,7 +54,7 @@ fn merge_config_and_load_plugins(
// merge_yaml(&mut config_parser, config_to_merge);
let config_parser = config_to_merge;
if let Value::Mapping(ref mut config_parser) = config_parser {
config_parser.insert(key_to_add.into(), value_to_add.into());
config_parser.insert(format!("config_{}", key_to_add).into(), value_to_add.into());
}
load_plugins(config_parser)
}
Expand All @@ -68,88 +74,89 @@ impl ConfigMap {
host.clone(),
load_plugins,
);
let mut uri_config: Option<HashMap<String, Vec<Plugin>>> = None;
for (uri, mut uri_config_parser) in host_config_parser.uri.into_iter().flatten() {
let mut path_config: Option<HashMap<PathBuf, Vec<Plugin>>> = None;
for (path, mut path_config_parser) in host_config_parser.path.into_iter().flatten() {
let mut config_parser = config_parser.clone();
let plugin_list = merge_config_and_load_plugins(
&mut config_parser,
&mut uri_config_parser,
"uri",
uri.clone(),
&mut path_config_parser,
"path",
path.to_string_lossy().to_string(),
load_plugins,
);
uri_config.get_or_insert_default().insert(uri, plugin_list);
path_config
.get_or_insert_default()
.insert(path, plugin_list);
}
main_conf.host.get_or_insert_default().insert(
host,
HostConfig {
uri: uri_config,
path: path_config,
config: plugin_list,
},
);
}

for (uri, mut uri_config_parser) in main_conf_parser.uri.into_iter().flatten() {
for (path, mut path_config_parser) in main_conf_parser.path.into_iter().flatten() {
let mut config_parser = config_parser.clone();
let plugin_list = merge_config_and_load_plugins(
&mut config_parser,
&mut uri_config_parser,
"uri",
uri.clone(),
&mut path_config_parser,
"path",
path.to_string_lossy().to_string(),
load_plugins,
);
main_conf
.uri
.path
.get_or_insert_default()
.insert(uri, plugin_list);
.insert(path, plugin_list);
}

main_conf.config = load_plugins(&config_parser);
main_conf
}

pub fn get_config_for_request(&self, host: Option<&str>, uri: Option<&str>) -> &Vec<Plugin> {
pub fn get_config_for_request(&self, host: Option<&str>, path: Option<&str>) -> &Vec<Plugin> {
let mut config = &self.config;
let mut uri_map = &self.uri;
let mut path_map = &self.path;
if let Some(host_map) = &self.host
&& let Some(host) = host
&& let Some(host_config) = host_map.get(host.split(':').next().unwrap())
{
config = &host_config.config;
uri_map = &host_config.uri;
path_map = &host_config.path;
}

let uri = match uri {
Some(uri) => uri,
let path = match path {
Some(path) => path,
None => return config,
};

let uri_map = match uri_map {
Some(uri_map) => {
if uri_map.is_empty() {
let path_map = match path_map {
Some(path_map) => {
if path_map.is_empty() {
return config;
}
uri_map
path_map
}
None => return config,
};

let mut uri: &str = uri;
if let Some(i) = uri.find('?') {
uri = &uri[..i];
}
let mut path = PathBuf::from(path);

if let Some(config) = uri_map.get(uri) {
if let Some(config) = path_map.get(&path) {
return config;
}

while let Some(i) = uri.rfind('/') {
uri = &uri[..i];
if let Some(config) = uri_map.get(uri) {
loop {
if let Some(config) = path_map.get(&path) {
return config;
}

if !path.pop() {
return config;
}
}
config
}
}

Expand All @@ -165,7 +172,7 @@ impl ConfigMapParser {

#[cfg(test)]
mod tests {
use std::{sync::Arc, vec};
use std::{path::Path, sync::Arc};

use crate::{load_plugin, Config, ConfigMap, ConfigMapParser, JequiConfig, Plugin};

Expand Down Expand Up @@ -205,11 +212,27 @@ mod tests {
let host_jequi_com = config_map.host.as_ref().unwrap().get("jequi.com").unwrap();
assert_eq!(get_config(&host_jequi_com.config).ip, "1.1.2.1");
assert_eq!(
get_config(host_jequi_com.uri.as_ref().unwrap().get("/app").unwrap()).ip,
get_config(
host_jequi_com
.path
.as_ref()
.unwrap()
.get(Path::new("/app"))
.unwrap()
)
.ip,
"1.1.2.2"
);
assert_eq!(
get_config(host_jequi_com.uri.as_ref().unwrap().get("/api").unwrap()).ip,
get_config(
host_jequi_com
.path
.as_ref()
.unwrap()
.get(Path::new("/api"))
.unwrap()
)
.ip,
"1.1.2.3"
);
assert_eq!(
Expand All @@ -226,11 +249,27 @@ mod tests {
"1.1.3.1"
);
assert_eq!(
get_config(config_map.uri.as_ref().unwrap().get("/app").unwrap()).ip,
get_config(
config_map
.path
.as_ref()
.unwrap()
.get(Path::new("/app"))
.unwrap()
)
.ip,
"1.2.1.1"
);
assert_eq!(
get_config(config_map.uri.as_ref().unwrap().get("/test").unwrap()).ip,
get_config(
config_map
.path
.as_ref()
.unwrap()
.get(Path::new("/test"))
.unwrap()
)
.ip,
"1.2.1.2"
);

Expand Down
Loading

0 comments on commit ad6bb43

Please sign in to comment.