Skip to content

Commit

Permalink
add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
timzaak committed Jun 28, 2024
1 parent 907d6c3 commit 2f28ed7
Show file tree
Hide file tree
Showing 17 changed files with 464 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/spa-server-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: Swatinem/rust-cache@v2
- name: run integration test
# --show-output
run: cargo test -p tests --test starter -j 1 -- --test-threads 1
run: cargo test -p tests --test http_test -j 1 -- --test-threads 1
- name: run pebble
run: ./run_pebble.sh
working-directory: ./tests/bash/
Expand Down
10 changes: 6 additions & 4 deletions config.release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ addr = "0.0.0.0"
## port when serving public PI,default is http port. external_port should not be 0.
# external_port = 80

## optional, when https enabled, redirect_https default value is true
# redirect_https = false
## optional, when https enabled, redirect_https default value true
## it would the port would be https.external_port(https.external_port should be defined), otherwise is false
# redirect_https = true

# [https]
# port = 443 # https bind address
Expand Down Expand Up @@ -89,8 +90,9 @@ addr = "0.0.0.0"
# alias = ["example.com"]
# cors = false
# [domains.https]
## optional, default is `http.redirect_https` value.
# redirect_https = false
## optional, when https enabled, redirect_https default value true
## it would the port would be https.external_port(https.external_port should be defined), otherwise is false
# redirect_https = 443

## this would be usefully when set https.acme
# disable_acme = false
Expand Down
44 changes: 39 additions & 5 deletions server/src/admin_server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::HashMap;
use crate::acme::ACMEManager;
use crate::admin_server::request::{
DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption,
GetDomainOption, GetDomainPositionOption, UpdateUploadingStatusOption, UploadFileOption,
};
use crate::config::AdminConfig;
use crate::config::{AdminConfig, get_host_path_from_domain};
use crate::domain_storage::DomainStorage;
use crate::hot_reload::HotReloadManager;
use crate::with;
Expand All @@ -15,14 +16,15 @@ use std::str::FromStr;
use std::sync::Arc;
use warp::multipart::FormData;
use warp::reply::Response;
use warp::{Filter, Rejection};
use warp::{Filter, Rejection, Reply};

pub struct AdminServer {
conf: Arc<AdminConfig>,
domain_storage: Arc<DomainStorage>,
reload_manager: Arc<HotReloadManager>,
acme_manager: Arc<ACMEManager>,
delay_timer: DelayTimer,
host_alias: Arc<HashMap<String, String>>,
}

