summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml2
-rw-r--r--src/api.rs23
-rw-r--r--src/api/album.rs (renamed from src/routes/album.rs)2
-rw-r--r--src/api/artist.rs (renamed from src/routes/artist.rs)0
-rw-r--r--src/api/search_results.rs (renamed from src/routes/search_results.rs)2
-rw-r--r--src/api/song.rs (renamed from src/routes/song.rs)0
-rw-r--r--src/auth.rs53
-rw-r--r--src/main.rs24
-rw-r--r--src/routes.rs4
9 files changed, 89 insertions, 21 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6e80fa4..8022b51 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,6 +11,8 @@ tokio = "1.40.0"
dotenv = "0.15.0"
serde_json = "1.0.128"
env_logger = "0.11.6"
+jsonwebtoken = "9.3.0"
+chrono = "0.4.39"
[package.metadata.scripts]
db_create = "bash ./scripts/create_db.sh"
diff --git a/src/api.rs b/src/api.rs
new file mode 100644
index 0000000..1810f24
--- /dev/null
+++ b/src/api.rs
@@ -0,0 +1,23 @@
+use actix_web::{web, Scope};
+
+pub mod song;
+pub mod album;
+pub mod artist;
+pub mod search_results;
+
+pub fn api_scope() -> Scope {
+ web::scope("/api")
+ .service(song::get_song)
+ .service(song::post_song)
+ .service(song::put_song)
+ .service(song::delete_song)
+ .service(album::get_album)
+ .service(album::post_album)
+ .service(album::put_album)
+ .service(album::delete_album)
+ .service(artist::get_artist)
+ .service(artist::post_artist)
+ .service(artist::put_artist)
+ .service(artist::delete_artist)
+ .service(search_results::search_results)
+}
diff --git a/src/routes/album.rs b/src/api/album.rs
index ab82e2e..d9d0d52 100644
--- a/src/routes/album.rs
+++ b/src/api/album.rs
@@ -88,7 +88,7 @@ pub async fn delete_album(
}
match app_state.database.delete_album(id).await {
- Ok(_) => HttpResponse::Ok().body("Deletion succeeded\n"),
+ Ok(_) => HttpResponse::Ok().body("Deletion succeeded\n".to_owned()),
Err(e) => HttpResponse::Ok().body(format!("There was an issue in the request:\n{}", e)),
}
}
diff --git a/src/routes/artist.rs b/src/api/artist.rs
index 81ae773..81ae773 100644
--- a/src/routes/artist.rs
+++ b/src/api/artist.rs
diff --git a/src/routes/search_results.rs b/src/api/search_results.rs
index 3056aef..6456ff9 100644
--- a/src/routes/search_results.rs
+++ b/src/api/search_results.rs
@@ -9,7 +9,7 @@ struct SearchQueryOptions {
name: Option<String>,
}
-#[get("/searchResults")]
+#[get("/search-results")]
pub async fn search_results(
app_state: web::Data<AppState>,
get_args: web::Query<SearchQueryOptions>,
diff --git a/src/routes/song.rs b/src/api/song.rs
index 850c759..850c759 100644
--- a/src/routes/song.rs
+++ b/src/api/song.rs
diff --git a/src/auth.rs b/src/auth.rs
new file mode 100644
index 0000000..8bf29f7
--- /dev/null
+++ b/src/auth.rs
@@ -0,0 +1,53 @@
+use actix_web::{web, Scope, HttpResponse};
+use serde::{Serialize, Deserialize};
+use chrono::{Utc, Duration};
+use jsonwebtoken::{encode, EncodingKey, Header};
+
+use crate::AppState;
+
+pub fn auth_scope() -> Scope {
+ web::scope("/auth")
+ .route("/encode-token/{id}", web::get().to(encode_token))
+ .route("/decode-token", web::post().to(decode_token))
+ .route("/protected", web::get().to(protected))
+}
+
+#[derive(Serialize, Deserialize)]
+struct Claims{
+ id: usize,
+ exp: usize,
+}
+
+#[derive(Serialize, Deserialize)]
+struct Response{
+ message: String,
+}
+
+#[derive(Serialize, Deserialize)]
+struct EncodeResponse{
+ message: String,
+ token: String,
+}
+
+async fn encode_token(path: web::Path<usize>, data: web::Data<AppState>) -> HttpResponse {
+ let id: usize = path.into_inner();
+ let exp: usize = (Utc::now() + Duration::days(365)).timestamp() as usize;
+ let claims: Claims = Claims {id, exp};
+ let token: String = encode(
+ &Header::default(),
+ &claims,
+ &EncodingKey::from_secret(data.secret.as_str().as_ref()),
+ ).unwrap();
+ HttpResponse::Ok().json(EncodeResponse {
+ message: "success".to_owned(),
+ token: token.to_owned(),
+ })
+}
+
+async fn decode_token() -> HttpResponse {
+ HttpResponse::Ok().body("decode_token\n".to_owned())
+}
+
+async fn protected() -> HttpResponse {
+ HttpResponse::Ok().body("protected\n".to_owned())
+}
diff --git a/src/main.rs b/src/main.rs
index 0bd3087..8a64e0d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,6 @@
+mod api;
+mod auth;
mod database;
-mod routes;
mod structs;
use actix_web::{web, App, HttpServer};
@@ -9,6 +10,7 @@ use std::sync::Arc;
#[derive(Clone)]
struct AppState {
database: Arc<database::DatabaseWrapper>,
+ secret: String,
}
#[actix_web::main]
@@ -25,25 +27,17 @@ async fn main() -> std::io::Result<()> {
};
let db = Arc::new(db_raw);
- let app_state = AppState { database: db };
+ let app_state = AppState {
+ database: db,
+ secret: "secret".to_owned(),
+ };
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(app_state.clone()))
.route("/", web::get().to(root))
- .service(routes::song::get_song)
- .service(routes::song::post_song)
- .service(routes::song::put_song)
- .service(routes::song::delete_song)
- .service(routes::album::get_album)
- .service(routes::album::post_album)
- .service(routes::album::put_album)
- .service(routes::album::delete_album)
- .service(routes::artist::get_artist)
- .service(routes::artist::post_artist)
- .service(routes::artist::put_artist)
- .service(routes::artist::delete_artist)
- .service(routes::search_results::search_results)
+ .service(api::api_scope())
+ .service(auth::auth_scope())
})
.bind(("127.0.0.1", 8000))?
.run()
diff --git a/src/routes.rs b/src/routes.rs
deleted file mode 100644
index 0cefd1b..0000000
--- a/src/routes.rs
+++ /dev/null
@@ -1,4 +0,0 @@
-pub mod song;
-pub mod album;
-pub mod artist;
-pub mod search_results;