cool-spotify-blend/server/src/main.rs

85 lines
2.4 KiB
Rust

mod spotify_auth;
mod spotify_get_top;
mod spotify_playlist;
mod spotify_types;
mod sql_users;
mod sql_tracks;
mod state;
mod joint_err;
mod refresh;
use spotify_auth::callback_manager;
use refresh::refresh_manager;
use actix_web::{App, HttpServer, HttpResponse, get, Responder, web};
use dotenvy::dotenv;
use std::env;
const BASE_URL: &'static str = "https://api.spotify.com/v1";
#[get("/")]
async fn root(data: web::Data<state::AppState>) -> impl Responder {
log::warn!("Access to root");
HttpResponse::Ok().body(format!("hi, pls go to: {}\n k thx", data.auth_url))
}
#[get("/failed")]
async fn failed() -> impl Responder {
HttpResponse::Ok().body("yeah sorry bud, something went wrong.")
}
#[get("/cool")]
async fn succeeded() -> impl Responder {
HttpResponse::Ok().body("whatever you were doing worked. bye")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::builder()
.target(env_logger::Target::Stdout)
.filter_level(log::LevelFilter::Warn)
.init();
dotenv().ok();
let port: u16 = std::env::var("PORT")
.expect("Cannot proceed without port")
.parse().expect("Port must be a number");
let address = std::env::var("ADDRESS").expect("Cannot proceed without a port to listen to");
let db_url = env::var("DATABASE_URL").expect("Cannot proceed without database url");
let pool = sqlx::mysql::MySqlPoolOptions::new()
.max_connections(5)
.connect(&db_url)
.await
.expect("Cannot proceed with failed database connection");
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(state::AppState {
auth_url: construct_auth_url(),
pool: pool.clone(),
}))
.service(root)
.service(callback_manager)
.service(succeeded)
.service(failed)
.service(refresh_manager)
})
.bind((address, port))?
.run()
.await
}
fn construct_auth_url() -> String {
let redirect_url = env::var("REDIRECT_URL").expect("Cannot proceed without redirect url");
let client_id = env::var("CLIENT_ID").expect("Cannot proceed without client id");
format!("https://accounts.spotify.com/authorize?client_id={}&scope={}&redirect_uri={}&response_type={}",
client_id,
"user-top-read playlist-modify-public user-read-email",
redirect_url,
"code")
}