A decentralized music tracking and discovery platform built on AT Protocol 馃幍
at setup-tracing 75 lines 2.0 kB view raw
1use std::{env, sync::Arc, time::Duration}; 2 3use actix_limitation::{Limiter, RateLimiter}; 4use actix_session::SessionExt; 5use actix_web::{ 6 dev::ServiceRequest, 7 web::{self, Data}, 8 App, HttpServer, 9}; 10use anyhow::Error; 11use owo_colors::OwoColorize; 12use sqlx::postgres::PgPoolOptions; 13 14use crate::{cache::Cache, consts::BANNER}; 15 16pub mod auth; 17pub mod cache; 18pub mod consts; 19pub mod crypto; 20pub mod handlers; 21pub mod musicbrainz; 22pub mod repo; 23pub mod rocksky; 24pub mod scrobbler; 25pub mod spotify; 26pub mod types; 27pub mod xata; 28 29pub async fn start_server() -> Result<(), Error> { 30 println!("{}", BANNER.magenta()); 31 32 let cache = Cache::new()?; 33 34 let pool = PgPoolOptions::new() 35 .max_connections(5) 36 .connect(&env::var("XATA_POSTGRES_URL")?) 37 .await?; 38 39 let conn = Arc::new(pool); 40 41 let host = env::var("WEBSCROBBLER_HOST").unwrap_or_else(|_| "127.0.0.1".to_string()); 42 let port = env::var("WEBSCROBBLER_PORT") 43 .unwrap_or_else(|_| "7883".to_string()) 44 .parse::<u16>() 45 .unwrap_or(7883); 46 47 tracing::info!(url = %format!("http://{}:{}", host, port).bright_green(), "Starting WebScrobbler server @"); 48 49 let limiter = web::Data::new( 50 Limiter::builder("redis://127.0.0.1") 51 .key_by(|req: &ServiceRequest| { 52 req.get_session() 53 .get(&"session-id") 54 .unwrap_or_else(|_| req.cookie(&"rate-api-id").map(|c| c.to_string())) 55 }) 56 .limit(100) 57 .period(Duration::from_secs(60)) // 60 minutes 58 .build() 59 .unwrap(), 60 ); 61 62 HttpServer::new(move || { 63 App::new() 64 .wrap(RateLimiter::default()) 65 .app_data(limiter.clone()) 66 .app_data(Data::new(conn.clone())) 67 .app_data(Data::new(cache.clone())) 68 .service(handlers::index) 69 .service(handlers::handle_scrobble) 70 }) 71 .bind((host, port))? 72 .run() 73 .await?; 74 Ok(()) 75}