some updates

This commit is contained in:
Jonas_Jones 2023-12-13 04:19:04 +01:00
parent b852500513
commit f93e034373
12 changed files with 265 additions and 20 deletions

84
src/config/mod.rs Normal file
View file

@ -0,0 +1,84 @@
use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use serde::{Deserialize, Serialize};
use crate::Logger;
#[derive(Debug, Deserialize, Serialize)]
pub struct ServerConfig {
host: String,
port: u32,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct LastFMConfig {
api_key: String,
api_secret: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct DatabaseConfig {
host: String,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
server: ServerConfig,
lastfm: LastFMConfig,
database: DatabaseConfig,
}
pub fn load_config() -> (ServerConfig, LastFMConfig, DatabaseConfig) {
// load config.toml. create new one if it doesn't exist
// return config
let config_path = "config.toml";
if !std::path::Path::new(config_path).exists() {
// If it doesn't exist, create a new config file with default values
create_default_config(config_path);
}
// Read the configuration file
let mut config_file = File::open("config.toml").expect({Logger::panic("Failed to open config file"); std::process::exit(1)});
let mut config_toml = String::new();
config_file
.read_to_string(&mut config_toml)
.expect("Failed to read config file");
// Deserialize the TOML into the AppConfig struct
let config: Config = toml::from_str(&config_toml).expect({Logger::panic("Failed to deserialize config file"); std::process::exit(1)});
// Return the config
return (config.server, config.lastfm, config.database);
}
fn create_default_config(path: &str) {
// Create default Config
let default_config = Config {
server: ServerConfig {
host: String::from("localhost"),
port: 8080,
},
lastfm: LastFMConfig {
api_key: String::from(""),
api_secret: String::from(""),
},
database: DatabaseConfig {
host: String::from(""),
},
};
// Serialize default config to TOML
let toml_string = toml::to_string_pretty(&default_config).expect("Failed to serialize config");
// Write the TOML string to the config file
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(path)
.expect("Failed to open config file for writing");
file.write_all(toml_string.as_bytes())
.expect("Failed to write to config file");
}

1
src/db/apps/mod.rs Normal file
View file

@ -0,0 +1 @@
pub fn main() {}

0
src/db/mod.rs Normal file
View file

0
src/db/users/mod.rs Normal file
View file

View file

@ -1,16 +1,5 @@
// pub fn help() -> warp::reply::Response {
// return warp::reply::Reply::with_header(
// warp::reply::html(""),
// "Content-Type",
// "text/html",
// );
// }
// create a function help() that returns a response with a ststus code of 200 and a body of "help"
use warp::Filter;
pub fn get_builtin_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path("v1")
.and((warp::path("help").map(|| "help"))

View file

@ -1,3 +1,59 @@
pub fn main() {
}
use std::convert::Infallible;
use warp::Filter;
use reqwest::Error;
pub fn get_kcomebacks_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path("v1").and(warp::path("kcomebacks"))
// - /v1/kcomebacks/last_update
// - /v1/kcomebacks/start_update with token
// - /v1/kcomebacks/upcoming/today?limit={0-50}&offset={n}
// - /v1/kcomebacks/upcoming/week?limit={0-50}&offset={n}
// - /v1/kcomebacks/upcoming/month?limit={0-50}&offset={n}
// - /v1/kcomebacks/filter/id?id={n}
// - /v1/kcomebacks/filter/daterange?start={date: YYYY-MM-DD}&end={date: YYYY-MM-DD}&limit={0-50}&offset={n}
// - /v1/kcomebacks/filter/artist?artist={artist}&limit={0-50}&offset={n}
// - /v1/kcomebacks/filter/first
// - /v1/kcomebacks/filter/last
// - /v1/kcomebacks/filter/title?title={title}&limit={0-50}&offset={n}
// - /v1/kcomebacks/filter/type?type={type}&limit={0-50}&offset={n}
// - /v1/kcomebacks/filter/gettypes
.and(warp::path("last_update").and(warp::get()).and_then(last_update_handler)
.or(warp::path("start_update").map(|| "Not implemented yet")))
}
// get json data from https://cdn.jonasjones.dev/api/kcomebacks/rkpop_data.json
async fn fetch_data() -> Result<serde_json::Value, Error> {
let url = "https://cdn.jonasjones.dev/api/kcomebacks/rkpop_data.json";
let response = reqwest::get(url).await?;
if response.status().is_success() {
// Parse the JSON response
let json_data: serde_json::Value = response.json().await?;
return Ok(json_data);
} else {
// Handle non-successful status codes
Err(response.error_for_status().unwrap_err())
}
}
async fn last_update_handler() -> Result<impl warp::Reply, warp::Rejection> {
match last_update().await {
Ok(last_update_value) => Ok(warp::reply::json(&last_update_value)),
Err(_) => {
#[derive(Debug)]
struct InternalServerError;
impl warp::reject::Reject for InternalServerError {}
Err(warp::reject::custom(InternalServerError))
}
}
}
async fn last_update() -> Result<serde_json::Value, Error> {
// get the value of last_update of the first element of the json that fetch_data() returns
let last_update_value = fetch_data().await?.get(0).unwrap().get("last_update").unwrap().clone();
return Ok(last_update_value);
}

View file

@ -0,0 +1,8 @@
pub fn get_kcomebacks_upcoming_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
warp::path("upcoming")
.and(warp::path("today").and(warp::get()).and_then(upcoming_today_handler))
.or(warp::path("week").and(warp::get()).and_then(upcoming_week_handler))
.or(warp::path("month").and(warp::get()).and_then(upcoming_month_handler))
}

View file

@ -1,7 +1,12 @@
mod builtin;
mod kcomebacks;
pub use builtin::get_builtin_routes as get_v1_builtin_routes;
pub use kcomebacks::get_kcomebacks_routes as get_v1_kcomebacks_routes;
use warp::Filter;
pub fn get_v1_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
return get_v1_builtin_routes();
return get_v1_builtin_routes()
.or(get_v1_kcomebacks_routes());
}