initial commit
All checks were successful
Docker / build (push) Successful in 7m39s

This commit is contained in:
2025-03-08 14:14:13 +03:00
commit eaea3f906c
9 changed files with 1808 additions and 0 deletions

55
src/main.rs Normal file
View File

@@ -0,0 +1,55 @@
use rocket::fs::NamedFile;
use rocket::response::status;
use rocket::{self, State, get, launch, routes};
use std::env;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
struct HitCount {
count: AtomicUsize,
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/hello/<name>")]
fn hello(name: String) -> String {
format!("Hello, {}!", name)
}
#[get("/env/<key>")]
fn get_env(key: String) -> Result<String, status::NotFound<String>> {
env::var(key).map_err(|e| status::NotFound(e.to_string()))
}
#[get("/files/<file>")]
async fn get_file(file: PathBuf) -> Result<NamedFile, status::NotFound<String>> {
if file.file_name() != Some(file.as_os_str()) {
return Err(status::NotFound(
"File name cannot contain slashes".to_string(),
));
}
let prefix = env::var("FILES_PREFIX").unwrap_or_else(|_| "/files".to_string());
let path = Path::new(&prefix).join(file);
NamedFile::open(&path)
.await
.map_err(|e| status::NotFound(e.to_string()))
}
#[get("/count")]
fn count(hit_count: &State<HitCount>) -> String {
let current_count = hit_count.count.load(Ordering::Relaxed);
hit_count.count.fetch_add(1, Ordering::Relaxed);
format!("Number of visits: {}", current_count)
}
#[launch]
fn rocket() -> _ {
rocket::build()
.manage(HitCount {
count: AtomicUsize::new(0),
})
.mount("/", routes![index, hello, get_env, get_file, count])
}