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

backend/feat: #20 Added Driver Module #21

Merged
merged 1 commit into from
May 2, 2024
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
8 changes: 8 additions & 0 deletions .github/workflows/cross_native.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ on:
pull_request:
branches: [ "main" ]

permissions:
statuses: write
pull-requests: write

jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -25,3 +29,7 @@ jobs:
with:
channel: stable
- run: flutter build apk
- name: Update Status
uses: ouzi-dev/commit-status-updater@v2
with:
status: "${{ job.status }}"
25 changes: 25 additions & 0 deletions .github/workflows/driver.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Rust

on: [pull_request]

env:
CARGO_TERM_COLOR: always
permissions:
statuses: write
pull-requests: write

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: Backend/crates/driver/

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Update Status
uses: ouzi-dev/commit-status-updater@v2
with:
status: "${{ job.status }}"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: Backend/
working-directory: Backend/crates/user/

steps:
- uses: actions/checkout@v3
Expand Down
45 changes: 6 additions & 39 deletions Backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
[package]
name = "stosp"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.3.0"
actix-rt = "2.8.0"
chrono = { version = "0.4", features = ["serde"] }
dotenv = "0.15.0"
diesel = { version = "2.0.3", features = ["postgres", "r2d2", "uuid", "chrono"] }
diesel_migrations = "2.0.0"
env_logger = "0.11.1"
lazy_static = "1.4"
listenfd = "1.0.0"
log = "0.4"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
r2d2 = "0.8"
rand = "0.8"
futures = "0.3.26"
futures-util = "0.3.26"
futures-macro = "0.3.26"
chacha20poly1305 = "0.10.1"
base64 = "0.22.0"
jsonwebtoken = "9.2.0"
serde_dhall ="0.12.1"
uuid = { version = "1.3.0", features = ["serde", "v4"] }
cargo-watch = "8.5.2"
cargo-run-script = "0.2.0"
geo = { version = "0.28.0", features = ["use-serde"] }
geojson = { version = "0.24.1", features = ["geo-types"] }

[package.metadata.scripts]
start = "cargo watch -x run"
goodbye = "echo Goodbye"
install = "cargo install --path . --force"
[workspace]
resolver = "2"
members = ["crates/*"]
package.name = "stosp"
package.edition = "2021"
package.license = "Apache-2.0"
19 changes: 19 additions & 0 deletions Backend/crates/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "common"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.5.1"
actix-rt = "2.9.0"
chrono = { version = "0.4", features = ["serde"] }
diesel = { version = "2.1.6", features = ["postgres", "r2d2", "uuid", "chrono"] }
diesel_migrations = "2.1.0"
geo = { version = "0.28.0", features = ["use-serde"] }
geojson = { version = "0.24.1", features = ["geo-types"] }
jsonwebtoken = "9.3.0"
serde = {version = "1.0.199", features = ["derive"]}
serde_json = "1.0.116"
log = "0.4"
1 change: 1 addition & 0 deletions Backend/crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tools;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl SessionToken {
SessionToken {
id,
exp,
iat: get_current_time().timestamp(),
iat: get_current_time().and_utc().timestamp(),
}
}
}
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions Backend/crates/driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "driver"
version = "0.1.0"
edition.workspace = true
license.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4.5.1"
actix-rt = "2.9.0"
chrono = { version = "0.4", features = ["serde"] }
dotenv = "0.15.0"
diesel = { version = "2.1.6", features = ["postgres", "r2d2", "uuid", "chrono"] }
diesel_migrations = "2.1.0"
lazy_static = "1.4"
listenfd = "1.0.1"
log = "0.4"
serde = {version = "1.0.199", features = ["derive"]}
serde_json = "1.0.116"
r2d2 = "0.8.10"
rand = "0.8.5"
futures = "0.3.30"
futures-util = "0.3.30"
futures-macro = "0.3.30"
chacha20poly1305 = "0.10.1"
base64 = "0.22.0"
jsonwebtoken = "9.3.0"
serde_dhall ="0.12.1"
uuid = { version = "1.8.0", features = ["serde", "v4"] }
cargo-watch = "8.5.2"
cargo-run-script = "0.2.0"
geo = { version = "0.28.0", features = ["use-serde"] }
geojson = { version = "0.24.1", features = ["geo-types"] }
common = { path = "../common" }

[package.metadata.scripts]
start = "cargo watch -x run"
goodbye = "echo Goodbye"
install = "cargo install --path . --force"

File renamed without changes.
9 changes: 9 additions & 0 deletions Backend/crates/driver/diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/db/schema.rs"
schema = "school_trips_driver"

[migrations_directory]
dir = "./migrations"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Your SQL goes here
CREATE SCHEMA IF NOT EXISTS school_trips_driver;
CREATE TABLE school_trips_driver.auth
(
id VARCHAR(255) PRIMARY KEY,
mobile_number VARCHAR NOT NULL,
country_code VARCHAR(10) NOT NULL,
token VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL,
otp VARCHAR(10) NOT NULL,
updated_at TIMESTAMP NOT NULL
);

ALTER TABLE school_trips_driver.auth ADD COLUMN token_expiry TIMESTAMP NOT NULL DEFAULT now();
ALTER TABLE school_trips_driver.auth ALTER COLUMN created_at SET DATA TYPE TIMESTAMPTZ;
ALTER TABLE school_trips_driver.auth ALTER COLUMN updated_at SET DATA TYPE TIMESTAMPTZ;
ALTER TABLE school_trips_driver.auth ALTER COLUMN token_expiry SET DATA TYPE TIMESTAMPTZ;

