Skip to content

Commit

Permalink
Merge pull request #13 from Nemesis-AS/feat-embed-static
Browse files Browse the repository at this point in the history
Added Static Asset Bundling
  • Loading branch information
Nemesis-AS authored Dec 16, 2024
2 parents f705224 + 41c7312 commit 5e3da64
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 27 deletions.
77 changes: 77 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ base64 = "0.21.4"
configparser = "3.0.3"
lofty = "0.15.0"
md5 = "0.7.0"
mime_guess = "2.0.5"
r2d2 = "0.8.10"
r2d2_sqlite = "0.22.0"
rusqlite = { version = "0.29.0", features = ["bundled"] }
rust-embed = { version = "8.5.0", features = ["actix"] }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.105"
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

Loklang is a local audio streaming service that can be used to access audio files on the host from any device on the network.

## Configuration

Create a file named `config.ini` in the same directory as the `loklang.exe` file and add any supported config options to that file.
```ini
[config]
rootdir = "path/to/music"
```

### Currently Supported Options

- `rootdir`: Root directory for the music files

## Development

1. Clone this repo
Expand Down
2 changes: 2 additions & 0 deletions config.example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[config]
rootdir = D:\Music\Test
33 changes: 12 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@ mod metadata;
mod routes;
mod utils;

use actix_web::{
web::{scope, Data},
App, HttpServer,
};
use actix_files::Files;
use actix_web::{web::Data, App, HttpServer};

use configparser::ini::Ini;

use r2d2::Pool;
use r2d2_sqlite::{self, SqliteConnectionManager};

use routes::{
get_albums, get_artists, get_picture, get_song_by_id, get_songs, get_songs_by_album,
get_songs_by_artist, get_stream_by_id,
};
use routes::register;

use std::{fs::create_dir_all, collections::HashMap};
use std::path::PathBuf;
use std::{collections::HashMap, fs::create_dir_all};

use rust_embed::Embed;

#[derive(Embed)]
#[folder = "views/"]
pub struct Asset;

async fn setup_db(pool: &Pool<SqliteConnectionManager>, config: &HashMap<String, Option<String>>) {
db::create_tables(pool).await;
Expand Down Expand Up @@ -64,17 +63,9 @@ async fn main() -> std::io::Result<()> {

println!("Started server at PORT 8000!");
HttpServer::new(move || {
App::new().app_data(Data::new(pool.clone())).service(
scope("/api/v1")
.service(get_songs)
.service(get_song_by_id)
.service(get_albums)
.service(get_songs_by_album)
.service(get_artists)
.service(get_songs_by_artist)
.service(get_stream_by_id)
.service(get_picture),
).service(Files::new("/", "./views").index_file("index.html"))
App::new()
.app_data(Data::new(pool.clone()))
.configure(register)
})
.bind(("0.0.0.0", 8000))?
.run()
Expand Down
13 changes: 12 additions & 1 deletion src/routes.rs → src/routes/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{

type DataDB = Data<r2d2::Pool<r2d2_sqlite::SqliteConnectionManager>>;

// GET songs
#[get("/songs")]
pub async fn get_songs(db: DataDB) -> HttpResponse {
let res = handle_song_action(&db, SongActions::GetAllSongs)
Expand Down Expand Up @@ -108,3 +107,15 @@ pub async fn get_picture(data: Path<String>, db: DataDB) -> HttpResponse {
HttpResponse::Ok().body(res[0].clone())
}
}

pub fn register(config: &mut actix_web::web::ServiceConfig) {
config
.service(get_songs)
.service(get_song_by_id)
.service(get_albums)
.service(get_songs_by_album)
.service(get_artists)
.service(get_songs_by_artist)
.service(get_stream_by_id)
.service(get_picture);
}
12 changes: 12 additions & 0 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod api;
pub mod views;

use actix_web::web::{scope, ServiceConfig};

use api::register as register_api;
use views::register as register_views;

pub fn register(config: &mut ServiceConfig) {
config.service(scope("/api/v1").configure(register_api));
config.service(scope("").configure(register_views));
}
21 changes: 21 additions & 0 deletions src/routes/views.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::utils::static_resolver::handle_embedded_file;
use actix_web::{
get,
web::{Path, ServiceConfig},
HttpResponse, Responder,
};

// View Routes
#[get("/")]
pub async fn index() -> HttpResponse {
handle_embedded_file("index.html")
}

#[get("/dist/{_:.*}")]
pub async fn dist(path: Path<String>) -> impl Responder {
handle_embedded_file(path.as_str())
}

pub fn register(config: &mut ServiceConfig) {
config.service(index).service(dist);
}
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod static_resolver;

use std::{fs, path::PathBuf};

use crate::metadata;
Expand Down
12 changes: 12 additions & 0 deletions src/utils/static_resolver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::Asset;
use actix_web::HttpResponse;
use mime_guess::from_path;

pub fn handle_embedded_file(path: &str) -> HttpResponse {
match Asset::get(path) {
Some(content) => HttpResponse::Ok()
.content_type(from_path(path).first_or_octet_stream().as_ref())
.body(content.data.into_owned()),
None => HttpResponse::NotFound().body("404 Not Found"),
}
}
6 changes: 3 additions & 3 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Loklang</title>

<link rel="stylesheet" href="./index.css" />
<link rel="stylesheet" href="./dist/index.css" />
</head>
<body>
<aside>
Expand Down Expand Up @@ -41,7 +41,7 @@

<div class="play-bar">
<div class="song-info">
<img src="./images/404.png" alt="" class="song-img" id="songImg" />
<img src="./dist/images/404.png" alt="" class="song-img" id="songImg" />
<div class="song-text">
<div id="songTitleEl" class="song-title">Some Song</div>
<div id="songArtistEl" class="song-artists">
Expand Down Expand Up @@ -109,6 +109,6 @@
</div>
</div>

<script src="./index.js"></script>
<script src="./dist/index.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions views/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ class App {
});

this.songImgEl.addEventListener("error", e => {
if (!this.songImgEl.src === "./images/404.png")
this.songImgEl.src = "./images/404.png";
if (!this.songImgEl.src === "./dist/images/404.png")
this.songImgEl.src = "./dist/images/404.png";
});

this.fetchPlaylist();
Expand Down

0 comments on commit 5e3da64

Please sign in to comment.