初次提交
This commit is contained in:
59
src/main.rs
Normal file
59
src/main.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{Router, extract::State, response::Html, routing::get};
|
||||
|
||||
use num_bigint::{self, ToBigUint};
|
||||
use sled::Db;
|
||||
use tower_http::services::fs::ServeFile;
|
||||
|
||||
const INDEX: &str = include_str!("../index.html");
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let db = sled::open("database").unwrap();
|
||||
match db.contains_key("c") {
|
||||
Ok(r) => {
|
||||
if r == false {
|
||||
let _ = db.insert(b"c", 0.to_biguint().unwrap().to_bytes_be());
|
||||
println!("初始次数:0")
|
||||
} else {
|
||||
println!(
|
||||
"初始次数:{}",
|
||||
num_bigint::BigUint::from_bytes_be(&db.get(b"c").unwrap().unwrap().to_vec())
|
||||
.to_string()
|
||||
)
|
||||
};
|
||||
}
|
||||
Err(_) => eprintln!("无法检查键值对"),
|
||||
};
|
||||
let arc_db = Arc::new(db);
|
||||
let app = Router::new()
|
||||
.route("/", get(index))
|
||||
.route("/c", get(count))
|
||||
.route("/a", get(add))
|
||||
.route_service("/img", ServeFile::new("./icon.jpg"))
|
||||
.with_state(arc_db);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", 1145))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
println!("127.0.0.1:1145");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn index() -> Html<&'static str> {
|
||||
Html(INDEX)
|
||||
}
|
||||
|
||||
async fn count(State(db): State<Arc<Db>>) -> String {
|
||||
num_bigint::BigUint::from_bytes_be(&db.get(b"c").unwrap().unwrap().to_vec()).to_string()
|
||||
}
|
||||
|
||||
async fn add(State(db): State<Arc<Db>>) -> String {
|
||||
let o = num_bigint::BigUint::from_bytes_be(&db.get(b"c").unwrap().unwrap().to_vec());
|
||||
let n = &o + 1.to_biguint().unwrap();
|
||||
|
||||
let _ = db.insert(b"c", n.to_bytes_be());
|
||||
n.to_string()
|
||||
}
|
||||
Reference in New Issue
Block a user