1
0
Fork 0

update rocket and add diesel

This commit is contained in:
Florian RICHER 2021-07-08 09:00:00 +02:00
parent e7ce218de5
commit c54cdc98ba
11 changed files with 1414 additions and 308 deletions

3
.idea/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

6
.idea/misc.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/project_test.iml" filepath="$PROJECT_DIR$/project_test.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

1616
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,10 +1,13 @@
[package]
name = "hello-rocket"
name = "project_test"
version = "0.1.0"
authors = ["Florian RICHER <florian.richer@unova.fr>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.7"
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"] }
diesel = { version = "1.3", features = ["sqlite", "r2d2"] }
diesel_migrations = "1.3"

2
Rocket.toml Normal file
View file

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

BIN
db/dev.db Normal file

Binary file not shown.

12
project_test.iml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUST_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -1,3 +1,25 @@
fn main() {
println!("Hello, world!");
#[macro_use] extern crate diesel;
use rocket::*;
use rocket_sync_db_pools::{database};
mod task;
#[database("sqlite_logs")]
pub struct DbConn(diesel::SqliteConnection);
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[get("/log/<id>")]
async fn get_log(conn: DbConn, id: i32) -> String {
let result = task::Task::all(&conn).await;
format!("test {}, {:?}", id, result)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, get_log]).attach(DbConn::fairing())
}

34
src/task.rs Normal file
View file

@ -0,0 +1,34 @@
use rocket::serde::Serialize;
use diesel::{self, result::QueryResult, Queryable, Insertable, prelude::*};
mod schema {
table! {
tasks {
id -> Nullable<Integer>,
description -> Text,
completed -> Bool,
}
}
}
use schema::tasks;
use schema::tasks::dsl::{tasks as all_tasks, completed as task_completed};
use crate::DbConn;
#[derive(Serialize, Queryable, Insertable, Debug, Clone)]
#[serde(crate = "rocket::serde")]
#[table_name="tasks"]
pub struct Task {
pub id: Option<i32>,
pub description: String,
pub completed: bool
}
impl Task {
pub async fn all(conn: &DbConn) -> QueryResult<Vec<Task>> {
conn.run(|c| {
all_tasks.order(tasks::id.desc()).load::<Task>(c)
}).await
}
}