diff --git a/.env b/.env new file mode 100644 index 0000000..4b4d1be --- /dev/null +++ b/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://admin:secret+123@localhost/rss diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..35a12ff --- /dev/null +++ b/diesel.toml @@ -0,0 +1,8 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" + +[migrations_directory] +dir = "migrations" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f2a6bb6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.7" +services: + postgres: + container_name: 'to-do-postgres' + image: 'postgres:latest' + restart: always + ports: + - '5432:5432' + environment: + - 'POSTGRES_USER=admin' + - 'POSTGRES_DB=to_do' + - 'POSTGRES_PASSWORD=secret+123' diff --git a/migrations/00000000000000_diesel_initial_setup/down.sql b/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/migrations/00000000000000_diesel_initial_setup/up.sql b/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; diff --git a/migrations/2022-11-21-174138_create_users/down.sql b/migrations/2022-11-21-174138_create_users/down.sql new file mode 100644 index 0000000..dc3714b --- /dev/null +++ b/migrations/2022-11-21-174138_create_users/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE users; diff --git a/migrations/2022-11-21-174138_create_users/up.sql b/migrations/2022-11-21-174138_create_users/up.sql new file mode 100644 index 0000000..6a6f896 --- /dev/null +++ b/migrations/2022-11-21-174138_create_users/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR NOT NULL, + email VARCHAR NOT NULL, + password VARCHAR NOT NULL, + unique_id VARCHAR NOT NULL, + UNIQUE (email), + UNIQUE (username) +) diff --git a/src/models/mod.rs b/src/models/mod.rs new file mode 100644 index 0000000..22d12a3 --- /dev/null +++ b/src/models/mod.rs @@ -0,0 +1 @@ +pub mod user; diff --git a/src/models/user/mod.rs b/src/models/user/mod.rs new file mode 100644 index 0000000..0590a75 --- /dev/null +++ b/src/models/user/mod.rs @@ -0,0 +1,3 @@ +pub mod new_user; +pub mod user; + diff --git a/src/models/user/new_user.rs b/src/models/user/new_user.rs new file mode 100644 index 0000000..74fc9b2 --- /dev/null +++ b/src/models/user/new_user.rs @@ -0,0 +1,29 @@ +extern crate bcrypt; + +use bcrypt::{hash, DEFAULT_COST}; +use diesel::Insertable; +use uuid::Uuid; + +use crate::schema::users; + +#[derive(Insertable, Clone)] +#[diesel(table_name=users)] +pub struct NewUser { + pub username: String, + pub email: String, + pub password: String, + pub unique_id: String, +} + +impl NewUser { + pub fn new(username: String, email: String, password: String) -> NewUser { + let hashed_password: String = hash(password.as_str(), DEFAULT_COST).unwrap(); + let uuid = Uuid::new_v4(); + NewUser { + username, + email, + password: hashed_password, + unique_id: uuid.to_string(), + } + } +} diff --git a/src/models/user/user.rs b/src/models/user/user.rs new file mode 100644 index 0000000..c893f66 --- /dev/null +++ b/src/models/user/user.rs @@ -0,0 +1,23 @@ +extern crate bcrypt; + +use bcrypt::verify; +use diesel::{Identifiable, Queryable}; + +use crate::schema::users; + +#[derive(Queryable, Identifiable)] +#[diesel(table_name=users)] +#[derive(Clone)] +pub struct User { + pub id: i32, + pub username: String, + pub email: String, + pub password: String, + pub unique_id: String, +} + +impl User { + pub fn verify(self, password: String) -> bool { + return verify(password.as_str(), &self.password).unwrap(); + } +} diff --git a/src/reader/feeds.rs b/src/reader/feeds.rs new file mode 100644 index 0000000..92c402d --- /dev/null +++ b/src/reader/feeds.rs @@ -0,0 +1,9 @@ +use std::error::Error; + +use rss::Channel; + +pub async fn get_feed(feed: &String) -> Result> { + let content = reqwest::get(feed).await?.bytes().await?; + let channel = Channel::read_from(&content[..])?; + Ok(channel) +} diff --git a/src/reader/mod.rs b/src/reader/mod.rs new file mode 100644 index 0000000..abf705f --- /dev/null +++ b/src/reader/mod.rs @@ -0,0 +1 @@ +pub mod feeds; diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..f242c0a --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,11 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + users (id) { + id -> Int4, + username -> Varchar, + email -> Varchar, + password -> Varchar, + unique_id -> Varchar, + } +} diff --git a/src/views/mod.rs b/src/views/mod.rs new file mode 100644 index 0000000..e6e325e --- /dev/null +++ b/src/views/mod.rs @@ -0,0 +1,8 @@ +use actix_web::web; + +pub fn views_factory(app: &mut web::ServiceConfig) { + // auth::auth_factory(app); + // to_do::item_factory(app); + // app::app_factory(app); + // users::user_factory(app); +} diff --git a/src/views/path.rs b/src/views/path.rs new file mode 100644 index 0000000..0def744 --- /dev/null +++ b/src/views/path.rs @@ -0,0 +1,16 @@ +pub struct Path { + pub prefix: String, + pub backend: bool, +} + +impl Path { + pub fn define(&self, following_path: String) -> String { + match self.backend { + true => { + let path: String = self.prefix.to_owned() + &following_path; + return String::from("/api/v1") + &path; + } + false => self.prefix.to_owned() + &following_path, + } + } +}