Add CORS and authorized origins to server
This commit is contained in:
parent
57bc676ec1
commit
a2a2863d62
@ -1,3 +1,5 @@
|
|||||||
|
ORDABOK_HOSTS=https://example.com # if empty or unset, CORS will allow all origins
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
POSTGRES_HOST=0.0.0.0
|
POSTGRES_HOST=0.0.0.0
|
||||||
POSTGRES_PASSWORD=changeme
|
POSTGRES_PASSWORD=changeme
|
||||||
|
12
Cargo.toml
12
Cargo.toml
@ -6,8 +6,18 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
# Environment variables
|
||||||
|
dotenvy = "0.15"
|
||||||
|
|
||||||
|
# Database
|
||||||
diesel = { version = "2.0", features = ["postgres", "chrono"] }
|
diesel = { version = "2.0", features = ["postgres", "chrono"] }
|
||||||
diesel-derive-enum = { version = "2.0.0-rc.0", features = ["postgres"] }
|
diesel-derive-enum = { version = "2.0.0-rc.0", features = ["postgres"] }
|
||||||
chrono = "0.4.23"
|
chrono = "0.4.23"
|
||||||
|
|
||||||
dotenvy = "0.15"
|
# Web server
|
||||||
|
rocket = "0.5.0-rc.2"
|
||||||
|
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", rev = "c17e814" }
|
||||||
|
|
||||||
|
# logging
|
||||||
|
tracing = "0.1.37"
|
||||||
|
tracing-subscriber = "0.3.16"
|
@ -1,5 +1,5 @@
|
|||||||
|
use super::super::schema::{langandagents, languages};
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use super::super::schema::{languages, langandagents};
|
|
||||||
|
|
||||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||||
#[DieselTypePath = "crate::db::schema::sql_types::Release"]
|
#[DieselTypePath = "crate::db::schema::sql_types::Release"]
|
||||||
@ -7,7 +7,7 @@ pub enum Release {
|
|||||||
Public,
|
Public,
|
||||||
NonCommercial,
|
NonCommercial,
|
||||||
Research,
|
Research,
|
||||||
Private
|
Private,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||||
@ -19,14 +19,14 @@ pub enum DictGenre {
|
|||||||
Specialized,
|
Specialized,
|
||||||
Historical,
|
Historical,
|
||||||
Orthography,
|
Orthography,
|
||||||
Terminology
|
Terminology,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||||
#[DieselTypePath = "crate::db::schema::sql_types::Agentlanguagerelation"]
|
#[DieselTypePath = "crate::db::schema::sql_types::Agentlanguagerelation"]
|
||||||
pub enum AgentLanguageRelation {
|
pub enum AgentLanguageRelation {
|
||||||
Publisher,
|
Publisher,
|
||||||
Author
|
Author,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
use diesel::prelude::*;
|
|
||||||
use super::super::schema::{wordrelation, words};
|
use super::super::schema::{wordrelation, words};
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||||
#[DieselTypePath = "crate::db::schema::sql_types::Wordrelationship"]
|
#[DieselTypePath = "crate::db::schema::sql_types::Wordrelationship"]
|
||||||
pub enum WordRelationship {
|
pub enum WordRelationship {
|
||||||
Definition,
|
Definition,
|
||||||
Related
|
Related,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
#[derive(diesel_derive_enum::DbEnum, Debug, Clone, PartialEq, Eq)]
|
||||||
@ -27,7 +27,7 @@ pub enum PartOfSpeech {
|
|||||||
SubjConj,
|
SubjConj,
|
||||||
Symbol,
|
Symbol,
|
||||||
Verb,
|
Verb,
|
||||||
Other
|
Other,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
|
||||||
|
57
src/main.rs
57
src/main.rs
@ -1,5 +1,58 @@
|
|||||||
|
#![warn(clippy::style, clippy::pedantic)]
|
||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
|
|
||||||
fn main() {
|
use std::{env, error::Error};
|
||||||
println!("Hello, world!");
|
|
||||||
|
use dotenvy::dotenv;
|
||||||
|
use tracing::{debug, info, Level};
|
||||||
|
use tracing_subscriber::FmtSubscriber;
|
||||||
|
|
||||||
|
fn setup_logging() {
|
||||||
|
let subscriber = FmtSubscriber::builder()
|
||||||
|
.with_max_level(Level::DEBUG)
|
||||||
|
.finish();
|
||||||
|
tracing::subscriber::set_global_default(subscriber)
|
||||||
|
.expect("Setting default subscriber failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_cors() -> Result<rocket_cors::Cors, rocket_cors::Error> {
|
||||||
|
use rocket_cors::{AllowedHeaders, AllowedOrigins};
|
||||||
|
use std::str::FromStr;
|
||||||
|
rocket_cors::CorsOptions {
|
||||||
|
allowed_methods: ["Get", "Post"]
|
||||||
|
.iter()
|
||||||
|
.map(|s| FromStr::from_str(s).unwrap())
|
||||||
|
.collect(),
|
||||||
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
||||||
|
allowed_origins: match env::var("ORDABOK_HOSTS") {
|
||||||
|
Ok(val) => {
|
||||||
|
if val.is_empty() {
|
||||||
|
AllowedOrigins::all()
|
||||||
|
} else {
|
||||||
|
AllowedOrigins::some_exact(
|
||||||
|
val.split(',').collect::<Vec<_>>().as_ref(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => AllowedOrigins::all(),
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.to_cors()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rocket::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
setup_logging();
|
||||||
|
|
||||||
|
info!("Reading environment variables");
|
||||||
|
dotenv().ok();
|
||||||
|
|
||||||
|
let cors = make_cors()?;
|
||||||
|
debug!("CORS: {:?}", cors);
|
||||||
|
|
||||||
|
#[allow(clippy::let_underscore_drop)]
|
||||||
|
let _ = rocket::build().attach(cors).launch().await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user