Skip to content
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
117 changes: 117 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions backend/.env

This file was deleted.

2 changes: 2 additions & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ db = { path = "../db" }
dotenvy = "0.15.7"
jsonwebtoken = {version = "10.2.0", features = ["rust_crypto"]}
serde = {version = "1.0.228", features = ["derive"]}
chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "1.6", features = ["serde", "v4"] }
27 changes: 14 additions & 13 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
use actix_web::{App, HttpServer, dev::ServiceRequest, web};
use db::Db;

use crate::routes::user::{create_user, me_handler, sign_in};

use actix_web::{App, HttpServer, web,};
pub mod routes;
pub mod middleware;
pub use routes::*;
pub mod models;
pub mod middleware;
pub use middleware::*;

#[actix_web::main]
async fn main() {
async fn main(){
dotenvy::dotenv().unwrap();
let db = Db::new().await.unwrap();
let _ = HttpServer::new(move || {
let db = db::Db::new()
.await
.expect("Failed to connect to database");
let _ = HttpServer::new(move || { //move || makes a closure that captures the db variable so each worker thread gets a clone.
App::new()
.service(web::resource("/signup").route(web::post().to(create_user)))
.service(web::resource("/signin").route(web::post().to(sign_in)))
.wrap(my_middleware)
.service(web::resource("/me").route(web::get().to(me_handler)))
.service(web::resource("/create_room").route(web::post().to(create_room)))
.service(web::resource("/get_room").route(web::get().to(get_room)))
.service(web::resource("/join_room").route(web::post().to(join_rooms)))
.app_data(actix_web::web::Data::new(db.clone()))

})
.bind("0.0.0.0:3000")
.unwrap()
.run()
.await;

}
15 changes: 8 additions & 7 deletions backend/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::{env, future::{Ready, ready}};

use actix_web::{
dev::Payload, error::ErrorUnauthorized, web, FromRequest, HttpRequest,
dev::Payload, error::ErrorUnauthorized, FromRequest, HttpRequest,
};
use jsonwebtoken::{decode, DecodingKey, Validation};
use std::{env, future::Ready, future::ready};
use jsonwebtoken::{DecodingKey, Validation, decode};

use crate::routes::user::Claims;
use crate::models::Claims;

pub struct JwtClaims(pub Claims);
pub struct JwtClaims (pub Claims);

impl FromRequest for JwtClaims {
type Error = actix_web::Error;
type Future = Ready<Result<Self, Self::Error>>;
type Future = Ready<Result<Self,Self::Error>>;

fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let auth_header = req.headers().get("Authorization");

if let Some(header_value) = auth_header {
Expand Down
4 changes: 4 additions & 0 deletions backend/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod user;
pub use user::*;
pub mod room;
pub use room::*;
14 changes: 14 additions & 0 deletions backend/src/models/room.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Serialize,Deserialize)]
pub struct UserRoomRequest{
pub id : Uuid
}


#[derive(Serialize,Deserialize)]
pub struct UserJoinRoomRequest{
pub room_id : Uuid,
pub player_o_id :Uuid
}
35 changes: 35 additions & 0 deletions backend/src/models/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;


#[derive(Serialize, Deserialize)]
pub struct UserRequest {
pub username: String,
pub password: String
}

#[derive(Serialize,Deserialize)]
pub struct UserResponse{
pub id : Uuid
}

#[derive(Serialize,Deserialize)]
pub struct SigninResponse {
pub token: String
}

#[derive(Serialize,Deserialize)]
pub struct Claims {
pub sub : Uuid,
pub exp : usize

}

impl Claims{
pub fn new(sub:Uuid) ->Self{
Self {
sub,
exp:1000000000000,
}
}
}
5 changes: 4 additions & 1 deletion backend/src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod user;
pub mod user;
pub use user::*;
pub mod room;
pub use room::*;
Loading