impl AdminServer {
Expand All @@ -32,13 +34,15 @@ impl AdminServer {
reload_manager: HotReloadManager,
acme_manager: Arc<ACMEManager>,
delay_timer: DelayTimer,
host_alias: Arc<HashMap<String, String>>
) -> Self {
AdminServer {
conf: Arc::new(conf.clone()),
domain_storage,
reload_manager: Arc::new(reload_manager),
acme_manager,
delay_timer,
host_alias
}
}

Expand Down Expand Up @@ -86,7 +90,7 @@ impl AdminServer {

fn get_domain_info(
&self,
) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path("status")
.and(warp::query::<GetDomainOption>())
.and(with(self.domain_storage.clone()))
Expand All @@ -99,6 +103,7 @@ impl AdminServer {
warp::path!("upload" / "position")
.and(warp::query::<GetDomainPositionOption>())
.and(with(self.domain_storage.clone()))
.and(with(self.host_alias.clone()))
.map(service::get_upload_position)
}

Expand Down Expand Up @@ -140,15 +145,28 @@ impl AdminServer {
warp::body::content_length_limit(1024 * 16)
.and(warp::body::json::<UpdateUploadingStatusOption>()),
)
.and(with(self.host_alias.clone()))
.and_then(service::change_upload_status)
}

fn upload_file(&self) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
fn check_alias(domain:&str, host_alias: Arc<HashMap<String, String>>) -> Option<Response> {
let (host,_) = get_host_path_from_domain(domain);
if let Some(original_host) = host_alias.get(host) {
return Some(bad_resp(format!("should not use alias domain, please use {original_host}")))
}
None
}

fn upload_file(&self) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
async fn handler(
query: UploadFileOption,
form: FormData,
storage: Arc<DomainStorage>,
host_alias: Arc<HashMap<String, String>>,
) -> Result<Response, Infallible> {
if let Some(resp) = AdminServer::check_alias(&query.domain, host_alias) {
return Ok(resp)
}
let resp = service::update_file(query, form, storage)
.await
.unwrap_or_else(|e| {
Expand All @@ -163,12 +181,13 @@ impl AdminServer {
.and(warp::query::<UploadFileOption>())
.and(warp::multipart::form().max_length(self.conf.max_upload_size))
.and(with(self.domain_storage.clone()))
.and(with(self.host_alias.clone()))
.and_then(handler)
}

fn get_files_metadata(
&self,
) -> impl Filter<Extract = (impl warp::Reply,), Error = Rejection> + Clone {
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path!("files" / "metadata")
.and(with(self.domain_storage.clone()))
.and(warp::query::<DomainWithVersionOption>())
Expand Down Expand Up @@ -204,6 +223,7 @@ impl AdminServer {
}

pub mod service {
use std::collections::HashMap;
use crate::acme::ACMEManager;
use crate::admin_server::request::{
DeleteDomainVersionOption, DomainWithOptVersionOption, DomainWithVersionOption,
Expand Down Expand Up @@ -274,7 +294,11 @@ pub mod service {
pub(super) fn get_upload_position(
option: GetDomainPositionOption,
storage: Arc<DomainStorage>,
host_alias: Arc<HashMap<String, String>>,
) -> Response {
if let Some(resp) = super::AdminServer::check_alias(&option.domain, host_alias) {
return resp
}
if URI_REGEX.is_match(&option.domain) {
match storage.get_upload_position(&option.domain) {
Ok(ret) => {
Expand Down Expand Up @@ -317,7 +341,11 @@ pub mod service {
storage: Arc<DomainStorage>,
acme_manager: Arc<ACMEManager>,
param: UpdateUploadingStatusOption,
host_alias: Arc<HashMap<String,String>>,
) -> Result<Response, Infallible> {
if let Some(resp) = super::AdminServer::check_alias(&param.domain, host_alias) {
return Ok(resp)
}
let resp = match storage
.update_uploading_status(param.domain, param.version, param.status, &acme_manager)
.await
Expand Down Expand Up @@ -473,6 +501,12 @@ pub mod service {
}
}

fn bad_resp(text:String) -> Response {
let mut resp = StatusCode::BAD_REQUEST.into_response();
*resp.body_mut() = Body::from(text);
resp
}

pub mod request {
use crate::domain_storage::UploadingStatus;
use serde::{Deserialize, Serialize};
Expand Down
5 changes: 3 additions & 2 deletions server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ pub struct DomainConfig {
pub cache: Option<DomainCacheConfig>,
pub https: Option<DomainHttpsConfig>,
pub alias: Option<Vec<String>>,
pub redirect_https: Option<bool>,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct DomainHttpsConfig {
pub ssl: Option<SSL>,
pub http_redirect_to_https: Option<u32>,
#[serde(default)]
pub disable_acme: bool,
}
Expand Down Expand Up @@ -162,6 +162,7 @@ pub struct HttpConfig {
pub addr: String,
pub port: u16,
pub external_port: Option<u16>,
pub redirect_https: Option<bool>,
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
Expand All @@ -172,7 +173,7 @@ pub struct HttpsConfig {
pub external_port: Option<u16>,
pub addr: String,
#[serde(default)]
pub http_redirect_to_https: u32,
pub http_redirect_to_https: u16,
}
// should write Deserialize by hand.
#[derive(Deserialize, Debug, Clone, PartialEq)]
Expand Down
23 changes: 15 additions & 8 deletions server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod cors;
pub mod service;
pub mod static_file_filter;

use std::collections::HashMap;
use crate::acme::{ACMEManager, RefreshDomainMessage, ReloadACMEState};
use crate::admin_server::AdminServer;
use crate::config::{AdminConfig, Config};
Expand Down Expand Up @@ -47,13 +48,15 @@ async fn run_admin_server(
reload_manager: HotReloadManager,
acme_manager: Arc<ACMEManager>,
delay_timer: DelayTimer,
host_alias: Arc<HashMap<String, String>>
) -> anyhow::Result<()> {
let admin_server = AdminServer::new(
config,
storage.clone(),
reload_manager,
acme_manager,
delay_timer,
host_alias,
);
admin_server.run().await
}
Expand All @@ -71,7 +74,7 @@ pub async fn reload_server(
let domain_storage = Arc::new(DomainStorage::init(&config.file_dir, cache)?);

let (state, http_rx, https_rx) = OneShotReloadState::init(&config);
let server = Server::new(config.clone(), domain_storage.clone());
let server = Server::new(config.clone(), domain_storage.clone())?;
let acme_config = config.https.as_ref().and_then(|x| x.acme.clone());
let reload_acme_state: Option<ReloadACMEState> = if let Some(acme_config) = acme_config {
Some(ACMEManager::init_acme_provider_and_certificate(
Expand All @@ -96,10 +99,10 @@ pub async fn reload_server(
tokio::task::spawn(async move {
join(
server
.init_http_server(http_rx, challenge_path.clone())
.init_http_server(http_rx, challenge_path)
.map_err(|error| error!("reload http server error:{error}")),
server
.init_https_server(https_rx, tls_server_config, challenge_path.clone())
.init_https_server(https_rx, tls_server_config)
.map_err(|error| error!("reload https server error:{error}")),
)
.await
Expand All @@ -125,7 +128,7 @@ pub async fn run_server() -> anyhow::Result<()> {
pub async fn run_server_with_config(config: Config) -> anyhow::Result<()> {
let cache = FileCache::new(&config);
let domain_storage = Arc::new(DomainStorage::init(&config.file_dir, cache)?);
let server = Server::new(config.clone(), domain_storage.clone());
let server = Server::new(config.clone(), domain_storage.clone())?;

if let Some(admin_config) = &config.admin_config {
tracing::info!("admin server enabled");
Expand Down Expand Up @@ -154,16 +157,19 @@ pub async fn run_server_with_config(config: Config) -> anyhow::Result<()> {
)?);
let challenge_path = acme_manager.challenge_dir.clone();

let tls_server_config = load_ssl_server_config(&config, acme_manager.clone(), server.get_host_alias())?;
let host_alias = server.get_host_alias();


let tls_server_config = load_ssl_server_config(&config, acme_manager.clone(), host_alias.clone())?;
let _ = tokio::join!(
server
.init_https_server(https_rx, tls_server_config, challenge_path.clone())
.init_https_server(https_rx, tls_server_config)
.map_err(|error| {
error!("init https server error: {error}");
error
}),
server
.init_http_server(http_rx, challenge_path.clone())
.init_http_server(http_rx, challenge_path)
.map_err(|error| {
error!("init http server error: {error}");
error
Expand All @@ -174,6 +180,7 @@ pub async fn run_server_with_config(config: Config) -> anyhow::Result<()> {
reload_manager,
acme_manager.clone(),
delay_timer,
host_alias,
)
.map_err(|error| {
error!("init admin server error: {error}");
Expand All @@ -197,7 +204,7 @@ pub async fn run_server_with_config(config: Config) -> anyhow::Result<()> {
let tls_server_config = load_ssl_server_config(&config, acme_manager.clone(), server.get_host_alias())?;
let _ = tokio::join!(
server
.init_https_server(None, tls_server_config, challenge_path.clone())
.init_https_server(None, tls_server_config)
.map_err(|error| {
error!("init https server error: {error}");
panic!("init https server error: {error}")
Expand Down
Loading

0 comments on commit 2f28ed7

Please sign in to comment.