diff --git a/flake.nix b/flake.nix index 58ec43f..4bc123d 100644 --- a/flake.nix +++ b/flake.nix @@ -1,114 +1,114 @@ { - description = "Quick rust website template"; + description = "Quick rust website template"; - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; - }; - - outputs = - { self, nixpkgs, ... }: - let - supportedSystems = [ - "x86_64-linux" - # "aarch64-linux" - # "x86_64-darwin" - # "aarch64-darwin" - ]; - forAllSystems = nixpkgs.lib.genAttrs supportedSystems; - in - { - packages = forAllSystems ( - system: - let - pkgs = import nixpkgs { inherit system; }; - in - { - default = pkgs.rustPlatform.buildRustPackage { - pname = "quick-rust-website"; - version = "0.1.0"; - - src = ./.; - - cargoLock.lockFile = ./Cargo.lock; - - nativeBuildInputs = [ pkgs.pkg-config ]; - - buildInputs = [ - pkgs.openssl - # pkgs.sqlite - ]; - - PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; - OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib"; - OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include"; - }; - } - ); - - devShells = forAllSystems ( - system: - let - pkgs = import nixpkgs { inherit system; }; - in - { - default = pkgs.mkShell { - packages = [ - pkgs.rustc - pkgs.cargo - pkgs.rust-analyzer - pkgs.pkg-config - pkgs.openssl - ]; - - env = { - PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; - OPENSSL_DIR = "${pkgs.openssl.dev}"; - }; - }; - } - ); - - nixosModules.default = - { - config, - lib, - pkgs, - ... - }: - let - cfg = config.services.slimes-server; - in - { - options.services.slimes-server = { - enable = lib.mkEnableOption "Quick rust website template"; - port = lib.mkOption { - type = lib.types.port; - default = 9003; - }; - # databasePath = lib.mkOption { - # type = lib.types.str; - # default = "/var/lib/quick-rust-website/database.db"; - # }; - }; - - config = lib.mkIf cfg.enable { - systemd.services.slimes-server = { - description = "Quick rust website template"; - after = [ "network.target" ]; - wantedBy = [ "multi-user.target" ]; - serviceConfig = { - ExecStart = "${ - self.packages.${pkgs.system}.default - }/bin/quick-rust-website --database-url ${cfg.databasePath} --port ${toString cfg.port}"; - Restart = "on-failure"; - StateDirectory = "quick-rust-website"; - DynamicUser = true; - ProtectSystem = "strict"; - ProtectHome = true; - NoNewPrivileges = true; - }; - }; - }; - }; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; }; + + outputs = + { self, nixpkgs, ... }: + let + supportedSystems = [ + "x86_64-linux" + # "aarch64-linux" + # "x86_64-darwin" + # "aarch64-darwin" + ]; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + in + { + packages = forAllSystems ( + system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + default = pkgs.rustPlatform.buildRustPackage { + pname = "quick-rust-website"; + version = "0.1.0"; + + src = ./.; + + cargoLock.lockFile = ./Cargo.lock; + + nativeBuildInputs = [ pkgs.pkg-config ]; + + buildInputs = [ + pkgs.openssl + # pkgs.sqlite + ]; + + PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; + OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib"; + OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include"; + }; + } + ); + + devShells = forAllSystems ( + system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + default = pkgs.mkShell { + packages = [ + pkgs.rustc + pkgs.cargo + pkgs.rust-analyzer + pkgs.pkg-config + pkgs.openssl + ]; + + env = { + PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; + OPENSSL_DIR = "${pkgs.openssl.dev}"; + }; + }; + } + ); + + nixosModules.default = + { + config, + lib, + pkgs, + ... + }: + let + cfg = config.services.quick-rust-website; + in + { + options.services.quick-rust-website = { + enable = lib.mkEnableOption "Quick rust website template"; + port = lib.mkOption { + type = lib.types.port; + default = 9003; + }; + # databasePath = lib.mkOption { + # type = lib.types.str; + # default = "/var/lib/quick-rust-website/database.db"; + # }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.quick-rust-website = { + description = "Quick rust website template"; + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + ExecStart = "${ + self.packages.${pkgs.system}.default + }/bin/quick-rust-website --port ${toString cfg.port} --host"; + Restart = "on-failure"; + StateDirectory = "quick-rust-website"; + DynamicUser = true; + ProtectSystem = "strict"; + ProtectHome = true; + NoNewPrivileges = true; + }; + }; + }; + }; + }; } diff --git a/src/main.rs b/src/main.rs index 981fd38..313067c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use axum::{Router, routing::get}; +use clap::Parser; use lazy_static::lazy_static; use std::sync::Arc; use tera::Tera; @@ -20,12 +21,30 @@ lazy_static! { }; } +#[derive(clap::Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + /// Port to serve on + #[arg(short, long, default_value = "8000")] + port: String, + + /// Whether to listen on 0.0.0.0 + #[arg(short, long)] + host: bool, + + /// Verbose mode + #[arg(short, long)] + verbose: bool, +} + struct AppState { app_name: String, } #[tokio::main] async fn main() -> anyhow::Result<()> { + let cli = Cli::parse(); + tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .init(); @@ -39,12 +58,10 @@ async fn main() -> anyhow::Result<()> { .route("/showcase", get(showcase_handler)) .with_state(shared_state); - let host = false; - let port = "8000"; - let addr = if host { - format!("0.0.0.0:{}", port) + let addr = if cli.host { + format!("0.0.0.0:{}", cli.port) } else { - format!("127.0.0.1:{}", port) + format!("127.0.0.1:{}", cli.port) }; let listener = tokio::net::TcpListener::bind(&addr).await?;