Skip to content

Commit

Permalink
Revert "add ssl toggle config"
Browse files Browse the repository at this point in the history
This reverts commit ba753d1.
  • Loading branch information
nramos0 committed Oct 22, 2022
1 parent e2bbfe8 commit b87bdc4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
8 changes: 3 additions & 5 deletions backend/src/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct ServerConfig {
pub host: String,
pub port: i32,
pub port_ssl: i32,
pub enable_ssl: bool,
pub secret: String,
pub token_time: i64,
pub salt: String,
Expand All @@ -27,9 +26,8 @@ pub struct AppConfig {

impl AppConfig {
pub fn from_env() -> Result<Self, ConfigError> {
config::Config::builder()
.add_source(config::File::new(".env", config::FileFormat::Ini))
.build()
.and_then(|config| config.try_deserialize())
let mut cfg = config::Config::new();
cfg.merge(config::Environment::new())?;
cfg.try_into()
}
}
42 changes: 20 additions & 22 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ async fn main() -> std::io::Result<()> {
}

let config = AppConfig::from_env();
let config = config.unwrap_or_else(|err| {
eprintln!("Error parsing config: {}", err);
process::exit(0);
});
let config = {
if config.is_ok() {
config.unwrap()
} else {
eprintln!("Error parsing config: {}", config.unwrap_err());
process::exit(0);
}
};

println!(
"Starting server at http://{0}:{1}/",
Expand All @@ -51,7 +55,13 @@ async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let json_config = web::JsonConfig::default().limit(config.server.json_max_size);

let server = HttpServer::new(move || {
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file("key.pem", SslFiletype::PEM)
.unwrap();
builder.set_certificate_chain_file("cert.pem").unwrap();

HttpServer::new(move || {
let cors = Cors::default()
.send_wildcard()
.allow_any_origin()
Expand All @@ -64,9 +74,7 @@ async fn main() -> std::io::Result<()> {
]);

App::new()
.wrap(NormalizePath::new(
actix_web::middleware::normalize::TrailingSlash::Always,
))
.wrap(NormalizePath::default())
.wrap(Logger::default())
.wrap(cors)
.app_data(json_config.clone())
Expand Down Expand Up @@ -101,18 +109,8 @@ async fn main() -> std::io::Result<()> {
.service(user::data::delete_mark)
.service(status)
})
.bind(address)?;

let server = if config.server.enable_ssl {
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
builder
.set_private_key_file("key.pem", SslFiletype::PEM)
.unwrap();
builder.set_certificate_chain_file("cert.pem").unwrap();
server.bind_openssl(&ssl_address[..], builder)?
} else {
server
};

server.run().await
.bind(address)?
.bind_openssl(&ssl_address[..], builder)?
.run()
.await
}

0 comments on commit b87bdc4

Please sign in to comment.