fixes to db stuff
This commit is contained in:
parent
9a43049978
commit
d3847471fb
8 changed files with 123 additions and 23 deletions
|
@ -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();
|
||||
|
|
11
src/lib.rs
Normal file
11
src/lib.rs
Normal file
|
@ -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,
|
||||
}
|
25
src/main.rs
25
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()
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
-- Create users table
|
||||
CREATE TABLE users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
email VARCHAR(255) NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- Create links table with user_id from the start
|
||||
CREATE TABLE links (
|
||||
id SERIAL PRIMARY KEY,
|
||||
original_url TEXT NOT NULL,
|
||||
short_code VARCHAR(8) NOT NULL UNIQUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
clicks BIGINT NOT NULL DEFAULT 0,
|
||||
user_id INTEGER REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- Create clicks table for tracking
|
||||
CREATE TABLE clicks (
|
||||
id SERIAL PRIMARY KEY,
|
||||
link_id INTEGER REFERENCES links(id),
|
||||
source TEXT,
|
||||
query_source TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Create indexes
|
||||
CREATE INDEX idx_short_code ON links(short_code);
|
||||
CREATE INDEX idx_user_id ON links(user_id);
|
||||
CREATE INDEX idx_link_id ON clicks(link_id);
|
Loading…
Add table
Add a link
Reference in a new issue