basic code checking
This commit is contained in:
+70
-2
@@ -1,3 +1,71 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
mod templates;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use axum::{
|
||||
Form, Router,
|
||||
response::{Html, Redirect},
|
||||
routing::{get, post},
|
||||
};
|
||||
|
||||
use crate::templates::{code1_invalid, code1_success, index_page};
|
||||
|
||||
const CODE1: &str = "aaaaaa";
|
||||
const CODE1_KEY: &str = "code_1";
|
||||
const CODE1_SECRET: &str = "super-secret-key";
|
||||
|
||||
static LIVE: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
async fn index() -> Html<String> {
|
||||
let checked = if LIVE.load(Ordering::SeqCst) {
|
||||
"checked"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let status = if LIVE.load(Ordering::SeqCst) {
|
||||
"live"
|
||||
} else {
|
||||
"offline"
|
||||
};
|
||||
|
||||
Html(index_page(status, checked))
|
||||
}
|
||||
|
||||
async fn toggle_live(Form(form): Form<HashMap<String, String>>) -> Redirect {
|
||||
let enabled = form.contains_key("live");
|
||||
LIVE.store(enabled, Ordering::SeqCst);
|
||||
println!("Live state set to: {}", enabled);
|
||||
Redirect::to("/")
|
||||
}
|
||||
|
||||
async fn code1_post(Form(form): Form<HashMap<String, String>>) -> Html<String> {
|
||||
let provided_key = form
|
||||
.get(CODE1_KEY)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| String::from(""));
|
||||
|
||||
if provided_key == CODE1_SECRET {
|
||||
println!("CODE1 authenticated successfully");
|
||||
Html(code1_success())
|
||||
} else {
|
||||
println!("CODE1 rejected: invalid secret");
|
||||
Html(code1_invalid(CODE1_KEY))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/toggle-live", post(toggle_live))
|
||||
.route(&format!("/{}", CODE1), post(code1_post));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
|
||||
.await
|
||||
.expect("failed to bind port 3000");
|
||||
|
||||
println!("Server running at http://localhost:3000");
|
||||
println!("CODE1 endpoint available at /{}", CODE1);
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
pub fn index_page(status: &str, checked: &str) -> String {
|
||||
format!(
|
||||
r#"
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Live Toggle</title>
|
||||
<style>
|
||||
body {{
|
||||
font-family: Arial, sans-serif;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
height: 100vh;
|
||||
margin: 0;
|
||||
background: #111827;
|
||||
color: #f9fafb;
|
||||
}}
|
||||
.card {{
|
||||
background: #1f2937;
|
||||
padding: 2rem 2.5rem;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
|
||||
}}
|
||||
label {{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
font-size: 1.1rem;
|
||||
}}
|
||||
p {{
|
||||
margin-top: 1rem;
|
||||
font-size: 1rem;
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<form method="post" action="/toggle-live">
|
||||
<label>
|
||||
<input type="checkbox" name="live" value="true" {checked} onchange="this.form.submit()">
|
||||
<span>live</span>
|
||||
</label>
|
||||
</form>
|
||||
<p>Status: <strong>{status}</strong></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
||||
pub fn code1_success() -> String {
|
||||
String::from(
|
||||
r#"
|
||||
<html>
|
||||
<body>
|
||||
<h1>CODE1 handled</h1>
|
||||
<p>Authenticated with secret key.</p>
|
||||
<p>Action: none</p>
|
||||
<a href="/">Back to home</a>
|
||||
</body>
|
||||
</html>
|
||||
"#,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn code1_invalid(key: &str) -> String {
|
||||
format!(
|
||||
r#"
|
||||
<html>
|
||||
<body>
|
||||
<h1>CODE1 rejected</h1>
|
||||
<p>Invalid {key}.</p>
|
||||
<p>Action: none</p>
|
||||
<a href="/">Back to home</a>
|
||||
</body>
|
||||
</html>
|
||||
"#
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user