Skip to content

Commit

Permalink
add https
Browse files Browse the repository at this point in the history
  • Loading branch information
sstepanchuk committed Feb 1, 2025
1 parent 6c05a40 commit 926ba2e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ jobs:
result=$( docker ps -a -q -f name=${{ env.CONTAINER_NAME }} )
echo "OLD_CONTAINER_ID=${result}" >> $GITHUB_ENV
#- name: Pass secrets and vars to ENV
# if: github.ref_name == 'master'
# uses: ./.github/actions/merge-json-to-env
# id: merge
# with:
- name: Pass secrets and vars to ENV
if: github.ref_name == 'master'
uses: ./.github/actions/merge-json-to-env
id: merge
with:
jsons: |
${{ toJSON(secrets) }}
# ${{ toJSON(secrets) }}
# jsons: |
# ${{ toJSON(vars) }}
# ${{ toJSON(secrets) }}
Expand Down Expand Up @@ -87,12 +90,7 @@ jobs:
- name: Run new container
run: |
if [ "${{ github.ref_name }}" == "main" ]; then
PORT=80
else
PORT=8080
fi
result=$( docker run -p ${PORT}:8080 --add-host=host.docker.internal:host-gateway -v /dev/shm:/dev/shm --env-file ${{ env.ENV_FILE }} -i --restart unless-stopped --name ${{ needs.state.outputs.CONTAINER_NAME }}-new -d ${{ needs.push_to_registry.outputs.NEW_IMAGE_ID }} )
result=$( docker run -p 80:3000 -p 443:3443 --add-host=host.docker.internal:host-gateway -v /dev/shm:/dev/shm --env-file ${{ env.ENV_FILE }} -i --restart unless-stopped --name ${{ needs.state.outputs.CONTAINER_NAME }}-new -d ${{ needs.push_to_registry.outputs.NEW_IMAGE_ID }} )
echo "NEW_CONTAINER_ID=${result}" >> $GITHUB_ENV
outputs:
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ COPY --from=builder /app/Cargo.toml /app/

# Set any required env variables and
ENV RUST_LOG="info"
ENV LEPTOS_SITE_ADDR="0.0.0.0:8080"
ENV LEPTOS_SITE_ADDR="0.0.0.0:3000"
ENV LEPTOS_SITE_ROOT="site"
EXPOSE 8080
EXPOSE 3000
EXPOSE 3443

# -- NB: update binary name from "server" to match your app name in Cargo.toml --
# Run the server
Expand Down
24 changes: 24 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod shared;

use app::*;
use axum::extract::Host;
use axum::http::HeaderValue;
use axum::response::Redirect;
use axum::routing::get;
use axum::Router;
use axum_server::tls_rustls::RustlsConfig;
use base64::{engine::general_purpose::STANDARD, Engine as _};
Expand All @@ -11,6 +14,7 @@ use leptos_image::*;
use shared::{file_and_error_handler, AppState};
use tracing::level_filters::LevelFilter;
use std::env;
use std::net::SocketAddr;
use std::time::Duration;
use tower::ServiceBuilder;
use tower_http::compression::{CompressionLayer, CompressionLevel};
Expand Down Expand Up @@ -93,6 +97,8 @@ async fn main() {

// If we have TLS config, run HTTPS and HTTP servers
if let Some(tls_config) = tls_config {
tokio::spawn(start_http_redirect_server(addr.clone()));

addr.set_port(addr.port() + 443);
info!("Starting HTTPS server on port {}", addr);

Expand Down Expand Up @@ -163,3 +169,21 @@ async fn get_tls_config() -> Result<Option<RustlsConfig>> {
}
}
}

pub async fn start_http_redirect_server(addr: SocketAddr) {
let app = Router::new().fallback(
get(|Host(hostname): Host| async move {
let redirect_url = format!("https://{}/", hostname);
Redirect::permanent(&redirect_url)
})
);

info!("Starting HTTP redirect server on {}", addr);

if let Err(e) = axum_server::bind(addr)
.serve(app.into_make_service())
.await
{
warn!("HTTP redirect server failed: {}", e);
}
}

0 comments on commit 926ba2e

Please sign in to comment.