update rocket and add diesel
This commit is contained in:
parent
e7ce218de5
commit
c54cdc98ba
11 changed files with 1414 additions and 308 deletions
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
6
.idea/misc.xml
Normal file
6
.idea/misc.xml
Normal 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
8
.idea/modules.xml
Normal 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
6
.idea/vcs.xml
Normal 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
1616
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,10 +1,13 @@
|
||||||
[package]
|
[package]
|
||||||
name = "hello-rocket"
|
name = "project_test"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Florian RICHER <florian.richer@unova.fr>"]
|
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[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
2
Rocket.toml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[global.databases]
|
||||||
|
sqlite_logs = { url = "db/dev.db", pool_size = 20 }
|
BIN
db/dev.db
Normal file
BIN
db/dev.db
Normal file
Binary file not shown.
12
project_test.iml
Normal file
12
project_test.iml
Normal 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>
|
26
src/main.rs
26
src/main.rs
|
@ -1,3 +1,25 @@
|
||||||
fn main() {
|
#[macro_use] extern crate diesel;
|
||||||
println!("Hello, world!");
|
|
||||||
|
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
34
src/task.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue