almost production ready
This commit is contained in:
parent
3932b48a64
commit
937b3fc811
21 changed files with 1071 additions and 55 deletions
32
src/main.rs
32
src/main.rs
|
@ -1,10 +1,15 @@
|
|||
use actix_cors::Cors;
|
||||
use actix_web::{web, App, HttpServer};
|
||||
use actix_files::Files;
|
||||
use actix_web::{middleware::DefaultHeaders, web, App, HttpServer};
|
||||
use anyhow::Result;
|
||||
use simple_link::{handlers, AppState};
|
||||
use simplelink::{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
|
||||
|
@ -26,10 +31,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(|_| "8080".to_string());
|
||||
let port = std::env::var("SERVER_PORT").unwrap_or_else(|_| "3000".to_string());
|
||||
info!("Starting server at http://{}:{}", host, port);
|
||||
|
||||
// Start HTTP server
|
||||
|
@ -42,20 +47,31 @@ async fn main() -> Result<()> {
|
|||
|
||||
App::new()
|
||||
.wrap(cors)
|
||||
.app_data(web::Data::new(state.clone()))
|
||||
// Add headers to help with caching static assets
|
||||
.wrap(DefaultHeaders::new().add(("Cache-Control", "max-age=31536000")))
|
||||
// API routes
|
||||
.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)),
|
||||
)
|
||||
.service(web::resource("/{short_code}").route(web::get().to(handlers::redirect_to_url)))
|
||||
// Serve static files
|
||||
.service(Files::new("/assets", "./static/assets"))
|
||||
// Handle SPA routes - must be last
|
||||
.default_service(web::get().to(index))
|
||||
})
|
||||
.workers(2)
|
||||
.backlog(10_000)
|
||||
.bind(format!("{}:{}", host, port))?
|
||||
.run()
|
||||
.await?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue