feat(api): add song CRUD routes, wire SongService into AppState
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
mod routes;
|
||||
|
||||
use axum::{Router, routing::post};
|
||||
use axum::{Router, routing::{get, post}};
|
||||
use common::SongService;
|
||||
use persistence::SqliteRepositoryFactory;
|
||||
use routes::songs::{create_song, delete_song, get_song, list_songs};
|
||||
use routes::tabs::{AppState, parse_tab};
|
||||
use std::sync::Arc;
|
||||
use tower_http::cors::{Any, CorsLayer};
|
||||
@@ -10,9 +13,17 @@ use ug_parser::{UgHtmlParser, UgTabFetcher};
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let database_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "sqlite://./pocket-chords.db".into());
|
||||
let repo = SqliteRepositoryFactory::create(&database_url)
|
||||
.await
|
||||
.expect("failed to connect to database");
|
||||
let songs = SongService::new(Box::new(repo));
|
||||
|
||||
let state = Arc::new(AppState {
|
||||
fetcher: Box::new(UgTabFetcher::new()),
|
||||
parser: Box::new(UgHtmlParser),
|
||||
songs,
|
||||
});
|
||||
|
||||
let cors = CorsLayer::new()
|
||||
@@ -22,6 +33,8 @@ async fn main() {
|
||||
|
||||
let app = Router::new()
|
||||
.route("/tabs/parse", post(parse_tab))
|
||||
.route("/songs", post(create_song).get(list_songs))
|
||||
.route("/songs/{id}", get(get_song).delete(delete_song))
|
||||
.layer(cors)
|
||||
.with_state(state);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user