1
0
Fork 0

Add template

This commit is contained in:
Florian RICHER 2021-07-08 23:29:40 +02:00
parent 4bee273216
commit 2d1d5b66a9
8 changed files with 727 additions and 44 deletions

704
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -2,6 +2,7 @@
name = "project_test"
version = "0.1.0"
edition = "2018"
publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -9,5 +10,6 @@ edition = "2018"
rocket = { version = "0.5.0-rc.1", features = ["secrets", "tls", "json"] }
rocket_codegen = "0.5.0-rc.1"
rocket_sync_db_pools = { version = "0.1.0-rc.1", features = ["diesel_sqlite_pool"] }
rocket_dyn_templates = { version = "0.1.0-rc.1", features = ["tera"] }
diesel = { version = "1.4.0", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.4.0"

View file

@ -1,2 +1,5 @@
[default]
template_dir = "views"
[global.databases]
sqlite_logs = { url = "db/dev.db", pool_size = 20 }

View file

@ -1,8 +1,17 @@
use rocket::get;
use rocket::{get, catch};
use rocket::serde::json::{Value, json};
pub mod tasks_controller;
#[get("/")]
pub fn index() -> &'static str {
"Hello, world!"
}
#[catch(404)]
pub fn not_found() -> Value {
json!({
"status": "error",
"reason": "Resource was not found."
})
}

View file

@ -1,6 +1,22 @@
use rocket::serde::json::Json;
use rocket::serde::{json::Json, Serialize};
use rocket::{get, post, patch, delete};
use crate::models::{DbConn, task::Task};
use rocket_dyn_templates::{Template};
#[get("/")]
pub async fn index_template(conn: DbConn) -> Template {
#[derive(Serialize, Debug)]
#[serde(crate = "rocket::serde")]
struct Data {
title: String,
tasks: Vec<Task>
}
Template::render("tasks/index", Data {
title: "Hello".to_string(),
tasks: Task::all(&conn).await.unwrap(),
})
}
#[get("/tasks")]
pub async fn index(conn: DbConn) -> Option<Json<Vec<Task>>> {

View file

@ -7,6 +7,7 @@ mod controllers;
use rocket::*;
use rocket::fairing::AdHoc;
use models::DbConn;
use rocket_dyn_templates::Template;
async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
embed_migrations!();
@ -21,6 +22,9 @@ async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
fn rocket() -> _ {
rocket::build()
.mount("/", routes![
controllers::tasks_controller::index_template
])
.mount("/api/", routes![
controllers::index,
controllers::tasks_controller::index,
controllers::tasks_controller::show,
@ -28,6 +32,10 @@ fn rocket() -> _ {
controllers::tasks_controller::update,
controllers::tasks_controller::delete
])
.register("/", catchers![
controllers::not_found
])
.attach(DbConn::fairing())
.attach(Template::fairing())
.attach(AdHoc::on_ignite("Run Migrations", run_migrations))
}

15
views/layout.html.tera Normal file
View file

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Tera Demo - {{ title }}</title>
</head>
<body>
{% block content %}{% endblock content %}
<footer>
<a href="/">Home</a>
</footer>
</body>
</html>

View file

@ -0,0 +1,10 @@
{% extends "layout" %}
{% block content %}
<h3>Here are your items:</h3>
<ul>
{% for task in tasks %}
<li>{{ task.id }} {{ task.description }} {{ task.completed }}</li>
{% endfor %}
</ul>
{% endblock content %}