[web] Add the ability to parse arguments

(don't actually do anything with them yet)
This commit is contained in:
Emi Simpson 2021-10-27 20:00:45 -04:00
parent c64437a915
commit 498cbc4fce
Signed by: Emi
GPG Key ID: A12F2C2FFDC3D847
3 changed files with 85 additions and 0 deletions

View File

@ -11,6 +11,7 @@ actix-web = "3"
log = "0.4"
env_logger = "0.9"
askama = "0.10"
argh = "0.1.6"
rusttype = { version = "0.9.2", optional = true }
image = { version = "0.23.14", optional = true }
lazy_static = { version = "1.4.0", optional = true }
@ -29,4 +30,8 @@ ogp_images = ["rusttype", "image", "lazy_static"]
# flag, and will remove the ability to serve any static assets.
embed_static_assets = []
# Adjust some of the default values for commandline and config values to be sane defaults
# for use within a Docker container
docker = []
default = ["ogp_images", "embed_static_assets"]

63
web/src/configuration.rs Normal file
View File

@ -0,0 +1,63 @@
use argh::FromArgs;
#[derive(FromArgs, PartialEq, Debug)]
/// Everything you need to selfhost a pronouns.today server
pub struct PronounsTodayArgs {
#[argh(subcommand)]
pub command: SubCommand
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum SubCommand {
Run(Run),
#[cfg(feature = "embed_static_assets")]
DumpStatics(DumpStatics),
}
#[derive(FromArgs, PartialEq, Debug)]
#[cfg(feature = "embed_static_assets")]
#[argh(subcommand, name = "dump-statics")]
/// Dump necessary static files to a directory to be served by a webserver. Serving
/// static files with a dedicated webserver isn't necessary, but may improve performance.
/// If serving static files, all files should be available under the /static/filename.ext
/// route.
pub struct DumpStatics {
#[argh(option, default = "String::from(\"./static/\")")]
/// the directory to write the static files to
///
/// Defaults to ./static/
pub destination: String,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "run")]
/// Run the server from the specified config file. Commandline options override config
/// options.
pub struct Run {
#[cfg_attr(feature = "docker",
argh(
option,
default = "String::from(\"/data/config.yml\")",
short = 'c'))]
#[cfg_attr(not(feature = "docker"),
argh(
option,
default = "String::from(\"/etc/pronouns_today.yml\")",
short = 'c'))]
/// path to the config file (generated if needed)
pub config: String,
#[argh(switch)]
/// don't generate a config file if not present
pub no_generate_cfg: bool,
#[argh(option, short = 'p')]
/// the port to listen on
pub port: Option<u16>,
#[argh(option)]
/// default pronoun probabilites (formatted as a prefstring, like the ones in the
/// used in the custom URLs, e.g. "acaqeawdym")
pub default_prefstr: Option<String>,
}

View File

@ -1,6 +1,7 @@
pub mod ogp_images;
pub mod contrast;
pub mod statics;
pub mod configuration;
use std::collections::HashMap;
use std::fmt::{self, Display};
@ -11,6 +12,7 @@ use actix_web::middleware::normalize::TrailingSlash;
use actix_web::middleware::{self, Logger};
use actix_web::web::resource;
use actix_web::{App, HttpRequest, HttpResponse, HttpServer, Responder, ResponseError, Result, post, web};
use argh;
use askama::Template;
use pronouns_today::user_preferences::Preference;
use pronouns_today::{InstanceSettings, Pronoun};
@ -139,6 +141,21 @@ async fn not_found() -> impl Responder {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args: configuration::PronounsTodayArgs = argh::from_env();
match args.command {
configuration::SubCommand::DumpStatics(_subargs) => {
println!("Support for dumping statics not yet implemented");
Ok(())
}
configuration::SubCommand::Run(_subargs) => {
start_server().await
}
}
}
async fn start_server() -> std::io::Result<()> {
env_logger::init();
println!("Starting pronouns-today-web on 127.0.0.1:8080");
HttpServer::new(|| {