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 serde::{Deserialize, Serialize};
4use sqlx::FromRow;
5
6#[derive(Debug, Serialize, Deserialize)]
7pub struct Claims {
8 pub sub: i32, // user id
9 pub exp: usize,
10}
11
12impl Claims {
13 pub fn new(user_id: i32) -> Self {
14 let exp = SystemTime::now()
15 .duration_since(UNIX_EPOCH)
16 .unwrap()
17 .as_secs() as usize + 24 * 60 * 60; // 24 hours from now
18
19 Self {
20 sub: user_id,
21 exp,
22 }
23 }
24}
25
26#[derive(Deserialize)]
27pub struct CreateLink {
28 pub url: String,
29 pub source: Option<String>,
30 pub custom_code: Option<String>,
31}
32
33#[derive(Serialize, FromRow)]
34pub struct Link {
35 pub id: i32,
36 pub user_id: Option<i32>,
37 pub original_url: String,
38 pub short_code: String,
39 pub created_at: chrono::DateTime<chrono::Utc>,
40 pub clicks: i64,
41}
42
43#[derive(Deserialize)]
44pub struct LoginRequest {
45 pub email: String,
46 pub password: String,
47}
48
49#[derive(Deserialize)]
50pub struct RegisterRequest {
51 pub email: String,
52 pub password: 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}