diff options
| author | Melonai <einebeere@gmail.com> | 2020-05-07 04:29:30 +0200 |
|---|---|---|
| committer | Melonai <einebeere@gmail.com> | 2020-05-07 04:29:30 +0200 |
| commit | 1e06820480fb2d9cf9db1373817d5980a891d5b4 (patch) | |
| tree | 63d0d45190f95b40f20dfd8a718a912434286db3 /src | |
| parent | 385fcdd46b536d28094a96f3f6be0e87496277a1 (diff) | |
| download | shorest-1e06820480fb2d9cf9db1373817d5980a891d5b4.tar.zst shorest-1e06820480fb2d9cf9db1373817d5980a891d5b4.zip | |
interaction between API and client
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index ab4c044..12c1356 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,12 @@ use schema::links; mod types; use types::*; -use actix_web::{middleware, web, HttpServer, App, HttpResponse, Result}; +use actix_web::{middleware, web, HttpServer, App, HttpResponse, Result, HttpRequest}; use actix_web::web::{Json, Path, Data}; use diesel::{PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, QueryResult}; use dotenv::dotenv; use diesel::r2d2::{ConnectionManager, Pool}; +use actix_files::{Files, NamedFile}; fn establish_connection() -> Pool<ConnectionManager<PgConnection>> { dotenv().ok(); @@ -58,16 +59,14 @@ fn add_to_database_safely(mut hash: String, user_url: String, connection: &PgCon } } Err(_) => { - if add_entry_to_database(Entry { hash: hash.clone() , url: user_url }, connection).is_err() { - - } + add_entry_to_database(Entry { hash: hash.clone() , url: user_url }, connection).unwrap(); } }; hash } -async fn root() -> HttpResponse { - HttpResponse::Ok().body("Please make a POST request to / in the format {'url': '<your_url>'} with the URL you want to shorten!") +async fn root(req: HttpRequest) -> HttpResponse { + NamedFile::open("./client/index.html").unwrap().into_response(&req).unwrap() } async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse { @@ -77,7 +76,7 @@ async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse return HttpResponse::BadRequest().body("The URL you entered does not follow the proper URL format."); }, }; - let hash= add_to_database_safely(get_hash_from_string(&user_url), user_url, &state.get().expect("Could not get a connection from pool")); + let hash = add_to_database_safely(get_hash_from_string(&user_url), user_url, &state.get().expect("Could not get a connection from pool")); HttpResponse::Ok().json(UserResponse{ hash }) } @@ -109,6 +108,9 @@ async fn main() -> std::io::Result<()> { web::resource("/{hash}") .route(web::get().to(redirect)) ) + .service( + Files::new("/static/", "./client/static/") + ) }) .bind("localhost:3000")? .run() |