CREATE TYPE school_trips_driver.role_type AS ENUM ('PARENT', 'DRIVER');

CREATE TABLE school_trips_driver.users
( name Varchar(255),
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
session_token Varchar NOT NULL,
mobile_number Varchar NOT NULL,
role role_type NOT NULL,
id Varchar PRIMARY KEY
);

ALTER TABLE school_trips_driver.auth ADD COLUMN role role_type NOT NULL;

ALTER TABLE school_trips_driver.users DROP COLUMN session_token;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
CREATE TYPE school_trips_driver.role_type AS ENUM ('PARENT', 'DRIVER');
File renamed without changes.
File renamed without changes.
49 changes: 49 additions & 0 deletions Backend/crates/driver/src/db/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// @generated automatically by Diesel CLI.

pub mod school_trips_driver {
pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "role_type"))]
pub struct RoleType;
}

diesel::table! {
use diesel::sql_types::*;
use super::sql_types::RoleType;

school_trips_driver.auth (id) {
#[max_length = 255]
id -> Varchar,
mobile_number -> Varchar,
#[max_length = 10]
country_code -> Varchar,
token -> Varchar,
created_at -> Timestamptz,
#[max_length = 10]
otp -> Varchar,
updated_at -> Timestamptz,
token_expiry -> Timestamptz,
role -> RoleType,
}
}

diesel::table! {
use diesel::sql_types::*;
use super::sql_types::RoleType;

school_trips_driver.users (id) {
#[max_length = 255]
name -> Nullable<Varchar>,
created_at -> Timestamptz,
updated_at -> Timestamptz,
mobile_number -> Varchar,
role -> RoleType,
id -> Varchar,
}
}

diesel::allow_tables_to_appear_in_same_query!(
auth,
users,
);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::storage::models;
use crate::storage::models::auth::{Auth, AuthResponse};
use crate::storage::result::QueryResult;
use crate::tools;
use crate::tools::utils::get_current_time;
// use crate::tools::utils::get_current_time;
use crate::ServerState;
use actix_web::web::{self};
use actix_web::HttpResponse;
use base64::{engine::general_purpose::STANDARD, Engine as _};
use common::tools::utils::{get_current_time, get_exipry_from_minutes};

pub fn handle_auth(
state: web::Data<ServerState>,
Expand All @@ -21,7 +21,7 @@ pub fn handle_auth(
let updated_auth = Auth {
token: id.clone(),
updated_at: get_current_time(),
token_expiry: tools::utils::get_exipry_from_minutes(state.config.auth_timeout),
token_expiry: get_exipry_from_minutes(state.config.auth_timeout),
..existing_user
};
updated_auth.update(&state.data.pool);
Expand All @@ -38,7 +38,7 @@ pub fn handle_auth(
country_code: req.country_code.clone(),
otp: state.config.default_otp.clone(),
role: req.role.to_owned(),
token_expiry: tools::utils::get_exipry_from_minutes(state.config.auth_timeout),
token_expiry: get_exipry_from_minutes(state.config.auth_timeout),
};
match auth.insert(&state.data.pool) {
QueryResult::Success => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use crate::storage::models;
use crate::storage::models::auth::{Auth, VerifyAuthResponse};
use crate::storage::models::user::User;
use crate::storage::result::QueryResult;
use crate::tools::contants::ONEWEEK;
use crate::tools::session_utils::SessionToken;
use crate::tools::utils::{get_current_time, get_exipry_from_minutes, is_expired};
use crate::ServerState;
use actix_web::web::{self};
use actix_web::HttpResponse;
use common::tools::contants::ONEWEEK;
use common::tools::session_utils::SessionToken;
use common::tools::utils::{get_current_time, get_exipry_from_minutes, is_expired};

pub fn handle_verify_auth(
state: web::Data<ServerState>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
storage::models::geometry::ServiceabilityRequest,
tools::polygon_utils::geo_json::get_available_shape_files, web, HttpResponse, ServerState,
common::tools::polygon_utils::geo_json::get_available_shape_files, web, HttpResponse, ServerState,
};
use geo::{coord, Intersects};

Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions Backend/src/main.rs → Backend/crates/driver/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate diesel;
extern crate diesel_migrations;
extern crate common;
use actix_web::{web, App, HttpResponse, HttpServer, Result};
use serde;
use types::ServerState;
mod db;
mod handlers;
mod routes;
mod storage;
mod tools;
mod transformers;
mod types;

Expand All @@ -28,7 +28,6 @@ async fn not_found() -> Result<HttpResponse> {
async fn main() -> std::io::Result<()> {
let server_state = ServerState::new(); // creating the server state
let app_data: web::Data<_> = web::Data::new(server_state);
env_logger::init();
HttpServer::new(
move || {
App::new()
Expand Down
File renamed without changes.
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actix_web::{get, http::header::AUTHORIZATION, web, HttpRequest, HttpResponse};

use crate::{
tools::{session_utils::SessionToken, utils::get_token_from_bearer},
common::tools::{session_utils::SessionToken, utils::get_token_from_bearer},
types::ServerState,
};

Expand Down
File renamed without changes.
Loading
Loading