From 1b9529223a95b5b8f3d041ab9d352fd179a946d5 Mon Sep 17 00:00:00 2001 From: Oskar Date: Mon, 26 Sep 2022 19:41:37 +0000 Subject: [PATCH] chore: configure github codespace --- .devcontainer/Dockerfile | 16 ++++++++++ .devcontainer/devcontainer.json | 52 +++++++++++++++++++++++++++++++++ .gitignore | 6 ++-- README.md | 2 +- config.toml | 2 +- src/server/mod.rs | 10 +++++-- 6 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..d6592d8 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,16 @@ +# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/rust/.devcontainer/base.Dockerfile + +# [Choice] Debian OS version (use bullseye on local arm64/Apple Silicon): buster, bullseye +ARG VARIANT="buster" +FROM mcr.microsoft.com/vscode/devcontainers/rust:0-${VARIANT} + +USER vscode +RUN rustup install nightly \ + && rustup component add rustfmt --toolchain nightly \ + && rustup component add clippy --toolchain nightly + +USER root + +# [Optional] Uncomment this section to install additional packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..6ade40b --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,52 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/rust +{ + "name": "Rust", + "build": { + "dockerfile": "Dockerfile", + "args": { + // Use the VARIANT arg to pick a Debian OS version: buster, bullseye + // Use bullseye when on local on arm64/Apple Silicon. + "VARIANT": "bullseye" + } + }, + "runArgs": [ + "--cap-add=SYS_PTRACE", + "--security-opt", + "seccomp=unconfined" + ], + + // Configure tool-specific properties. + "customizations": { + // Configure properties specific to VS Code. + "vscode": { + // Set *default* container specific settings.json values on container create. + "settings": { + "lldb.executable": "/usr/bin/lldb", + // VS Code don't watch files under ./target + "files.watcherExclude": { + "**/target/**": true + }, + "rust-analyzer.checkOnSave.command": "clippy" + }, + + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "vadimcn.vscode-lldb", + "mutantdino.resourcemonitor", + "rust-lang.rust-analyzer", + "tamasfe.even-better-toml", + "serayuzgur.crates" + ] + } + }, + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "rustc --version", + + // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "vscode" +} diff --git a/.gitignore b/.gitignore index 4bef88c..0f87aa5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -# Generated by Cargo -# will have compiled files and executables +# Rust output /target # Logs files @@ -17,3 +16,6 @@ cert.pem # IDE configs .idea .vscode + +# Development storage directory +/storage diff --git a/README.md b/README.md index ac7d448..305b045 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Now you can run server using command `./target/release/homedisk`. ```bash # Generate private key -openssl genrsa -out cert.key 204 +openssl genrsa -out cert.key 2048 # Generate certificate openssl req -new -x509 -key cert.key -out cert.pem -days 365 ``` diff --git a/config.toml b/config.toml index 7f1598f..0898867 100644 --- a/config.toml +++ b/config.toml @@ -11,4 +11,4 @@ secret = "secret key used to sign tokens" # jsonwebtoken secret string used to s expires = 24 # token expiration time in hours (default one day) [storage] -path = "/home/homedisk" # path to directory where user files will be stored +path = "storage" # path to directory where user files will be stored diff --git a/src/server/mod.rs b/src/server/mod.rs index 27385ba..a1ec5b1 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -2,7 +2,7 @@ mod api; pub mod error; pub mod utils; -use std::path::PathBuf; +use std::{path::{PathBuf, Path}, process::exit}; use anyhow::anyhow; use axum::{ @@ -18,13 +18,19 @@ use tower_http::{ cors::{AllowOrigin, CorsLayer}, BoxError, }; -use tracing::{debug, info}; +use tracing::{debug, info, error}; use crate::{config::Config, database::Database}; pub async fn start_server(config: Config, db: Database) -> anyhow::Result<()> { let host = format!("{}:{}", config.http.host, config.http.https_port); + // check if tls cert and key file exists + if !Path::new(&config.http.tls_cert).exists() || !Path::new(&config.http.tls_key).exists() { + error!("TLS cert or/and key file not found!"); + exit(1); + } + tokio::spawn(redirect_http_to_https(config.clone())); info!("🚀 Server has launched on https://{host}");