summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2020-05-20 19:52:18 +0200
committerMelonai <einebeere@gmail.com>2020-05-20 19:52:18 +0200
commite58b453f24c8b4081361112862d29c34eb22009d (patch)
tree3437fb38ecb8b22ad5475d88f7e9ca901b320917 /src/main.rs
parenta00a8a867cae381982c7b8b77f07836ab4a504ed (diff)
downloadshorest-e58b453f24c8b4081361112862d29c34eb22009d.tar.zst
shorest-e58b453f24c8b4081361112862d29c34eb22009d.zip
port to react and better error handling in backend
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 72f1cad..6fb0073 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -69,14 +69,14 @@ fn add_to_database_safely(mut hash: String, user_url: String, connection: &PgCon
 }
 
 async fn root(req: HttpRequest) -> HttpResponse {
-    NamedFile::open("./client/index.html").unwrap().into_response(&req).unwrap()
+    NamedFile::open("./client/build/index.html").unwrap().into_response(&req).unwrap()
 }
 
 async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse {
     let user_url = match make_url(&params.url) {
         Ok(parse_result) => parse_result,
         Err(_) => {
-            return HttpResponse::BadRequest().body("The URL you entered does not follow the proper URL format.");
+            return HttpResponse::BadRequest().json(ErrorResponse{error: "The URL you entered does not follow the proper URL format.".to_string()});
         },
     };
     let hash = add_to_database_safely(get_hash_from_string(&user_url), user_url, &state.get().expect("Could not get a connection from pool"));
@@ -87,7 +87,7 @@ async fn shorten(params: Json<UserData>, state: Data<PoolState>) -> HttpResponse
 async fn redirect(info: Path<String>, state: Data<PoolState>) -> HttpResponse {
     match get_url_from_database(&info, &state.get().expect("Could not get a connection from pool")) {
         Ok(url) => HttpResponse::TemporaryRedirect().header("Location", url).finish(),
-        Err(_) => HttpResponse::NotFound().body("The URL you specified could not be found.")
+        Err(_) => HttpResponse::TemporaryRedirect().header("Location", "/").finish()
     }
 }
 
@@ -117,7 +117,7 @@ async fn main() -> std::io::Result<()> {
                     .route(web::get().to(redirect))
             )
             .service(
-                Files::new("/static/", "./client/static/")
+                Files::new("/client/", "./client/build/")
             )
     })
         .bind(("0.0.0.0", port))?