hurray, its the first time we get some skills back from database :)
This commit is contained in:
parent
91deda2614
commit
a391919784
15
server/Cargo.toml
Normal file
15
server/Cargo.toml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "server"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Gulliver <gulliver@traumkristalle.net>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
dotenv = "0.13.0"
|
||||||
|
diesel = { version = "*", features = ["sqlite","r2d2"] }
|
||||||
|
actix-web = "3"
|
||||||
|
sqlite3 = { path = "../model/sqlite3" }
|
||||||
|
serde = { version = "1.0" , features = ["derive"]}
|
||||||
|
futures = "*"
|
BIN
server/development.db
Normal file
BIN
server/development.db
Normal file
Binary file not shown.
40
server/src/main.rs
Normal file
40
server/src/main.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#![allow(proc_macro_derive_resolution_fallback)]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate diesel;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate sqlite3;
|
||||||
|
|
||||||
|
use actix_web::{ HttpServer, Responder, middleware, App};
|
||||||
|
use diesel::sqlite::SqliteConnection;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
mod routes;
|
||||||
|
|
||||||
|
use self::routes::{index,projects,skills};
|
||||||
|
use diesel::r2d2::{self, ConnectionManager};
|
||||||
|
|
||||||
|
|
||||||
|
type DbPool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
|
||||||
|
|
||||||
|
#[actix_web::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
|
|
||||||
|
// set up database connection pool
|
||||||
|
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||||
|
let manager = ConnectionManager::<SqliteConnection>::new(connspec);
|
||||||
|
let pool = r2d2::Pool::builder()
|
||||||
|
.build(manager)
|
||||||
|
.expect("Failed to create pool.");
|
||||||
|
|
||||||
|
HttpServer::new(move || {
|
||||||
|
App::new()
|
||||||
|
.data(pool.clone())
|
||||||
|
.service(index)
|
||||||
|
.service(projects)
|
||||||
|
.service(skills)
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8080")?
|
||||||
|
.run()
|
||||||
|
.await
|
||||||
|
}
|
60
server/src/routes.rs
Normal file
60
server/src/routes.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use crate::{sqlite3::db::schema, sqlite3::db::models};
|
||||||
|
use actix_web::{
|
||||||
|
web,get,
|
||||||
|
Responder,
|
||||||
|
Result,
|
||||||
|
error::{ErrorInternalServerError, ErrorNotFound},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
use crate::DbPool;
|
||||||
|
|
||||||
|
#[get("/{id}/{name}/index.html")]
|
||||||
|
pub async fn index( web::Path((id, name)): web::Path<(u32, String)>)
|
||||||
|
-> impl Responder {
|
||||||
|
format!("Hello {}! id:{}", name, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/projects/{id}")]
|
||||||
|
pub async fn projects(web::Path(id): web::Path<u32>) -> impl Responder {
|
||||||
|
format!("Project id:{}", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn load_skills(
|
||||||
|
conn: &SqliteConnection,
|
||||||
|
) -> Result<Vec<models::Skill>, diesel::result::Error> {
|
||||||
|
use crate::sqlite3::db::schema::skills::dsl::*;
|
||||||
|
|
||||||
|
let s = skills.load::<models::Skill>(conn);
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/skills")]
|
||||||
|
async fn skills(pool: web::Data<DbPool>) -> impl Responder
|
||||||
|
{
|
||||||
|
let conn = pool.get().expect("couldn't get db connection from pool");
|
||||||
|
|
||||||
|
// use web::block to offload blocking Diesel code without blocking server thread
|
||||||
|
let skills = web::block(move || load_skills( &conn))
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
// HttpResponse::InternalServerError().reason("").finish()
|
||||||
|
});
|
||||||
|
web::Json(skills.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
//#[get("/skills/{id}")]
|
||||||
|
//pub /*async*/ fn skills(state: web::Data<AppState>) -> Result<impl Responder>// {
|
||||||
|
// let results = schema::skills::table.load::<models::Skill>(&state.db);
|
||||||
|
|
||||||
|
// match results {
|
||||||
|
// Ok(skill) => Ok(HttpResponse::Ok().json(skill)),
|
||||||
|
// Err(_) => Ok(HttpResponse::InternalServerError().into()),
|
||||||
|
// }
|
||||||
|
|
||||||
|
//}
|
Loading…
Reference in New Issue
Block a user