Revert "almost production ready"

This reverts commit 937b3fc811.
This commit is contained in:
WaveringAna 2025-01-26 01:45:50 -05:00
parent 937b3fc811
commit f3a61bfa99
21 changed files with 55 additions and 1071 deletions

View file

@ -2,8 +2,7 @@ use crate::auth::AuthenticatedUser;
use crate::{
error::AppError,
models::{
AuthResponse, Claims, ClickStats, CreateLink, Link, LoginRequest, RegisterRequest,
SourceStats, User, UserResponse,
AuthResponse, Claims, CreateLink, Link, LoginRequest, RegisterRequest, User, UserResponse,
},
AppState,
};
@ -306,85 +305,3 @@ pub async fn delete_link(
Ok(HttpResponse::NoContent().finish())
}
pub async fn get_link_clicks(
state: web::Data<AppState>,
user: AuthenticatedUser,
path: web::Path<i32>,
) -> Result<impl Responder, AppError> {
let link_id = path.into_inner();
// Verify the link belongs to the user
let link = sqlx::query!(
"SELECT id FROM links WHERE id = $1 AND user_id = $2",
link_id,
user.user_id
)
.fetch_optional(&state.db)
.await?;
if link.is_none() {
return Err(AppError::NotFound);
}
let clicks = sqlx::query_as!(
ClickStats,
r#"
SELECT
DATE(created_at)::date as "date!",
COUNT(*)::bigint as "clicks!"
FROM clicks
WHERE link_id = $1
GROUP BY DATE(created_at)
ORDER BY DATE(created_at) ASC -- Changed from DESC to ASC
LIMIT 30
"#,
link_id
)
.fetch_all(&state.db)
.await?;
Ok(HttpResponse::Ok().json(clicks))
}
pub async fn get_link_sources(
state: web::Data<AppState>,
user: AuthenticatedUser,
path: web::Path<i32>,
) -> Result<impl Responder, AppError> {
let link_id = path.into_inner();
// Verify the link belongs to the user
let link = sqlx::query!(
"SELECT id FROM links WHERE id = $1 AND user_id = $2",
link_id,
user.user_id
)
.fetch_optional(&state.db)
.await?;
if link.is_none() {
return Err(AppError::NotFound);
}
let sources = sqlx::query_as!(
SourceStats,
r#"
SELECT
query_source as "source!",
COUNT(*)::bigint as "count!"
FROM clicks
WHERE link_id = $1
AND query_source IS NOT NULL
AND query_source != ''
GROUP BY query_source
ORDER BY COUNT(*) DESC
LIMIT 10
"#,
link_id
)
.fetch_all(&state.db)
.await?;
Ok(HttpResponse::Ok().json(sources))
}

View file

@ -1,15 +1,10 @@
use actix_cors::Cors;
use actix_files::Files;
use actix_web::{middleware::DefaultHeaders, web, App, HttpServer};
use actix_web::{web, App, HttpServer};
use anyhow::Result;
use simplelink::{handlers, AppState};
use simple_link::{handlers, AppState};
use sqlx::postgres::PgPoolOptions;
use tracing::info;
async fn index() -> Result<actix_files::NamedFile, actix_web::Error> {
Ok(actix_files::NamedFile::open("./static/index.html")?)
}
#[actix_web::main]
async fn main() -> Result<()> {
// Load environment variables from .env file
@ -31,10 +26,10 @@ async fn main() -> Result<()> {
// Run database migrations
sqlx::migrate!("./migrations").run(&pool).await?;
let _state = AppState { db: pool };
let state = AppState { db: pool };
let host = std::env::var("SERVER_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
let port = std::env::var("SERVER_PORT").unwrap_or_else(|_| "3000".to_string());
let port = std::env::var("SERVER_PORT").unwrap_or_else(|_| "8080".to_string());
info!("Starting server at http://{}:{}", host, port);
// Start HTTP server
@ -47,31 +42,20 @@ async fn main() -> Result<()> {
App::new()
.wrap(cors)
// Add headers to help with caching static assets
.wrap(DefaultHeaders::new().add(("Cache-Control", "max-age=31536000")))
// API routes
.app_data(web::Data::new(state.clone()))
.service(
web::scope("/api")
.route("/shorten", web::post().to(handlers::create_short_url))
.route("/links", web::get().to(handlers::get_all_links))
.route("/links/{id}", web::delete().to(handlers::delete_link))
.route(
"/links/{id}/clicks",
web::get().to(handlers::get_link_clicks),
)
.route(
"/links/{id}/sources",
web::get().to(handlers::get_link_sources),
)
.route("/auth/register", web::post().to(handlers::register))
.route("/auth/login", web::post().to(handlers::login))
.route("/health", web::get().to(handlers::health_check)),
)
// Serve static files
.service(Files::new("/assets", "./static/assets"))
// Handle SPA routes - must be last
.default_service(web::get().to(index))
.service(web::resource("/{short_code}").route(web::get().to(handlers::redirect_to_url)))
})
.workers(2)
.backlog(10_000)
.bind(format!("{}:{}", host, port))?
.run()
.await?;

View file

@ -1,6 +1,5 @@
use std::time::{SystemTime, UNIX_EPOCH};
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
@ -15,10 +14,12 @@ impl Claims {
let exp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize
+ 24 * 60 * 60; // 24 hours from now
Self { sub: user_id, exp }
.as_secs() as usize + 24 * 60 * 60; // 24 hours from now
Self {
sub: user_id,
exp,
}
}
}
@ -69,15 +70,3 @@ pub struct User {
pub email: String,
pub password_hash: String,
}
#[derive(sqlx::FromRow, Serialize)]
pub struct ClickStats {
pub date: NaiveDate,
pub clicks: i64,
}
#[derive(sqlx::FromRow, Serialize)]
pub struct SourceStats {
pub source: String,
pub count: i64,
}