From 217a273f0b61710b25659bd115e9b709af8b500d Mon Sep 17 00:00:00 2001 From: catvayor Date: Thu, 13 Jun 2024 12:54:26 +0200 Subject: [PATCH] =?UTF-8?q?nginx=20s=C3=A9partion=20for=20static=20serving?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + nix/vm.nix | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 2 +- src/main.rs | 4 +--- 4 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 nix/vm.nix diff --git a/.gitignore b/.gitignore index 3ea2906..0d88ed5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .envrc .direnv +/nixos.qcow2 # Added by cargo diff --git a/nix/vm.nix b/nix/vm.nix new file mode 100644 index 0000000..30237d5 --- /dev/null +++ b/nix/vm.nix @@ -0,0 +1,62 @@ +{ pkgs, lib, ... }: +{ + nix = { + nixPath = [ + "nixpkgs=${builtins.storePath pkgs.path}" + "nixos=${builtins.storePath pkgs.path}" + ]; + package = pkgs.lix; + }; + environment.systemPackages = with pkgs; [ wget tmux ]; + services.nginx = { + enable = true; + virtualHosts."localhost" = { + default = true; + locations = { + "/" = { + root = "/traque/static"; + tryFiles = "$uri @backend"; + }; + "@backend" = { + recommendedProxySettings = true; + proxyPass = "http://localhost:8000"; + extraConfig = '' + proxy_set_header Connection '''; + proxy_http_version 1.1; + chunked_transfer_encoding off; + proxy_buffering off; + proxy_cache off; + ''; + }; + }; + #extraConfig = '' + # error_page 502 =503; + #''; + }; + }; + services.getty = { + autologinUser = "root"; + helpLine = lib.mkForce '' + On serial console: type Ctrl-a c to switch to the qemu console and `quit` to stop the VM. + traque source is on /traque (rw), `cd /traque && ./target/debug/traque` to run the test. + Compile from the host for performance (the VM is highly limited).''; + }; + nixos-shell.mounts = { + mountHome = false; + mountNixProfile = false; + cache = "none"; + extraMounts = { + "/traque" = { + target = toString ../.; + cache = "none"; + }; + }; + }; + virtualisation = { + forwardPorts = [ + { from = "host"; host.port = 8000; guest.port = 80; } + ]; + }; + + system.stateVersion = "24.11"; +} diff --git a/shell.nix b/shell.nix index eaa9a72..b21d197 100644 --- a/shell.nix +++ b/shell.nix @@ -1,4 +1,4 @@ { pkgs ? (import ) { }, lib ? pkgs.lib}: pkgs.mkShell { - buildInputs = with pkgs; [ cargo rustc rustfmt ]; + buildInputs = with pkgs; [ cargo rustc rustfmt nixos-shell ]; } diff --git a/src/main.rs b/src/main.rs index 64d1ea1..65a28fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ #[macro_use] extern crate rocket; use rocket::{ - fs::{relative, FileServer}, response::stream::Event, tokio::{ self, select, @@ -102,8 +101,7 @@ async fn rocket() -> _ { .manage(key) .mount("/", routes![index]) .mount("/track", track::routes()) - .mount("/admin", admin::routes()) - .mount("/", FileServer::from(relative!("static"))); + .mount("/admin", admin::routes()); tokio::spawn(async move { let mut clean_interval = time::interval(5 * EVENT_TIMEOUT); let mut coord_interval = time::interval(Duration::from_millis(3000));