A decentralized music tracking and discovery platform built on AT Protocol 馃幍
rocksky.app
spotify
atproto
lastfm
musicbrainz
scrobbling
listenbrainz
1use std::{env, sync::Arc};
2
3use actix_web::{
4 get, post,
5 web::{self, Data},
6 App, HttpRequest, HttpResponse, HttpServer, Responder,
7};
8use anyhow::Error;
9use owo_colors::OwoColorize;
10use serde_json::json;
11use sqlx::{postgres::PgPoolOptions, Pool, Postgres};
12
13use crate::handlers::handle;
14
15#[get("/")]
16async fn index(_req: HttpRequest) -> HttpResponse {
17 HttpResponse::Ok().json(json!({
18 "server": "Rocksky Dropbox Server",
19 "version": "0.1.0",
20 }))
21}
22
23#[post("/{method}")]
24async fn call_method(
25 data: web::Data<Arc<Pool<Postgres>>>,
26 mut payload: web::Payload,
27 req: HttpRequest,
28) -> Result<impl Responder, actix_web::Error> {
29 let method = req.match_info().get("method").unwrap_or("unknown");
30 tracing::info!(method = %method.bright_green(), "API call");
31
32 let conn = data.get_ref().clone();
33 handle(method, &mut payload, &req, conn)
34 .await
35 .map_err(actix_web::error::ErrorInternalServerError)
36}
37
38pub async fn serve() -> Result<(), Error> {
39 let host = env::var("DROPBOX_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
40 let port = env::var("DROPBOX_PORT").unwrap_or_else(|_| "7881".to_string());
41 let addr = format!("{}:{}", host, port);
42
43 let url = format!("http://{}", addr);
44 tracing::info!(url = %url.bright_green(), "Listening on");
45
46 let pool = PgPoolOptions::new()
47 .max_connections(5)
48 .connect(&env::var("XATA_POSTGRES_URL")?)
49 .await?;
50 let conn = Arc::new(pool);
51
52 let conn = conn.clone();
53 HttpServer::new(move || {
54 App::new()
55 .app_data(Data::new(conn.clone()))
56 .service(index)
57 .service(call_method)
58 })
59 .bind(&addr)?
60 .run()
61 .await
62 .map_err(Error::new)?;
63
64 Ok(())
65}