added a CLI and css styling
This commit is contained in:
Generated
+127
@@ -17,6 +17,56 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.100"
|
||||
@@ -203,6 +253,52 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.5.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.5.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.49"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation-sys"
|
||||
version = "0.8.7"
|
||||
@@ -261,6 +357,7 @@ dependencies = [
|
||||
"askama",
|
||||
"axum",
|
||||
"chrono",
|
||||
"clap",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
@@ -410,6 +507,12 @@ dependencies = [
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "1.4.0"
|
||||
@@ -546,6 +649,12 @@ dependencies = [
|
||||
"hashbrown 0.16.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.15"
|
||||
@@ -654,6 +763,12 @@ version = "1.21.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.5"
|
||||
@@ -961,6 +1076,12 @@ dependencies = [
|
||||
"lock_api",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.111"
|
||||
@@ -1279,6 +1400,12 @@ version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.1"
|
||||
|
||||
@@ -16,3 +16,4 @@ anyhow = "1.0.100"
|
||||
toml = "0.9.8"
|
||||
chrono = { version = "0.4.42", features = ["serde"] }
|
||||
askama = "0.14.0"
|
||||
clap = { version = "4.5.53", features = ["derive"] }
|
||||
|
||||
+25
-4
@@ -15,6 +15,24 @@ use tokio::sync::Mutex;
|
||||
use tower_governor::{GovernorLayer, governor::GovernorConfigBuilder};
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
pub struct Cli {
|
||||
/// Config path
|
||||
#[arg(short, long, default_value = "config.toml")]
|
||||
config_path: String,
|
||||
|
||||
/// Output file
|
||||
#[arg(short, long, default_value = "answers.json")]
|
||||
output_file: String,
|
||||
|
||||
/// Verbose mode
|
||||
#[arg(short, long)]
|
||||
verbose: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct FieldDef {
|
||||
name: String,
|
||||
@@ -46,13 +64,16 @@ struct AppState {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let subscriber = tracing_subscriber::FmtSubscriber::new();
|
||||
tracing::subscriber::set_global_default(subscriber).ok();
|
||||
|
||||
// load config
|
||||
let cfg = load_config("config.toml").context("loading config.toml")?;
|
||||
let cfg = load_config(&cli.config_path).context(format!("loading {}", cli.config_path))?;
|
||||
tracing::info!(
|
||||
"Loaded config: json_output='{}' with {} fields",
|
||||
"Loaded config: '{}', writing answers to '{}' with {} fields",
|
||||
cli.config_path,
|
||||
cfg.json_output,
|
||||
cfg.fields.len()
|
||||
);
|
||||
@@ -93,7 +114,7 @@ async fn main() -> anyhow::Result<()> {
|
||||
.layer(cors)
|
||||
.layer(GovernorLayer::new(governor_conf));
|
||||
|
||||
let port = std::env::var("SERVER_PORT").unwrap_or_else(|_| "8081".to_string());
|
||||
let port = std::env::var("SERVER_PORT").unwrap_or("8081".to_string());
|
||||
let addr = format!("127.0.0.1:{port}");
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
||||
@@ -142,7 +163,7 @@ async fn submit(
|
||||
let path = &state.cfg.json_output;
|
||||
|
||||
let mut existing: Vec<ResponseEntry> = match std::fs::read_to_string(path) {
|
||||
Ok(raw) => serde_json::from_str(&raw).unwrap_or_else(|_| Vec::new()),
|
||||
Ok(raw) => serde_json::from_str(&raw).unwrap_or(Vec::new()),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Vec::new(),
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to read {}: {}", path, e);
|
||||
|
||||
+87
-28
@@ -1,51 +1,110 @@
|
||||
body {
|
||||
font-family: system-ui, -apple-system, Roboto, "Segoe UI", Arial;
|
||||
padding: 2rem;
|
||||
max-width: 800px;
|
||||
margin: auto;
|
||||
html {
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
body {
|
||||
font-family: 'Inter', system-ui, -apple-system, Roboto, "Segoe UI", Arial, sans-serif;
|
||||
padding: 20px;
|
||||
margin: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background-color: #fff;
|
||||
overflow-x: hidden;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.8rem;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.form-container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
text-align: left;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
background: #fff;
|
||||
border-radius: 16px;
|
||||
padding: 2rem;
|
||||
max-width: 720px;
|
||||
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
|
||||
}
|
||||
|
||||
.form-container img {
|
||||
display: block;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.desc {
|
||||
font-size: 1.2rem;
|
||||
margin-bottom: 0.5rem;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
label {
|
||||
font-weight: 600;
|
||||
display: block;
|
||||
margin-bottom: 0.2rem;
|
||||
}
|
||||
|
||||
.field {
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.desc {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
margin-bottom: 0.4rem;
|
||||
margin-bottom: 0.25rem;
|
||||
font-size: 1.35rem;
|
||||
color: #222;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea,
|
||||
select {
|
||||
width: 100%;
|
||||
padding: 0.4rem;
|
||||
margin-top: 0.2rem;
|
||||
padding: 0.6rem 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
box-sizing: border-box;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
font-size: 1rem;
|
||||
color: #333333aa;
|
||||
background-color: #e0f0ff; /* light blue */
|
||||
border: 1px solid #88c1ff; /* medium blue */
|
||||
transition: all 0.2s ease-in-out;
|
||||
}
|
||||
|
||||
input:focus,
|
||||
textarea:focus,
|
||||
select:focus {
|
||||
outline: #66aaffaa; /* soft blue focus */
|
||||
background-color: #cce5ff; /* slightly darker blue on focus */
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: none;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 0.6rem 1rem;
|
||||
font-size: 1rem;
|
||||
background-color: #007acc;
|
||||
color: white;
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 1.3rem;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
border-radius: 0.25rem;
|
||||
cursor: pointer;
|
||||
background-color: #66aaff; /* medium blue */
|
||||
color: #fff;
|
||||
transition: background-color 0.2s;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #005fa3;
|
||||
background-color: #3390ff; /* darker blue on hover */
|
||||
}
|
||||
|
||||
.submit-container {
|
||||
text-align: center;
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
@media (max-width: 720px) {
|
||||
html {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.form-container {
|
||||
max-width: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
+26
-23
@@ -3,27 +3,30 @@
|
||||
{% block title %}Form{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form action="/submit" method="POST" autocomplete="off">
|
||||
{% for f in fields %}
|
||||
{% if f.html_before.is_some() %}
|
||||
{{ f.html_before.as_ref().unwrap() | safe }}
|
||||
{% endif %}
|
||||
<div class="field">
|
||||
<label>{{ f.title }}</label>
|
||||
<div class="desc">{{ f.description }}</div>
|
||||
{% if f.answer_type == "textarea" %}
|
||||
<textarea id="{{ f.name }}" name="{{ f.name }}" rows="5"></textarea>
|
||||
{% elif f.answer_type == "select" %}
|
||||
<select id="{{ f.name }}" name="{{ f.name }}"><option value="">(no options)</option></select>
|
||||
{% else %}
|
||||
<input id="{{ f.name }}" name="{{ f.name }}" type="{{ f.answer_type }}">
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if f.html_after.is_some() %}
|
||||
{{ f.html_after.as_ref().unwrap() | safe }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
<div><button type="submit">{{ submit_button }}</button></div>
|
||||
</form>
|
||||
<div class="form-container">
|
||||
<form action="/submit" method="POST" autocomplete="off">
|
||||
{% for f in fields %}
|
||||
{% if f.html_before.is_some() %}
|
||||
{{ f.html_before.as_ref().unwrap() | safe }}
|
||||
{% endif %}
|
||||
<div class="field">
|
||||
<label for="{{ f.name }}">{{ f.title }}</label>
|
||||
<div class="desc">{{ f.description }}</div>
|
||||
{% if f.answer_type == "textarea" %}
|
||||
<textarea id="{{ f.name }}" name="{{ f.name }}" rows="10" placeholder="{{ f.title|lower }}"></textarea>
|
||||
{% elif f.answer_type == "select" %}
|
||||
<select id="{{ f.name }}" name="{{ f.name }}">
|
||||
<option value="">Select an option</option>
|
||||
</select>
|
||||
{% else %}
|
||||
<input id="{{ f.name }}" name="{{ f.name }}" type="{{ f.answer_type }}" placeholder="{{ f.title|lower }}">
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if f.html_after.is_some() %}
|
||||
{{ f.html_after.as_ref().unwrap() | safe }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
<div class="submit-container"><button type="submit">{{ submit_button }}</button></div>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user