diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 43 | ||||
| -rw-r--r-- | src/templates.rs | 3 | ||||
| -rw-r--r-- | src/ui.rs | 138 |
3 files changed, 123 insertions, 61 deletions
diff --git a/src/main.rs b/src/main.rs index c9afc46..8a1548a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,20 +11,33 @@ mod db; mod structures; use rocket_contrib::templates::Template; +use actix_web::{ + error::ErrorInternalServerError, get, middleware::Logger, web, + App, Error, HttpResponse, HttpServer +}; -fn main() { - rocket::ignite() - .attach(Template::fairing()) - .register(catchers![ - ui::not_found - ]) - .mount("/", routes![ - ui::index, - ui::boards, - ui::board - ]) - .mount("/api/v1/", routes![ - api::post - ]) - .launch(); +#[actix_web::main] +async fn main() -> std::io::Result<()> { + // rocket::ignite() + // .attach(Template::fairing()) + // .register(catchers![ + // ui::not_found + // ]) + // .mount("/", routes![ + // ui::index, + // ui::boards, + // ui::board + // ]) + // .mount("/api/v1/", routes![ + // api::post + // ]) + // .launch(); + + HttpServer::new(move || App::new().wrap(Logger::default()) + .service(ui::index) + .service(ui::boards) + ) + .bind("127.0.0.1:8000")? + .run() + .await } diff --git a/src/templates.rs b/src/templates.rs new file mode 100644 index 0000000..b28b04f --- /dev/null +++ b/src/templates.rs @@ -0,0 +1,3 @@ + + + @@ -1,60 +1,106 @@ -use rocket_contrib::templates::Template; +use std::collections::HashMap; + +use actix_web::{ + error::ErrorInternalServerError, get, web, + Error, HttpResponse +}; + +use yarte::{TemplateMin, Render, Template}; use crate::db::*; -// GET: Index. +use serde::Serialize; + +#[derive(TemplateMin)] +#[template(path = "index")] +pub struct IndexTemplate { + pub query: web::Query<HashMap<String, String>> +} + +/// GET: Index. #[get("/")] -pub fn index() -> Template { - Template::render("index", &()) +pub async fn index( + query: web::Query<HashMap<String, String>> +) -> Result<HttpResponse, Error> { + IndexTemplate { query } + .call() + .map(|body| { + HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(body) + }) + .map_err(|_| ErrorInternalServerError("Some error message.")) +} + +#[derive(Template)] +#[template(path = "routes/boards")] +pub struct BoardsTemplate<S: Render> { + // pub threads: serde_json::Value + pub threads: S } -#[catch(404)] -pub fn not_found() -> Template { - Template::render("404", &()) +/// GET: Check out all public boards. +#[get("/boards")] +pub async fn boards() -> Result<HttpResponse, Error> { + let context = get_boards().unwrap(); + let threads = serde_json::json!(&context); + BoardsTemplate { threads } + .call() + .map(|body| { + HttpResponse::Ok() + .content_type("text/html; charset=utf-8") + .body(body) + }) + .map_err(|_| ErrorInternalServerError("Some error message.")) } -// GET: Make a new thread. -// #[get("/post")] -// pub fn make_post() -> Template { -// Template::render("post", &()) +// #[catch(404)] +// pub fn not_found() -> Template { +// Template::render("404", &()) // } -// GET: Check out all the threads. -// #[get("/threads")] -// pub fn threads() -> Template { -// let context = get_all_threads().unwrap(); +// // GET: Make a new thread. +// // #[get("/post")] +// // pub fn make_post() -> Template { +// // Template::render("post", &()) +// // } + +// // GET: Check out all the threads. +// // #[get("/threads")] +// // pub fn threads() -> Template { +// // let context = get_all_threads().unwrap(); +// // let threads = serde_json::json!(&context); +// // Template::render("threads", threads) +// // } + +// // GET: Check out all the boards. +// #[get("/boards")] +// pub fn boards() -> Template { +// let context = get_boards().unwrap(); // let threads = serde_json::json!(&context); -// Template::render("threads", threads) +// Template::render("boards", threads) // } -// GET: Check out all the boards. -#[get("/boards")] -pub fn boards() -> Template { - let context = get_boards().unwrap(); - let threads = serde_json::json!(&context); - Template::render("boards", threads) -} +// // GET: Check out all threads within a board. +// #[get("/board/<board>")] +// pub fn board(board: String) -> Template { +// let boards = serde_json::json!(get_boards().unwrap()); -// GET: Check out all threads within a board. -#[get("/board/<board>")] -pub fn board(board: String) -> Template { - let boards = serde_json::json!(get_boards().unwrap()); - - let mut board_count: i32 = 0; - for i in boards.as_array().unwrap() { - if i.get("tag") == Some(&serde_json::json!(board)) { - board_count += 1; - } - } - if board_count == 0 { return Template::render("404", &()); } - - // Return 404 if the board is not found. - // This doesn't work, but I kept it because I might try to fix it later. - // if !boards.as_array().unwrap().contains(&serde_json::json!(board)) { - // return Template::render("404", &()); - // } - - let context = get_threads(board).unwrap(); - let threads = serde_json::json!(&context); - Template::render("threads", threads) -} +// let mut board_count: i32 = 0; +// for i in boards.as_array().unwrap() { +// if i.get("tag") == Some(&serde_json::json!(board)) { +// board_count += 1; +// } +// } +// if board_count == 0 { return Template::render("404", &()); } + +// // Return 404 if the board is not found. +// // This doesn't work, but I kept it because I might try to fix it later. +// // if !boards.as_array().unwrap().contains(&serde_json::json!(board)) { +// // return Template::render("404", &()); +// // } + +// let context = get_threads(board).unwrap(); +// let threads = serde_json::json!(&context); +// Template::render("threads", threads) +// } |