Commit 04d383a4 authored by Emmanuel Raviart's avatar Emmanuel Raviart

Merge branch 'feature/dotenv' into 'master'

Feature/dotenv

See merge request !8
parents 233d2dd9 e2170600
Pipeline #110657 passed with stage
in 3 minutes and 56 seconds
ALLOWED_HOSTS=https://assemblee.api.tricoteuses.fr
\ No newline at end of file
/target
**/*.rs.bk
.env
\ No newline at end of file
This diff is collapsed.
[api]
allowed_origins = [
"https://assemblee.api.tricoteuses.fr",
"http://localhost:3000",
"http://localhost:3001",
"http://localhost:8000",
......
......@@ -18,5 +18,6 @@ rocket_cors = "0.4"
serde = "1"
serde_derive = "1"
serde_json = "1"
dotenv = "0.13.0"
tricoteuses_api_assemblee_config = { path = "../config-lib" }
tricoteuses_api_assemblee_data = { path = "../data-lib" }
\ No newline at end of file
......@@ -9,6 +9,7 @@ extern crate juniper;
extern crate juniper_rocket;
#[macro_use]
extern crate rocket;
extern crate dotenv;
extern crate rocket_cors;
extern crate serde;
extern crate serde_json;
......@@ -23,11 +24,13 @@ use data::{
Mandat, Organe, Reunion, Scrutin, Statut, TexteLeg, TextesEtAmendements, ALL_DATASETS,
FONCTIONS_ORGANES,
};
use dotenv::dotenv;
use juniper::{EmptyMutation, FieldResult, RootNode};
use rocket::http::Method;
use rocket::response::{content, NamedFile};
use rocket::State;
use rocket_cors::AllowedOrigins;
use std::env;
use std::path::{Path, PathBuf};
struct Query;
......@@ -585,17 +588,39 @@ fn main() {
return;
}
// Start Rocket web server.
// gets allowed_origins from config file
let mut allowed_extended_origins: Vec<String> = config.api.allowed_origins;
// collect allowed origins from dotenv config file
dotenv().ok();
// check if variable is acceptable
let has_additional_allowed_hosts = env::var("ALLOWED_HOSTS").is_ok();
if has_additional_allowed_hosts {
match env::var("ALLOWED_HOSTS") {
Ok(hosts_values) => {
let hosts: Vec<&str> = hosts_values.split(',').collect();
for host in hosts {
allowed_extended_origins.push(host.to_string());
}
}
Err(_value) => {
println!("{:?}", _value);
}
}
}
let (allowed_origins, failed_origins) = AllowedOrigins::some(
config
.api
.allowed_origins
allowed_extended_origins
.iter()
.map(String::as_ref)
.collect::<Vec<&str>>()
.as_slice(),
);
assert!(failed_origins.is_empty());
let cors = rocket_cors::Cors {
allowed_origins,
allowed_methods: vec![Method::Get, Method::Post]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment