A very performant and light (2mb in memory) link shortener and tracker. Written in Rust and React and uses Postgres/SQLite.
1use std::time::{SystemTime, UNIX_EPOCH}; 2 3use chrono::NaiveDate; 4use serde::{Deserialize, Serialize}; 5use sqlx::FromRow; 6 7#[derive(Debug, Serialize, Deserialize)] 8pub struct Claims { 9 pub sub: i32, // user id 10 pub exp: usize, 11} 12 13impl Claims { 14 pub fn new(user_id: i32) -> Self { 15 let exp = SystemTime::now() 16 .duration_since(UNIX_EPOCH) 17 .unwrap() 18 .as_secs() as usize 19 + 24 * 60 * 60; // 24 hours from now 20 21 Self { sub: user_id, exp } 22 } 23} 24 25#[derive(Deserialize)] 26pub struct CreateLink { 27 pub url: String, 28 pub source: Option<String>, 29 pub custom_code: Option<String>, 30} 31 32#[derive(Serialize, FromRow)] 33pub struct Link { 34 pub id: i32, 35 pub user_id: Option<i32>, 36 pub original_url: String, 37 pub short_code: String, 38 pub created_at: chrono::DateTime<chrono::Utc>, 39 pub clicks: i64, 40} 41 42#[derive(Deserialize)] 43pub struct LoginRequest { 44 pub email: String, 45 pub password: String, 46} 47 48#[derive(Deserialize)] 49pub struct RegisterRequest { 50 pub email: String, 51 pub password: String, 52 pub admin_token: Option<String>, 53} 54 55#[derive(Serialize)] 56pub struct AuthResponse { 57 pub token: String, 58 pub user: UserResponse, 59} 60 61#[derive(Serialize)] 62pub struct UserResponse { 63 pub id: i32, 64 pub email: String, 65} 66 67#[derive(FromRow)] 68pub struct User { 69 pub id: i32, 70 pub email: String, 71 pub password_hash: String, 72} 73 74#[derive(sqlx::FromRow, Serialize)] 75pub struct ClickStats { 76 pub date: NaiveDate, 77 pub clicks: i64, 78} 79 80#[derive(sqlx::FromRow, Serialize)] 81pub struct SourceStats { 82 pub source: String, 83 pub count: i64, 84}