A very performant and light (2mb in memory) link shortener and tracker. Written in Rust and React and uses Postgres/SQLite.

fixes to db stuff

+35
.sqlx/query-2adc9fa303079a3e9c28bbf0565c1ac60eea3a5c37e34fc6a0cb6e151c325382.json
···
+
{
+
"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
···
+
{
+
"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
···
+
{
+
"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"
+
}
+4
Cargo.toml
···
version = "0.1.0"
edition = "2021"
+
[lib]
+
name = "simple_link"
+
path = "src/lib.rs"
+
[dependencies]
jsonwebtoken = "9"
actix-web = "4.4"
+2 -2
src/handlers.rs
···
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
···
+
use sqlx::PgPool;
+
+
pub mod auth;
+
pub mod error;
+
pub mod handlers;
+
pub mod models;
+
+
#[derive(Clone)]
+
pub struct AppState {
+
pub db: PgPool,
+
}
+6 -19
src/main.rs
···
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<()> {
···
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))
···
.await?;
// Run database migrations
-
//sqlx::migrate!("./migrations").run(&pool).await?;
+
sqlx::migrate!("./migrations").run(&pool).await?;
let state = AppState { db: pool };
···
.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()
+9 -2
src/migrations/2025125_initial.sql migrations/20250125000000_init.sql
···
+
-- 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,
···
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,
···
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),