forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
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;
11
12use crate::handlers::handle;
13
14#[get("/")]
15async fn index(_req: HttpRequest) -> HttpResponse {
16 HttpResponse::Ok().json(json!({
17 "server": "Rocksky Tracklist Server",
18 "version": "0.1.0",
19 }))
20}
21
22#[post("/{method}")]
23async fn call_method(
24 data: web::Data<Arc<redis::Client>>,
25 mut payload: web::Payload,
26 req: HttpRequest,
27) -> Result<impl Responder, actix_web::Error> {
28 let method = req.match_info().get("method").unwrap_or("unknown");
29 println!("Method: {}", method.bright_green());
30
31 let conn = data.get_ref().clone();
32 handle(method, &mut payload, &req, conn)
33 .await
34 .map_err(actix_web::error::ErrorInternalServerError)
35}
36
37pub async fn run() -> Result<(), Error> {
38 let host = env::var("TRACKLIST_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
39 let port = env::var("TRACKLIST_PORT").unwrap_or_else(|_| "7884".to_string());
40 let addr = format!("{}:{}", host, port);
41
42 let url = format!("http://{}", addr);
43 println!("Listening on {}", url.bright_green());
44
45 let client = redis::Client::open(env::var("REDIS_URL").unwrap_or("redis://127.0.0.1".into()))?;
46 let conn = Arc::new(client);
47
48 HttpServer::new(move || {
49 App::new()
50 .app_data(Data::new(conn.clone()))
51 .service(index)
52 .service(call_method)
53 })
54 .bind(&addr)?
55 .run()
56 .await
57 .map_err(Error::new)?;
58
59 Ok(())
60}