Skip to content
Snippets Groups Projects
Unverified Commit 7fecc3cf authored by Louis's avatar Louis :fire:
Browse files

Implement config routes support, get lua runtime from the request

parent 616e2297
No related branches found
No related tags found
1 merge request!2Scripting
...@@ -2,16 +2,17 @@ field_handling: Log ...@@ -2,16 +2,17 @@ field_handling: Log
file_handling: File file_handling: File
server: server:
port: 9000 port: 9000
requests: routes:
- /users/@user_id : scripts/get_user_by_id.dyon - route: /users/@user_id
- /users : script: scripts/get_user_by_id.dyon
- route: /users
response:
failure_rate: 5 failure_rate: 5
response_headers: headers:
x-rate-limit: 100 x-rate-limit: 100
x-rate-remaining: 96 x-rate-remaining: 96
x-rate-reset: 10/12/1990 x-rate-reset: 10/12/1990
response_type: application/json body:
response_body:
count: 2 count: 2
data: data:
- id: 1 - id: 1
......
...@@ -7,6 +7,8 @@ use std::default::Default; ...@@ -7,6 +7,8 @@ use std::default::Default;
use serde::{Deserialize, Deserializer, de}; use serde::{Deserialize, Deserializer, de};
use std::fmt; use std::fmt;
use serde_yaml as yaml; use serde_yaml as yaml;
use std::collections::HashMap;
use cli;
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum HandlerMethod { pub enum HandlerMethod {
...@@ -52,6 +54,8 @@ pub struct SwerveConfig { ...@@ -52,6 +54,8 @@ pub struct SwerveConfig {
pub file_handling: HandlerMethod, pub file_handling: HandlerMethod,
#[serde(default)] #[serde(default)]
pub server: ServerOptions, pub server: ServerOptions,
#[serde(default="get_empty_routes")]
pub routes: Vec<cli::RouteHandler>,
} }
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
...@@ -74,12 +78,15 @@ fn get_default_address() -> String { String::from("localhost") } ...@@ -74,12 +78,15 @@ fn get_default_address() -> String { String::from("localhost") }
fn get_default_quiet_attr() -> bool { false } fn get_default_quiet_attr() -> bool { false }
fn get_default_index_attr() -> bool { false } fn get_default_index_attr() -> bool { false }
fn get_empty_routes() -> Vec<cli::RouteHandler> { vec![] }
impl Default for SwerveConfig { impl Default for SwerveConfig {
fn default() -> Self { fn default() -> Self {
SwerveConfig { SwerveConfig {
field_handling: HandlerMethod::Log, field_handling: HandlerMethod::Log,
file_handling: HandlerMethod::Log, file_handling: HandlerMethod::Log,
server: ServerOptions::default(), server: ServerOptions::default(),
routes: get_empty_routes(),
} }
} }
} }
......
use std::collections::HashMap;
#[derive(Clone, Debug, Deserialize)]
pub struct RouteHandler {
route: String,
response: Option<ResponseHandler>,
script: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ResponseHandler {
failure_rate: Option<u32>,
#[serde(default="get_default_headers")]
headers: HashMap<String, String>,
}
fn get_default_headers() -> HashMap<String, String> { HashMap::new() }
mod cli; mod cli;
mod config_file; mod config_file;
mod config_routes;
pub mod gpl; pub mod gpl;
pub use self::cli::{Args, USAGE}; pub use self::cli::{Args, USAGE};
pub use self::config_file::{HandlerMethod, SwerveConfig}; pub use self::config_file::{HandlerMethod, SwerveConfig};
\ No newline at end of file pub use self::config_routes::RouteHandler;
\ No newline at end of file
...@@ -2,7 +2,7 @@ use rocket::{self, Outcome, http, Response}; ...@@ -2,7 +2,7 @@ use rocket::{self, Outcome, http, Response};
use rocket::request::{FromRequest, Request}; use rocket::request::{FromRequest, Request};
use rocket::http::ContentType; use rocket::http::ContentType;
use hyper::header::Headers; use hyper::header::Headers;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf, Component};
use std::io::BufReader; use std::io::BufReader;
use std::fs::File; use std::fs::File;
...@@ -57,3 +57,74 @@ impl rocket::response::Responder<'static> for TypedFile { ...@@ -57,3 +57,74 @@ impl rocket::response::Responder<'static> for TypedFile {
Ok(response) Ok(response)
} }
} }
pub struct RequestPath(pub String);
impl <'a, 'req>FromRequest<'a, 'req> for RequestPath {
type Error = ();
fn from_request(request: &'a Request<'req>) -> Outcome<Self, (http::Status, ()), ()> {
let uri = request.uri();
Outcome::Success(RequestPath(String::from(uri.path())))
}
}
pub mod path {
use std::path as std_path;
use std::option::Option::*;
pub struct MatchablePath(pub String);
pub type PathMatch = (String, String);
pub type MatchList = Vec<PathMatch>;
pub type MatchResult = Option<MatchList>;
impl MatchablePath {
pub fn from<T>(src: T) -> Self where T: ToString {
MatchablePath(src.to_string())
}
fn path_to_vec<T: ToString>(string: T) -> Vec<String> {
std_path::PathBuf::from(string.to_string())
.components()
.filter_map(|c| match c {
std_path::Component::Normal(cmp) => cmp.to_str()
.and_then(|s| Some(String::from(s))),
_ => None,
})
.collect()
}
pub fn matches<T>(&self, other: T) -> MatchResult where T: ToString {
let this_path = MatchablePath::path_to_vec(&self.0);
let other_path = MatchablePath::path_to_vec(other);
if this_path.len() == other_path.len() {
let parts_match = this_path.iter()
.zip(other_path.iter())
.fold(true, |acc, (this, other)| {
acc && (this.starts_with("@") || this == other)
});
if parts_match {
Some(this_path.iter()
.zip(other_path.iter())
.filter_map(|(this, other)| {
if this.starts_with("@") {
Some((
this.chars().skip(1).collect::<String>(),
other.clone()
))
} else {
None
}
})
.collect()
)
} else {
None
}
} else {
None
}
}
}
}
use scripting::run_script; use scripting::run_script;
use std::path::PathBuf; use std::path::PathBuf;
use routing::request;
use server::LuaRuntime;
use rlua::{Lua};
#[get("/__testing__/run-script")] #[get("/__testing__/run-script")]
pub fn route_script() -> String { pub fn route_script(path: request::RequestPath, runtime: LuaRuntime) -> String {
let lua: Lua = runtime.into();
let doowap = path.0;
let foo = request::path::MatchablePath(String::from("/inspection/@id"));
let matches = foo.matches(String::from("/inspection/123"));
println!("{:?}", matches);
let path = PathBuf::from("example/.swerve/get_user_by_id.rhai"); let path = PathBuf::from("example/.swerve/get_user_by_id.rhai");
run_script(path).unwrap_or(String::from("No script")) run_script(path).unwrap_or(String::from("No script"))
} }
\ No newline at end of file
use rlua::{Lua}; use rlua::{Lua};
use rocket::{self, Outcome, http, Response};
use rocket::request::{FromRequest, Request};
use std::convert::{Into, AsRef, AsMut};
pub fn create_runtime(with_debug: bool) -> Lua { pub struct LuaRuntime(Lua);
impl Into<Lua> for LuaRuntime {
fn into(self) -> Lua {
self.0
}
}
impl AsRef<Lua> for LuaRuntime {
fn as_ref(&self) -> &Lua {
&self.0
}
}
impl AsMut<Lua> for LuaRuntime {
fn as_mut(&mut self) -> &mut Lua {
&mut self.0
}
}
impl <'a, 'req>FromRequest<'a, 'req> for LuaRuntime {
type Error = ();
fn from_request(_request: &'a Request<'req>) -> Outcome<Self, (http::Status, ()), ()> {
Outcome::Success(create_runtime(false))
}
}
pub fn create_runtime(with_debug: bool) -> LuaRuntime {
let runtime = if with_debug { let runtime = if with_debug {
unsafe { Lua::new_with_debug() } unsafe { Lua::new_with_debug() }
} else { } else {
...@@ -9,5 +37,5 @@ pub fn create_runtime(with_debug: bool) -> Lua { ...@@ -9,5 +37,5 @@ pub fn create_runtime(with_debug: bool) -> Lua {
// Customise runtime here // Customise runtime here
runtime LuaRuntime(runtime)
} }
\ No newline at end of file
...@@ -2,4 +2,4 @@ mod server; ...@@ -2,4 +2,4 @@ mod server;
mod lua; mod lua;
pub use self::server::create_server; pub use self::server::create_server;
pub use self::lua::create_runtime; pub use self::lua::{LuaRuntime, create_runtime};
\ No newline at end of file \ No newline at end of file
use rocket::{self, Rocket, Config}; use rocket::{self, Rocket, Config};
use cli::{Args, SwerveConfig}; use cli::{Args, SwerveConfig};
use routing; use routing;
use server;
pub fn create_server(args: Args, config: SwerveConfig) -> Rocket { pub fn create_server(args: Args, config: SwerveConfig) -> Rocket {
let server_config = server_config_from_input(args.clone(), config.clone()); let server_config = server_config_from_input(args.clone(), config.clone());
...@@ -37,9 +36,6 @@ pub fn create_server(args: Args, config: SwerveConfig) -> Rocket { ...@@ -37,9 +36,6 @@ pub fn create_server(args: Args, config: SwerveConfig) -> Rocket {
})); }));
} }
let lua_runtime = server::create_runtime(false);
server = server.manage(lua_runtime);
server server
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment