url prefix processing

This commit is contained in:
realaravinth 2021-04-11 23:23:14 +05:30
parent 517ce4973f
commit 0d1ba0d150
No known key found for this signature in database
GPG Key ID: AD9F0F08E855ED88
5 changed files with 62 additions and 3 deletions

15
Cargo.lock generated
View File

@ -60,6 +60,20 @@ dependencies = [
"trust-dns-resolver", "trust-dns-resolver",
] ]
[[package]]
name = "actix-cors"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36b133d8026a9f209a9aeeeacd028e7451bcca975f592881b305d37983f303d7"
dependencies = [
"actix-web",
"derive_more",
"futures-util",
"log",
"once_cell",
"tinyvec",
]
[[package]] [[package]]
name = "actix-http" name = "actix-http"
version = "2.2.0" version = "2.2.0"
@ -1279,6 +1293,7 @@ name = "guard"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"actix", "actix",
"actix-cors",
"actix-http", "actix-http",
"actix-identity", "actix-identity",
"actix-rt", "actix-rt",

View File

@ -27,6 +27,9 @@ actix = "0.10"
actix-identity = "0.3" actix-identity = "0.3"
actix-http = "2.2" actix-http = "2.2"
actix-rt = "1" actix-rt = "1"
actix-cors= "0.5.4"
mime_guess = "2.0.3" mime_guess = "2.0.3"
rust-embed = "5.9.0" rust-embed = "5.9.0"
cache-buster = { version = "0.1", git = "https://github.com/realaravinth/cache-buster" } cache-buster = { version = "0.1", git = "https://github.com/realaravinth/cache-buster" }

View File

@ -28,9 +28,7 @@ ip= "0.0.0.0"
# enter your hostname, eg: example.com # enter your hostname, eg: example.com
domain = "localhost" domain = "localhost"
allow_registration = true allow_registration = true
# directory containing static files #url_prefix = ""
static_files_dir = "./frontend/dist"
[pow] [pow]
# Please set a unique value, your mCaptcha instance's security depends on this being # Please set a unique value, your mCaptcha instance's security depends on this being

View File

@ -15,6 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
//use actix_cors::Cors;
//use lazy_static::lazy_static;
pub mod get_config; pub mod get_config;
pub mod verify_pow; pub mod verify_pow;
pub mod verify_token; pub mod verify_token;
@ -22,3 +25,12 @@ pub mod verify_token;
pub use super::mcaptcha::duration::GetDurationResp; pub use super::mcaptcha::duration::GetDurationResp;
pub use super::mcaptcha::is_authenticated; pub use super::mcaptcha::is_authenticated;
pub use super::mcaptcha::levels::I32Levels; pub use super::mcaptcha::levels::I32Levels;
//lazy_static! {
// pub static ref CORS: Cors = Cors::default()
// .allow_any_origin()
// .allowed_methods(vec!["POST"])
// .allow_any_header()
// .max_age(0)
// .send_wildcard();
//}

View File

@ -29,6 +29,7 @@ pub struct Server {
pub domain: String, pub domain: String,
pub cookie_secret: String, pub cookie_secret: String,
pub ip: String, pub ip: String,
pub url_prefix: Option<String>,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -42,6 +43,15 @@ impl Server {
pub fn get_ip(&self) -> String { pub fn get_ip(&self) -> String {
format!("{}:{}", self.ip, self.port) format!("{}:{}", self.ip, self.port)
} }
fn check_url_prefix(&mut self) {
if let Some(prefix) = self.url_prefix.clone() {
self.url_prefix = Some(prefix.trim().into());
if prefix.trim().is_empty() {
panic!("URL prefix is set to empty string")
}
}
}
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -162,3 +172,24 @@ fn set_database_url(s: &mut Config) {
) )
.expect("Couldn't set databse url"); .expect("Couldn't set databse url");
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_prefix_test() {
let mut settings = Settings::new().unwrap();
assert!(settings.server.url_prefix.is_none());
settings.server.url_prefix = Some("test".into());
settings.server.check_url_prefix();
}
#[test]
#[should_panic]
fn url_prefix_panic_test() {
let mut settings = Settings::new().unwrap();
settings.server.url_prefix = Some(" ".into());
settings.server.check_url_prefix();
}
}