Use tera template and serve static assets

This commit is contained in:
Sam Samai 2021-09-12 10:07:00 +10:00
parent 1c1283ca66
commit 8f3a45e6ae
8 changed files with 115 additions and 555 deletions

View File

@ -14,6 +14,7 @@ futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
actix-http = "2"
actix-web = "3"
actix-files = "0.5"
tera = "1.8.0"
# remove `path = ""` in your own project
@ -22,7 +23,8 @@ sea-orm = { path = "../../", version = "^0.2", features = [
"sqlx-all",
"runtime-tokio-native-tls",
], default-features = false }
serde_json = { version = "^1" }
serde = "1"
env_logger = "0.8"
[dependencies.sqlx]
version = "^0.5"

View File

@ -1,40 +1,18 @@
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
// use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}
use actix_http::{body::Body, Response};
use actix_web::dev::ServiceResponse;
use actix_web::http::StatusCode;
use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
use actix_files as fs;
async fn manual_hello() -> impl Responder {
HttpResponse::Ok().body("Hey there!")
}
use tera::Tera;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
mod post;
pub use post::Entity as Post;
// use std::collections::HashMap;
// use actix_http::{body::Body, Response};
// use actix_web::dev::ServiceResponse;
// use actix_web::http::StatusCode;
// use actix_web::middleware::errhandlers::{ErrorHandlerResponse, ErrorHandlers};
// use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result};
// use tera::Tera;
// // store tera template in application state
// async fn index(
@ -55,252 +33,34 @@ async fn main() -> std::io::Result<()> {
// Ok(HttpResponse::Ok().content_type("text/html").body(s))
// }
// #[actix_web::main]
// async fn main() -> std::io::Result<()> {
// HttpServer::new(|| {
// App::new()
// .service(hello)
// .service(echo)
// .route("/hey", web::get().to(manual_hello))
// })
// .bind("127.0.0.1:8080")?
// .run()
// .await
// }
async fn list( tmpl: web::Data<tera::Tera>) -> Result<HttpResponse, Error> {
let posts: Vec<post::Model> = vec!();
let mut ctx = tera::Context::new();
ctx.insert("posts", &posts);
ctx.insert("page", &0);
ctx.insert("num_pages", &1);
let s = tmpl.render("index.html.tera", &ctx)
.map_err(|_| error::ErrorInternalServerError("Template error"))?;
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
// #[actix_web::main]
// async fn main() -> std::io::Result<()> {
// std::env::set_var("RUST_LOG", "actix_web=info");
// env_logger::init();
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
// println!("Listening on: 127.0.0.1:8080, open browser and visit have a try!");
// HttpServer::new(|| {
// let tera =
// Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
// App::new()
// .data(tera)
// .wrap(middleware::Logger::default()) // enable logger
// .service(web::resource("/").route(web::get().to(index)))
// .service(web::scope("").wrap(error_handlers()))
// })
// .bind("127.0.0.1:8080")?
// .run()
// .await
// }
// // Custom error handlers, to return HTML responses when an error occurs.
// fn error_handlers() -> ErrorHandlers<Body> {
// ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)
// }
// // Error handler for a 404 Page not found error.
// fn not_found<B>(res: ServiceResponse<B>) -> Result<ErrorHandlerResponse<B>> {
// let response = get_error_response(&res, "Page not found");
// Ok(ErrorHandlerResponse::Response(
// res.into_response(response.into_body()),
// ))
// }
// // Generic error handler.
// fn get_error_response<B>(res: &ServiceResponse<B>, error: &str) -> Response<Body> {
// let request = res.request();
// // Provide a fallback to a simple plain text response in case an error occurs during the
// // rendering of the error page.
// let fallback = |e: &str| {
// Response::build(res.status())
// .content_type("text/plain")
// .body(e.to_string())
// };
// let tera = request.app_data::<web::Data<Tera>>().map(|t| t.get_ref());
// match tera {
// Some(tera) => {
// let mut context = tera::Context::new();
// context.insert("error", error);
// context.insert("status_code", res.status().as_str());
// let body = tera.render("error.html", &context);
// match body {
// Ok(body) => Response::build(res.status())
// .content_type("text/html")
// .body(body),
// Err(_) => fallback(error),
// }
// }
// None => fallback(error),
// }
// }
// -----------------------------
// #[macro_use]
// extern crate rocket;
// use rocket::fairing::{self, AdHoc};
// use rocket::form::{Context, Form};
// use rocket::fs::{relative, FileServer};
// use rocket::request::FlashMessage;
// use rocket::response::{Flash, Redirect};
// use rocket::{Build, Request, Rocket};
// use rocket_db_pools::{sqlx, Connection, Database};
// use rocket_dyn_templates::{context, Template};
// use sea_orm::entity::*;
// mod pool;
// use pool::RocketDbPool;
// mod setup;
// #[derive(Database, Debug)]
// #[database("rocket_example")]
// struct Db(RocketDbPool);
// type Result<T, E = rocket::response::Debug<sqlx::Error>> = std::result::Result<T, E>;
// mod post;
// pub use post::Entity as Post;
// const DEFAULT_POSTS_PER_PAGE: usize = 25;
// #[get("/new")]
// fn new() -> Template {
// Template::render("new", &Context::default())
// }
// #[post("/", data = "<post_form>")]
// async fn create(conn: Connection<Db>, post_form: Form<post::Model>) -> Flash<Redirect> {
// let form = post_form.into_inner();
// post::ActiveModel {
// title: Set(form.title.to_owned()),
// text: Set(form.text.to_owned()),
// ..Default::default()
// }
// .save(&conn)
// .await
// .expect("could not insert post");
// Flash::success(Redirect::to("/"), "Post successfully added.")
// }
// #[post("/<id>", data = "<post_form>")]
// async fn update(conn: Connection<Db>, id: i32, post_form: Form<post::Model>) -> Flash<Redirect> {
// let post: post::ActiveModel = Post::find_by_id(id)
// .one(&conn)
// .await
// .unwrap()
// .unwrap()
// .into();
// let form = post_form.into_inner();
// post::ActiveModel {
// id: post.id,
// title: Set(form.title.to_owned()),
// text: Set(form.text.to_owned()),
// }
// .save(&conn)
// .await
// .expect("could not edit post");
// Flash::success(Redirect::to("/"), "Post successfully edited.")
// }
// #[get("/?<page>&<posts_per_page>")]
// async fn list(
// conn: Connection<Db>,
// posts_per_page: Option<usize>,
// page: Option<usize>,
// flash: Option<FlashMessage<'_>>,
// ) -> Template {
// let page = page.unwrap_or(0);
// let posts_per_page = posts_per_page.unwrap_or(DEFAULT_POSTS_PER_PAGE);
// let paginator = Post::find().paginate(&conn, posts_per_page);
// let num_pages = paginator.num_pages().await.ok().unwrap();
// let posts = paginator
// .fetch_page(page)
// .await
// .expect("could not retrieve posts");
// let flash = flash.map(FlashMessage::into_inner);
// Template::render(
// "index",
// context! {
// posts: posts,
// flash: flash,
// page: page,
// num_pages: num_pages,
// },
// )
// }
// #[get("/<id>")]
// async fn edit(conn: Connection<Db>, id: i32) -> Template {
// let post: Option<post::Model> = Post::find_by_id(id)
// .one(&conn)
// .await
// .expect("could not find post");
// Template::render(
// "edit",
// context! {
// post: post,
// },
// )
// }
// #[delete("/<id>")]
// async fn delete(conn: Connection<Db>, id: i32) -> Flash<Redirect> {
// let post: post::ActiveModel = Post::find_by_id(id)
// .one(&conn)
// .await
// .unwrap()
// .unwrap()
// .into();
// post.delete(&conn).await.unwrap();
// Flash::success(Redirect::to("/"), "Post successfully deleted.")
// }
// #[delete("/")]
// async fn destroy(conn: Connection<Db>) -> Result<()> {
// Post::delete_many().exec(&conn).await.unwrap();
// Ok(())
// }
// #[catch(404)]
// pub fn not_found(req: &Request<'_>) -> Template {
// Template::render(
// "error/404",
// context! {
// uri: req.uri()
// },
// )
// }
// async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
// let db_url = Db::fetch(&rocket).unwrap().db_url.clone();
// let conn = sea_orm::Database::connect(&db_url).await.unwrap();
// let _ = setup::create_post_table(&conn).await;
// Ok(rocket)
// }
// #[launch]
// fn rocket() -> _ {
// rocket::build()
// .attach(Db::init())
// .attach(AdHoc::try_on_ignite("Migrations", run_migrations))
// .mount("/", FileServer::from(relative!("/static")))
// .mount(
// "/",
// routes![new, create, delete, destroy, list, edit, update],
// )
// .register("/", catchers![not_found])
// .attach(Template::fairing())
// }
println!("Listening on: 127.0.0.1:8080");
HttpServer::new(|| {
let tera =
Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")).unwrap();
App::new()
.data(tera)
.wrap(middleware::Logger::default()) // enable logger
.service(fs::Files::new("/static", "./static").show_files_listing())
.service(web::resource("/").route(web::get().to(list)))
})
.bind("127.0.0.1:8080")?
.run()
.await
}

View File

@ -1,8 +1,8 @@
use rocket::serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize};
use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity, Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
#[serde(crate = "serde")]
pub struct Entity;
impl EntityName for Entity {
@ -12,9 +12,9 @@ impl EntityName for Entity {
}
#[derive(
Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Deserialize, Serialize, FromForm,
Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Deserialize, Serialize,
)]
#[serde(crate = "rocket::serde")]
#[serde(crate = "serde")]
pub struct Model {
#[serde(skip_deserializing, skip_serializing_if = "Option::is_none")]
pub id: Option<i32>,

View File

@ -1,81 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Rocket Todo Example</title>
<meta name="description" content="A todo application written in Rocket." />
<meta name="author" content="Sergio Benitez" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
href="//fonts.googleapis.com/css?family=Raleway:400,300,600"
rel="stylesheet"
type="text/css"
/>
<link rel="stylesheet" href="/css/normalize.css" />
<link rel="stylesheet" href="/css/skeleton.css" />
<link rel="stylesheet" href="/css/style.css" />
<link rel="icon" type="image/png" href="/images/favicon.png" />
</head>
<body>
<div class="container">
<p><!--Nothing to see here --></p>
{#
<div class="row">
<h4>Rocket Todo</h4>
<form action="/todo" method="post">
<div class="ten columns">
<input
type="text"
placeholder="enter a task description..."
name="description"
id="description"
value=""
autofocus
class="u-full-width {% if msg %}field-{{msg.0}}{% endif %}"
/>
{% if msg %}
<small class="field-{{msg.0}}-msg">
{{ msg.1 }}
</small>
{% endif %}
</div>
<div class="two columns">
<input type="submit" value="add task" />
</div>
</form>
</div>
<div class="row">
<div class="twelve columns">
<ul>
{% for task in tasks %} {% if task.completed %}
<li>
<span class="completed">{{ task.description }}</span>
<form class="inline" action="/todo/{{ task.id }}" method="post">
<input type="hidden" name="_method" value="put" />
<button class="small" type="submit">undo</button>
</form>
<form class="inline" action="/todo/{{ task.id }}" method="post">
<input type="hidden" name="_method" value="delete" />
<button class="primary small" type="submit">delete</button>
</form>
</li>
{% else %}
<li>
<form class="link" action="/todo/{{ task.id }}" method="post">
<input type="hidden" name="_method" value="put" />
<button class="link" type="submit">
{{ task.description }}
</button>
</form>
</li>
{% endif %} {% endfor %}
</ul>
</div>
</div>
#} {% block content %}{% endblock content %}
</div>
</body>
</html>

View File

@ -1,50 +0,0 @@
{% extends "base" %} {% block content %}
<div class="row">
<h4>Edit Post</h4>
<div class="twelve columns">
<div class="ten columns">
<form action="/{{ post.id }}" method="post">
<div class="twelve columns">
<input
type="text"
placeholder="title"
name="title"
id="title"
value="{{ post.title }}"
autofocus
class="u-full-width"
/>
<input
type="text"
placeholder="content"
name="text"
id="text"
value="{{ post.text }}"
autofocus
class="u-full-width"
/>
</div>
<div class="twelve columns">
<div class="two columns">
<a href="/">
<input type="button" value="cancel" />
</a>
</div>
<div class="eight columns"></div>
<div class="two columns">
<input type="submit" value="save post" />
</div>
</div>
</form>
</div>
<div class="two columns">
<form action="/{{ post.id }}" method="post">
<div class="two columns">
<input type="hidden" name="_method" value="delete" />
<input id="delete-button" type="submit" value="delete post" />
</div>
</form>
</div>
</div>
</div>
{% endblock content %}

View File

@ -1,46 +1,68 @@
{% extends "base" %} {% block content %}
<h1>Posts</h1>
{% if flash %}
<small class="field-{{ flash.0 }}-flash">
{{ flash.1 }}
</small>
{% endif %}
<table>
<tbody>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Text</th>
</tr>
</thead>
{% for post in posts %}
<tr class="post" onclick="window.location='/{{ post.id }}';">
<td>{{ post.id }}</td>
<td>{{ post.title }}</td>
<td>{{ post.text }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td></td>
<td>
{% if page == 0 %} Previous {% else %}
<a href="/?page={{ page - 1 }}">Previous</a>
{% endif %} | {% if page == num_pages - 1 %} Next {% else %}
<a href="/?page={{ page + 1 }}">Next</a>
{% endif %}
</td>
<td></td>
</tr>
</tfoot>
</table>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Actix Example</title>
<meta name="description" content="Actix - SeaOrm integration example" />
<meta name="author" content="Sam Samai" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<div class="twelve columns">
<a href="/new">
<input type="button" value="add post" />
</a>
</div>
<link
href="//fonts.googleapis.com/css?family=Raleway:400,300,600"
rel="stylesheet"
type="text/css"
/>
<link rel="stylesheet" href="/static/css/normalize.css" />
<link rel="stylesheet" href="/css/skeleton.css" />
<link rel="stylesheet" href="/static/css/style.css" />
<link rel="icon" type="image/png" href="/static/images/favicon.png" />
</head>
<body>
<div class="container">
<p><!--Nothing to see here --></p>
<h1>Posts</h1>
{# {% if flash %}
<small class="field-{{ flash.0 }}-flash">
{{ flash.1 }}
</small>
{% endif %} #}
<table>
<tbody>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Text</th>
</tr>
</thead>
{% for post in posts %}
<tr class="post" onclick="window.location='/{{ post.id }}';">
<td>{{ post.id }}</td>
<td>{{ post.title }}</td>
<td>{{ post.text }}</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td></td>
<td>
{% if page == 0 %} Previous {% else %}
<a href="/?page={{ page - 1 }}">Previous</a>
{% endif %} | {% if page == num_pages - 1 %} Next {% else %}
<a href="/?page={{ page + 1 }}">Next</a>
{% endif %}
</td>
<td></td>
</tr>
</tfoot>
</table>
{% endblock content %}
<div class="twelve columns">
<a href="/new">
<input type="button" value="add post" />
</a>
</div>
</div>
</body>
</html>

View File

@ -1,38 +0,0 @@
{% extends "base" %} {% block content %}
<div class="row">
<h4>New Post</h4>
<form action="/" method="post">
<div class="twelve columns">
<input
type="text"
placeholder="enter title"
name="title"
id="title"
value=""
autofocus
class="u-full-width"
/>
<input
type="text"
placeholder="enter content"
name="text"
id="text"
value=""
autofocus
class="u-full-width"
/>
</div>
<div class="twelve columns">
<div class="two columns">
<a href="/">
<input type="button" value="cancel" />
</a>
</div>
<div class="eight columns"></div>
<div class="two columns">
<input type="submit" value="save post" />
</div>
</div>
</form>
</div>
{% endblock content %}

View File

@ -2,9 +2,9 @@
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Rocket Todo Example</title>
<meta name="description" content="A todo application written in Rocket." />
<meta name="author" content="Sergio Benitez" />
<title>Rocket SeaORM Example</title>
<meta name="description" content="Example Rocket - SeaORM integration" />
<meta name="author" content="Sam Samai" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
@ -20,62 +20,7 @@
<body>
<div class="container">
<p><!--Nothing to see here --></p>
{#
<div class="row">
<h4>Rocket Todo</h4>
<form action="/todo" method="post">
<div class="ten columns">
<input
type="text"
placeholder="enter a task description..."
name="description"
id="description"
value=""
autofocus
class="u-full-width {% if msg %}field-{{msg.0}}{% endif %}"
/>
{% if msg %}
<small class="field-{{msg.0}}-msg">
{{ msg.1 }}
</small>
{% endif %}
</div>
<div class="two columns">
<input type="submit" value="add task" />
</div>
</form>
</div>
<div class="row">
<div class="twelve columns">
<ul>
{% for task in tasks %} {% if task.completed %}
<li>
<span class="completed">{{ task.description }}</span>
<form class="inline" action="/todo/{{ task.id }}" method="post">
<input type="hidden" name="_method" value="put" />
<button class="small" type="submit">undo</button>
</form>
<form class="inline" action="/todo/{{ task.id }}" method="post">
<input type="hidden" name="_method" value="delete" />
<button class="primary small" type="submit">delete</button>
</form>
</li>
{% else %}
<li>
<form class="link" action="/todo/{{ task.id }}" method="post">
<input type="hidden" name="_method" value="put" />
<button class="link" type="submit">
{{ task.description }}
</button>
</form>
</li>
{% endif %} {% endfor %}
</ul>
</div>
</div>
#} {% block content %}{% endblock content %}
{% block content %}{% endblock content %}
</div>
</body>
</html>