|
@@ -1,31 +1,89 @@
|
|
|
-use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
|
|
+mod config;
|
|
|
+mod vcs;
|
|
|
+
|
|
|
+extern crate chrono;
|
|
|
+
|
|
|
use actix_web::middleware::Logger;
|
|
|
-use log;
|
|
|
+use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
|
|
|
+use fern;
|
|
|
+use log::error;
|
|
|
+use std::{env, io};
|
|
|
+
|
|
|
+use config::Configuration;
|
|
|
|
|
|
-#[get("/")]
|
|
|
-async fn hello() -> impl Responder {
|
|
|
- HttpResponse::Ok().body("Hello world!")
|
|
|
+fn get_directory_listing(path: &str) -> Result<Vec<String>, io::Error>
|
|
|
+{
|
|
|
+ match std::fs::read_dir(path) {
|
|
|
+ Ok(files) => {
|
|
|
+ Ok(files
|
|
|
+ .into_iter()
|
|
|
+ .filter(|dir| dir.as_ref().unwrap().path().starts_with("./."))
|
|
|
+ .map(|dir| dir.as_ref().unwrap().path().to_str().unwrap().to_owned())
|
|
|
+ .collect::<Vec<String>>())
|
|
|
+ },
|
|
|
+ Err(error) => {
|
|
|
+ Err(error)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-#[post("/echo")]
|
|
|
-async fn echo(req_body: String) -> impl Responder {
|
|
|
- HttpResponse::Ok().body(req_body)
|
|
|
+#[get("")]
|
|
|
+async fn show_articles_list() -> impl Responder {
|
|
|
+ const CONTENT_FOLDER: &str = "./content";
|
|
|
+ let estimated_path: String = env::current_dir()
|
|
|
+ .unwrap_or_default()
|
|
|
+ .to_str()
|
|
|
+ .unwrap_or_default()
|
|
|
+ .to_string();
|
|
|
+
|
|
|
+ log::trace!("Estimated content repository: {estimated_path}/{CONTENT_FOLDER}");
|
|
|
+
|
|
|
+ let paths = match get_directory_listing(&CONTENT_FOLDER) {
|
|
|
+ Ok(list) => list.join("\n"),
|
|
|
+ Err(error) => {
|
|
|
+ error!("Couldn't list articles from path. Error: {error}");
|
|
|
+ String::new()
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ HttpResponse::Ok().body(format!("List of articles:\r\n{paths}"))
|
|
|
}
|
|
|
|
|
|
-async fn manual_hello() -> impl Responder {
|
|
|
- HttpResponse::Ok().body("Hey there!")
|
|
|
+#[get("/{path:.+}")]
|
|
|
+async fn show_article(path: web::Path<(String,)>) -> impl Responder {
|
|
|
+ HttpResponse::Ok().body(format!("Specific article: {}", path.into_inner().0))
|
|
|
}
|
|
|
|
|
|
#[actix_web::main]
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
+ if !std::env::vars().any(|(k, _)| k == "RUST_LOG") {
|
|
|
+ std::env::set_var("RUST_LOG", "trace");
|
|
|
+ }
|
|
|
+
|
|
|
+ let config = Configuration::default();
|
|
|
+
|
|
|
+ let _ = fern::Dispatch::new()
|
|
|
+ .format(|out, message, record| {
|
|
|
+ out.finish(format_args!(
|
|
|
+ "{}[{}][{}] {}",
|
|
|
+ chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
|
|
|
+ record.target(),
|
|
|
+ record.level(),
|
|
|
+ message
|
|
|
+ ))
|
|
|
+ })
|
|
|
+ .level(log::LevelFilter::Info)
|
|
|
+ .chain(std::io::stdout())
|
|
|
+ .apply();
|
|
|
+
|
|
|
HttpServer::new(|| {
|
|
|
- App::new()
|
|
|
- .wrap(Logger::default())
|
|
|
- .service(hello)
|
|
|
- .service(echo)
|
|
|
- .route("/hey", web::get().to(manual_hello))
|
|
|
+ App::new().wrap(Logger::default()).service(
|
|
|
+ web::scope("/a")
|
|
|
+ .service(show_article)
|
|
|
+ .service(show_articles_list),
|
|
|
+ )
|
|
|
})
|
|
|
- .bind(("127.0.0.1", 8080))?
|
|
|
- .run()
|
|
|
- .await
|
|
|
-}
|
|
|
+ .bind((config.listen_addr, config.listen_port))?
|
|
|
+ .run()
|
|
|
+ .await
|
|
|
+}
|