fixes to db stuff
This commit is contained in:
parent
9a43049978
commit
d3847471fb
8 changed files with 123 additions and 23 deletions
35
.sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json
generated
Normal file
35
.sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json
generated
Normal file
|
@ -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"
|
||||||
|
}
|
22
.sqlx/query-4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068.json
generated
Normal file
22
.sqlx/query-4560c237741ce9d4166aecd669770b3360a3ac71e649b293efb88d92c3254068.json
generated
Normal file
|
@ -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"
|
||||||
|
}
|
34
.sqlx/query-f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f.json
generated
Normal file
34
.sqlx/query-f3f58600e971f1be6cbe206bba24f77769f54c6230e28f5b3dc719b869d9cb3f.json
generated
Normal file
|
@ -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"
|
||||||
|
}
|
|
@ -3,6 +3,10 @@ name = "SimpleLink"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
name = "simple_link"
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
jsonwebtoken = "9"
|
jsonwebtoken = "9"
|
||||||
actix-web = "4.4"
|
actix-web = "4.4"
|
||||||
|
|
|
@ -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 users table
|
||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
|
@ -5,7 +12,7 @@ CREATE TABLE users (
|
||||||
password_hash TEXT NOT NULL
|
password_hash TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Create links table with user_id from the start
|
-- Create links table
|
||||||
CREATE TABLE links (
|
CREATE TABLE links (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
original_url TEXT NOT NULL,
|
original_url TEXT NOT NULL,
|
||||||
|
@ -15,7 +22,7 @@ CREATE TABLE links (
|
||||||
user_id INTEGER REFERENCES users(id)
|
user_id INTEGER REFERENCES users(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Create clicks table for tracking
|
-- Create clicks table
|
||||||
CREATE TABLE clicks (
|
CREATE TABLE clicks (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
link_id INTEGER REFERENCES links(id),
|
link_id INTEGER REFERENCES links(id),
|
|
@ -1,10 +1,10 @@
|
||||||
use actix_web::{web, HttpResponse, Responder, HttpRequest};
|
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 regex::Regex;
|
||||||
use argon2::{password_hash::{rand_core::OsRng, SaltString}, PasswordVerifier};
|
use argon2::{password_hash::{rand_core::OsRng, SaltString}, PasswordVerifier};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use argon2::{Argon2, PasswordHash, PasswordHasher};
|
use argon2::{Argon2, PasswordHash, PasswordHasher};
|
||||||
use crate::auth::{AuthenticatedUser};
|
use crate::auth::AuthenticatedUser;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref VALID_CODE_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_-]{1,32}$").unwrap();
|
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_web::{web, App, HttpServer};
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use sqlx::PgPool;
|
use sqlx::postgres::PgPoolOptions;
|
||||||
|
use simple_link::{AppState, handlers};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
mod error;
|
|
||||||
mod handlers;
|
|
||||||
mod models;
|
|
||||||
mod auth;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct AppState {
|
|
||||||
db: PgPool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Load environment variables from .env file
|
// 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");
|
let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
|
||||||
|
|
||||||
// Create database connection pool
|
// Create database connection pool
|
||||||
use sqlx::postgres::PgPoolOptions;
|
|
||||||
|
|
||||||
// In main(), replace the PgPool::connect with:
|
|
||||||
let pool = PgPoolOptions::new()
|
let pool = PgPoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
.acquire_timeout(std::time::Duration::from_secs(3))
|
.acquire_timeout(std::time::Duration::from_secs(3))
|
||||||
|
@ -36,7 +24,7 @@ async fn main() -> Result<()> {
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Run database migrations
|
// Run database migrations
|
||||||
//sqlx::migrate!("./migrations").run(&pool).await?;
|
sqlx::migrate!("./migrations").run(&pool).await?;
|
||||||
|
|
||||||
let state = AppState { db: pool };
|
let state = AppState { db: pool };
|
||||||
|
|
||||||
|
@ -58,16 +46,15 @@ async fn main() -> Result<()> {
|
||||||
.route("/shorten", web::post().to(handlers::create_short_url))
|
.route("/shorten", web::post().to(handlers::create_short_url))
|
||||||
.route("/links", web::get().to(handlers::get_all_links))
|
.route("/links", web::get().to(handlers::get_all_links))
|
||||||
.route("/auth/register", web::post().to(handlers::register))
|
.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(
|
.service(
|
||||||
web::resource("/{short_code}")
|
web::resource("/{short_code}")
|
||||||
.route(web::get().to(handlers::redirect_to_url))
|
.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)
|
.backlog(10_000)
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.run()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue