Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added static files #11

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ For JSON Postgres example [click here](https://github.com/saiumesh535/actix-serv
| Middleware | :heavy_check_mark: | [Link](https://github.com/saiumesh535/actix-server/pull/7) |
| Redis | :heavy_check_mark: | [Link](https://github.com/saiumesh535/actix-server/pull/6) |
| Email | :heavy_check_mark: | [Link](https://github.com/saiumesh535/actix-server/pull/10) |
| Static Files | :heavy_check_mark: | [Link](https://github.com/saiumesh535/actix-server/pull/9/files) |
| Validations | :x: | :x: |
| Download Files | :heavy_check_mark: | [Link](https://github.com/saiumesh535/actix-server/pull/9/files) |
| Validations | :x: | :x: |

Expand Down
4 changes: 2 additions & 2 deletions src/auth/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ pub async fn user_login(
let pool = db.get().await.unwrap();
let rows = pool.query(query, &[&user.email]).await.unwrap();
if rows.is_empty() {
return HttpResponse::Ok().json(ErrorResponse {
return HttpResponse::BadRequest().json(ErrorResponse {
message: String::from("check username and password"),
});
}
let hash: String = rows.get(0).unwrap().get("password");
let is_password_matches = verify_password(&hash, &user.password);
if !is_password_matches {
return HttpResponse::Ok().json(ErrorResponse {
return HttpResponse::BadRequest().json(ErrorResponse {
message: String::from("check username and password"),
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::redis_utils::connect_redis;
use actix_web::{App, HttpServer};
use actix_files::Files;
use actix_web::{middleware::Compress, App, HttpServer};
use deadpool_postgres::Config;
use dotenv;
use tokio_postgres::NoTls;
Expand Down Expand Up @@ -46,12 +47,14 @@ async fn main() -> std::io::Result<()> {
App::new()
.data(pool.clone())
.data(redis_client.clone())
.wrap(Compress::default())
.service(auth::auth_routes())
.service(Json::json_routes())
.service(errors::register_error_handlers())
.service(email::register_email_routes())
.service(downloads::register_download_routes())
.service(private::register_private().wrap(middleware::private::CheckToken))
.service(Files::new("/", "static").index_file("index.html"))
})
.bind("127.0.0.1:8000")?
.run()
Expand Down
14 changes: 14 additions & 0 deletions static/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
html,
body {
background-color: wheat;
}

h1#header {
text-align: center;
}

.container {
display: flex;
justify-content: center;
align-content: center;
}
18 changes: 18 additions & 0 deletions static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Actix server</title>
<link rel="stylesheet" href="./index.css">
<script type="text/javascript" src="./login.js"></script>
</head>
<body>
<h1 id="header">Welcome to Actix web</h1>
<div class="container">
<input name="email" placeholder="email" type="text" value="">
<input type="password" name="password" placeholder="password" type="text" value="">
<button id="loginButton">Login</button>
</div>
</body>
</html>
42 changes: 42 additions & 0 deletions static/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Example POST method implementation:
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
withCredentials: true,
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response; // parses JSON response into native JavaScript objects
}


(() => {
setTimeout(() => {
document.querySelector("#loginButton").onclick = async () => {
const email = document.querySelector('.container > input:nth-child(1)').value;
const password = document.querySelector('.container > input:nth-child(2)').value;
try {
const response = await postData('http://127.0.0.1:8000/auth/login', {
email, password
});
if(response.status !== 200) {
alert('check email and password');
return;
}
const responseBody = await response.json();
alert(responseBody.token);
} catch(error) {
console.error(error);
}
}
})
})();