diff options
| author | Melonai <einebeere@gmail.com> | 2020-05-20 21:16:34 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-05-20 21:16:34 +0200 |
| commit | e9f542ddc8b8230418b1e6fc1656677453ea5a10 (patch) | |
| tree | efe84fba111c308370a89fba61dd9e8548a01085 /src | |
| parent | a00a8a867cae381982c7b8b77f07836ab4a504ed (diff) | |
| parent | 58abd266b0b5ec37c5d7beea37abc2babd7d504a (diff) | |
| download | shorest-e9f542ddc8b8230418b1e6fc1656677453ea5a10.tar.zst shorest-e9f542ddc8b8230418b1e6fc1656677453ea5a10.zip | |
Merge pull request #1 from Melonai/react-port 0.2.0
React port
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 8 | ||||
| -rw-r--r-- | src/types.rs | 7 |
2 files changed, 10 insertions, 5 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(¶ms.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))? diff --git a/src/types.rs b/src/types.rs index d068043..0a4926e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -9,7 +9,12 @@ pub struct UserData { #[derive(Debug, Serialize)] pub struct UserResponse { - pub hash: String, + pub hash: String +} + +#[derive(Debug, Serialize)] +pub struct ErrorResponse { + pub error: String } #[derive(Queryable, Insertable)] |
