mirror of
https://github.com/MedzikUser/HomeDisk.git
synced 2024-08-14 21:46:53 +00:00
feat: add support for disable https to run http server
This commit is contained in:
parent
1b9529223a
commit
ff02a1e180
3 changed files with 42 additions and 20 deletions
|
@ -1,6 +1,7 @@
|
||||||
[http]
|
[http]
|
||||||
host = "0.0.0.0"
|
host = "0.0.0.0"
|
||||||
httpPort = 8080 # http server port (recommended 80)
|
httpPort = 8080 # http server port (recommended 80)
|
||||||
|
enableHttps = true # if true start https server and http will be redirect to https
|
||||||
httpsPort = 8443 # https server port (recommended 443)
|
httpsPort = 8443 # https server port (recommended 443)
|
||||||
cors = [ "localhost:8000" ] # CORS domains
|
cors = [ "localhost:8000" ] # CORS domains
|
||||||
tlsCert = "./cert.pem" # TLS certificate file
|
tlsCert = "./cert.pem" # TLS certificate file
|
||||||
|
|
|
@ -14,6 +14,7 @@ pub struct Config {
|
||||||
pub struct ConfigHTTP {
|
pub struct ConfigHTTP {
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub http_port: u16,
|
pub http_port: u16,
|
||||||
|
pub enable_https: bool,
|
||||||
pub https_port: u16,
|
pub https_port: u16,
|
||||||
pub cors: Vec<String>,
|
pub cors: Vec<String>,
|
||||||
pub tls_cert: String,
|
pub tls_cert: String,
|
||||||
|
|
|
@ -2,7 +2,10 @@ mod api;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
|
|
||||||
use std::{path::{PathBuf, Path}, process::exit};
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
process::exit,
|
||||||
|
};
|
||||||
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use axum::{
|
use axum::{
|
||||||
|
@ -18,21 +21,28 @@ use tower_http::{
|
||||||
cors::{AllowOrigin, CorsLayer},
|
cors::{AllowOrigin, CorsLayer},
|
||||||
BoxError,
|
BoxError,
|
||||||
};
|
};
|
||||||
use tracing::{debug, info, error};
|
use tracing::{debug, error, info};
|
||||||
|
|
||||||
use crate::{config::Config, database::Database};
|
use crate::{config::Config, database::Database};
|
||||||
|
|
||||||
pub async fn start_server(config: Config, db: Database) -> anyhow::Result<()> {
|
pub async fn start_server(config: Config, db: Database) -> anyhow::Result<()> {
|
||||||
let host = format!("{}:{}", config.http.host, config.http.https_port);
|
let host = if config.http.enable_https {
|
||||||
|
format!("{}:{}", config.http.host, config.http.https_port)
|
||||||
|
} else {
|
||||||
|
format!("{}:{}", config.http.host, config.http.http_port)
|
||||||
|
};
|
||||||
|
|
||||||
// check if tls cert and key file exists
|
if config.http.enable_https {
|
||||||
if !Path::new(&config.http.tls_cert).exists() || !Path::new(&config.http.tls_key).exists() {
|
// check if tls cert and key file exists
|
||||||
error!("TLS cert or/and key file not found!");
|
if !Path::new(&config.http.tls_cert).exists() || !Path::new(&config.http.tls_key).exists() {
|
||||||
exit(1);
|
error!("TLS cert or/and key file not found!");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start http redirect to https
|
||||||
|
tokio::spawn(redirect_http_to_https(config.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
tokio::spawn(redirect_http_to_https(config.clone()));
|
|
||||||
|
|
||||||
info!("🚀 Server has launched on https://{host}");
|
info!("🚀 Server has launched on https://{host}");
|
||||||
|
|
||||||
// change the type from Vec<String> to Vec<HeaderValue> so that the http server can correctly detect CORS hosts
|
// change the type from Vec<String> to Vec<HeaderValue> so that the http server can correctly detect CORS hosts
|
||||||
|
@ -43,23 +53,33 @@ pub async fn start_server(config: Config, db: Database) -> anyhow::Result<()> {
|
||||||
.map(|e| e.parse().expect("Failed to parse CORS hosts"))
|
.map(|e| e.parse().expect("Failed to parse CORS hosts"))
|
||||||
.collect::<Vec<HeaderValue>>();
|
.collect::<Vec<HeaderValue>>();
|
||||||
|
|
||||||
let tls_config = RustlsConfig::from_pem_file(
|
|
||||||
PathBuf::from("").join("").join(&config.http.tls_cert),
|
|
||||||
PathBuf::from("").join("").join(&config.http.tls_key),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.nest("/api", api::app())
|
.nest("/api", api::app())
|
||||||
.route("/", get(api::health))
|
.route("/", get(api::health))
|
||||||
.layer(CorsLayer::new().allow_origin(AllowOrigin::list(origins)))
|
.layer(CorsLayer::new().allow_origin(AllowOrigin::list(origins)))
|
||||||
.layer(Extension(config))
|
.layer(Extension(config.clone()))
|
||||||
.layer(Extension(db));
|
.layer(Extension(db));
|
||||||
|
|
||||||
axum_server::bind_rustls(host.parse()?, tls_config)
|
// if https is enabled, run it, otherwise run server http
|
||||||
.serve(app.into_make_service())
|
if config.http.enable_https {
|
||||||
.await?;
|
// start https server
|
||||||
|
let tls_config = RustlsConfig::from_pem_file(
|
||||||
|
PathBuf::from("").join("").join(&config.http.tls_cert),
|
||||||
|
PathBuf::from("").join("").join(&config.http.tls_key),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
axum_server::bind_rustls(host.parse()?, tls_config)
|
||||||
|
.serve(app.into_make_service())
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
// start http server
|
||||||
|
axum::Server::bind(&host.parse().unwrap())
|
||||||
|
.serve(app.into_make_service())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
Err(anyhow!("Server unexpected stopped!"))
|
Err(anyhow!("Server unexpected stopped!"))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue