:: rewrite ::
This commit is contained in:
parent
e30a136cc2
commit
775cbae281
23 changed files with 724 additions and 326 deletions
64
src/main.rs
64
src/main.rs
|
@ -3,26 +3,25 @@ mod logger;
|
|||
extern crate rocket;
|
||||
extern crate rocket_dyn_templates;
|
||||
|
||||
use rocket::response::content::RawJson;
|
||||
use rocket::{get, post, launch, build, routes};
|
||||
use rocket::fs::FileServer;
|
||||
use rocket_dyn_templates::{Template, context};
|
||||
use rocket::serde::{Deserialize, Serialize, json::Json};
|
||||
use rocket::serde::{json::Json, Deserialize, Serialize};
|
||||
use rocket::{build, get, launch, post, routes};
|
||||
use rocket_dyn_templates::{context, Template};
|
||||
|
||||
use chrono::Local;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::fs::read_to_string;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::logger::iwakulog::{LogLevel, log};
|
||||
use crate::logger::iwakulog::{log, LogLevel};
|
||||
|
||||
const ASSETS_DIR: &str = "./assets";
|
||||
const GUEST_MESSAGES: &str = "./guests.json";
|
||||
const ASSETS_DIR: &str = "./assets";
|
||||
const GUEST_MESSAGES: &str = "./guests.json";
|
||||
const DATE_TIME_FORMAT: &str = "%Y-%m-%d | %H:%M";
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
|
@ -32,7 +31,6 @@ struct GuestMessage<'r> {
|
|||
body: Cow<'r, str>,
|
||||
}
|
||||
|
||||
|
||||
fn check_file(path: &str) -> bool {
|
||||
Path::new(path).exists()
|
||||
}
|
||||
|
@ -46,7 +44,7 @@ fn read_guest_messages() -> Result<Value, Box<dyn std::error::Error>> {
|
|||
fn post_guest_message(name: &str, body: &str) {
|
||||
let mut guest_json = match read_guest_messages() {
|
||||
Ok(x) => x,
|
||||
Err(x) => panic!("Error while reading guest messages: {x:?}")
|
||||
Err(x) => panic!("Error while reading guest messages: {x:?}"),
|
||||
};
|
||||
let messages = guest_json["messages"]
|
||||
.as_array_mut()
|
||||
|
@ -58,10 +56,21 @@ fn post_guest_message(name: &str, body: &str) {
|
|||
"date": date.timestamp(),
|
||||
}));
|
||||
let mut file = File::create(GUEST_MESSAGES).expect("Couldn't read msgs");
|
||||
file.write_all(guest_json.to_string().as_bytes()).expect("wah");
|
||||
file.write_all(guest_json.to_string().as_bytes())
|
||||
.expect("wah");
|
||||
}
|
||||
#[get("/abuse")]
|
||||
fn abuse() -> Template {
|
||||
Template::render("abuse", context! {})
|
||||
}
|
||||
#[get("/post")]
|
||||
fn post() -> Template {
|
||||
Template::render("post", context! {})
|
||||
}
|
||||
#[get("/members")]
|
||||
fn members() -> Template {
|
||||
Template::render("members", context! {})
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> Template {
|
||||
|
@ -74,11 +83,16 @@ fn post_msg(input: Json<GuestMessage<'_>>) {
|
|||
return;
|
||||
}
|
||||
|
||||
#[get("/services")]
|
||||
fn services() -> Template {
|
||||
Template::render("services", context! {})
|
||||
}
|
||||
|
||||
#[get("/guestbook")]
|
||||
fn guests() -> Template {
|
||||
let guest_json = match read_guest_messages() {
|
||||
Ok(x) => x,
|
||||
Err(x) => panic!("Error while reading guest messages: {x:?}")
|
||||
Err(x) => panic!("Error while reading guest messages: {x:?}"),
|
||||
};
|
||||
Template::render("guest", &guest_json)
|
||||
}
|
||||
|
@ -86,13 +100,23 @@ fn guests() -> Template {
|
|||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
if !check_file(GUEST_MESSAGES) {
|
||||
log(LogLevel::Warn, format!("Guest messages file ({}) has not been found. Creating a new one.", GUEST_MESSAGES).as_str());
|
||||
log(
|
||||
LogLevel::Warn,
|
||||
format!(
|
||||
"Guest messages file ({}) has not been found. Creating a new one.",
|
||||
GUEST_MESSAGES
|
||||
)
|
||||
.as_str(),
|
||||
);
|
||||
let mut file = File::create(GUEST_MESSAGES).expect("Failed to create file.");
|
||||
file.write_all(b"{\"messages\":[]}").expect("Failed to write to file.");
|
||||
file.write_all(b"{\"messages\":[]}")
|
||||
.expect("Failed to write to file.");
|
||||
}
|
||||
build()
|
||||
.mount("/", routes![index, guests, post_msg])
|
||||
.mount(
|
||||
"/",
|
||||
routes![index, guests, post_msg, members, post, services, abuse],
|
||||
)
|
||||
.mount("/assets", FileServer::from(ASSETS_DIR))
|
||||
.attach(Template::fairing())
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue