diff --git a/server/Cargo.toml b/server/Cargo.toml new file mode 100644 index 0000000..579f66c --- /dev/null +++ b/server/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "server" +version = "0.1.0" +authors = ["Gulliver "] +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 = "*" \ No newline at end of file diff --git a/server/development.db b/server/development.db new file mode 100644 index 0000000..4705b23 Binary files /dev/null and b/server/development.db differ diff --git a/server/src/main.rs b/server/src/main.rs new file mode 100644 index 0000000..4bd682b --- /dev/null +++ b/server/src/main.rs @@ -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>; + +#[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::::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 +} diff --git a/server/src/routes.rs b/server/src/routes.rs new file mode 100644 index 0000000..24c243c --- /dev/null +++ b/server/src/routes.rs @@ -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) -> impl Responder { + format!("Project id:{}", id) +} + + +pub fn load_skills( + conn: &SqliteConnection, +) -> Result, diesel::result::Error> { + use crate::sqlite3::db::schema::skills::dsl::*; + + let s = skills.load::(conn); + + return s +} + +#[get("/skills")] +async fn skills(pool: web::Data) -> 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) -> Result// { +// let results = schema::skills::table.load::(&state.db); + +// match results { +// Ok(skill) => Ok(HttpResponse::Ok().json(skill)), +// Err(_) => Ok(HttpResponse::InternalServerError().into()), +// } + +//}