From d3847471fbcaa8e77d1de4196f8726d7c3c901f4 Mon Sep 17 00:00:00 2001 From: waveringana Date: Sat, 25 Jan 2025 03:41:45 -0500 Subject: [PATCH] fixes to db stuff --- ...c1ac60eea3a5c37e34fc6a0cb6e151c325382.json | 35 +++++++++++++++++++ ...70b3360a3ac71e649b293efb88d92c3254068.json | 22 ++++++++++++ ...4f77769f54c6230e28f5b3dc719b869d9cb3f.json | 34 ++++++++++++++++++ Cargo.toml | 4 +++ .../20250125000000_init.sql | 11 ++++-- src/handlers.rs | 4 +-- src/lib.rs | 11 ++++++ src/main.rs | 25 ++++--------- 8 files changed, 123 insertions(+), 23 deletions(-) create mode 100644 .sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json create mode 100644 .sqlx/query-4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068.json create mode 100644 .sqlx/query-f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f.json rename src/migrations/2025125_initial.sql => migrations/20250125000000_init.sql (75%) create mode 100644 src/lib.rs diff --git a/.sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json b/.sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json new file mode 100644 index 0000000..4f0db8c --- /dev/null +++ b/.sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json @@ -0,0 +1,35 @@ +{ + "db_name": "PostgreSQL", + "query": "INSERT INTO users (email, password_hash) VALUES ($1, $2) RETURNING *", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "email", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "password_hash", + "type_info": "Text" + } + ], + "parameters": { + "Left": [ + "Varchar", + "Text" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382" +} diff --git a/.sqlx/query-4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068.json b/.sqlx/query-4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068.json new file mode 100644 index 0000000..f464c54 --- /dev/null +++ b/.sqlx/query-4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT id FROM users WHERE email = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int4" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + false + ] + }, + "hash": "4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068" +} diff --git a/.sqlx/query-f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f.json b/.sqlx/query-f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f.json new file mode 100644 index 0000000..058fe52 --- /dev/null +++ b/.sqlx/query-f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f.json @@ -0,0 +1,34 @@ +{ + "db_name": "PostgreSQL", + "query": "SELECT * FROM users WHERE email = $1", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "id", + "type_info": "Int4" + }, + { + "ordinal": 1, + "name": "email", + "type_info": "Varchar" + }, + { + "ordinal": 2, + "name": "password_hash", + "type_info": "Text" + } + ], + "parameters": { + "Left": [ + "Text" + ] + }, + "nullable": [ + false, + false, + false + ] + }, + "hash": "f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f" +} diff --git a/Cargo.toml b/Cargo.toml index 7b3a07e..0013b25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,10 @@ name = "SimpleLink" version = "0.1.0" edition = "2021" +[lib] +name = "simple_link" +path = "src/lib.rs" + [dependencies] jsonwebtoken = "9" actix-web = "4.4" diff --git a/src/migrations/2025125_initial.sql b/migrations/20250125000000_init.sql similarity index 75% rename from src/migrations/2025125_initial.sql rename to migrations/20250125000000_init.sql index 0662f15..2cde11c 100644 --- a/src/migrations/2025125_initial.sql +++ b/migrations/20250125000000_init.sql @@ -1,3 +1,10 @@ +-- Add Migration Version +CREATE TABLE IF NOT EXISTS _sqlx_migrations ( + version BIGINT PRIMARY KEY, + description TEXT NOT NULL, + installed_on TIMESTAMPTZ NOT NULL DEFAULT NOW() +); + -- Create users table CREATE TABLE users ( id SERIAL PRIMARY KEY, @@ -5,7 +12,7 @@ CREATE TABLE users ( password_hash TEXT NOT NULL ); --- Create links table with user_id from the start +-- Create links table CREATE TABLE links ( id SERIAL PRIMARY KEY, original_url TEXT NOT NULL, @@ -15,7 +22,7 @@ CREATE TABLE links ( user_id INTEGER REFERENCES users(id) ); --- Create clicks table for tracking +-- Create clicks table CREATE TABLE clicks ( id SERIAL PRIMARY KEY, link_id INTEGER REFERENCES links(id), diff --git a/src/handlers.rs b/src/handlers.rs index 4214a80..74c1444 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,10 +1,10 @@ use actix_web::{web, HttpResponse, Responder, HttpRequest}; -use jsonwebtoken::{encode, decode, Header, EncodingKey, DecodingKey, Validation, errors::Error as JwtError};use crate::{error::AppError, models::{AuthResponse, Claims, CreateLink, Link, LoginRequest, RegisterRequest, User, UserResponse}, AppState}; +use jsonwebtoken::{encode, Header, EncodingKey};use crate::{error::AppError, models::{AuthResponse, Claims, CreateLink, Link, LoginRequest, RegisterRequest, User, UserResponse}, AppState}; use regex::Regex; use argon2::{password_hash::{rand_core::OsRng, SaltString}, PasswordVerifier}; use lazy_static::lazy_static; use argon2::{Argon2, PasswordHash, PasswordHasher}; -use crate::auth::{AuthenticatedUser}; +use crate::auth::AuthenticatedUser; lazy_static! { static ref VALID_CODE_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_-]{1,32}$").unwrap(); diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..38bc5b3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,11 @@ +use sqlx::PgPool; + +pub mod auth; +pub mod error; +pub mod handlers; +pub mod models; + +#[derive(Clone)] +pub struct AppState { + pub db: PgPool, +} diff --git a/src/main.rs b/src/main.rs index bb7d97b..8a9e3bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,10 @@ use actix_web::{web, App, HttpServer}; use actix_cors::Cors; use anyhow::Result; -use sqlx::PgPool; +use sqlx::postgres::PgPoolOptions; +use simple_link::{AppState, handlers}; use tracing::info; -mod error; -mod handlers; -mod models; -mod auth; - -#[derive(Clone)] -pub struct AppState { - db: PgPool, -} - #[actix_web::main] async fn main() -> Result<()> { // Load environment variables from .env file @@ -26,9 +17,6 @@ async fn main() -> Result<()> { let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set"); // Create database connection pool - use sqlx::postgres::PgPoolOptions; - - // In main(), replace the PgPool::connect with: let pool = PgPoolOptions::new() .max_connections(5) .acquire_timeout(std::time::Duration::from_secs(3)) @@ -36,7 +24,7 @@ async fn main() -> Result<()> { .await?; // Run database migrations - //sqlx::migrate!("./migrations").run(&pool).await?; + sqlx::migrate!("./migrations").run(&pool).await?; let state = AppState { db: pool }; @@ -58,16 +46,15 @@ async fn main() -> Result<()> { .route("/shorten", web::post().to(handlers::create_short_url)) .route("/links", web::get().to(handlers::get_all_links)) .route("/auth/register", web::post().to(handlers::register)) - .route("/auth/login", web::post().to(handlers::login)), - + .route("/auth/login", web::post().to(handlers::login)) + .route("/health", web::get().to(handlers::health_check)), ) .service( web::resource("/{short_code}") .route(web::get().to(handlers::redirect_to_url)) ) - .service(web::resource("/{short_code}").route(web::get().to(handlers::redirect_to_url))) }) - .workers(2) // Limit worker threads + .workers(2) .backlog(10_000) .bind("127.0.0.1:8080")? .run()