Build Docker image

This commit is contained in:
Pierre HUBERT 2022-03-24 16:10:59 +01:00
parent 7703aa3da5
commit 650b6dfc4d
7 changed files with 1472 additions and 0 deletions

2
.gitignore vendored
View File

@ -44,3 +44,5 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release
image

6
Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM debian:bullseye-slim
COPY player-server /usr/local/bin/
COPY web /web
CMD ["player-server", "--files-path", "/web"]

14
build_docker.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
echo Generate flutter release
flutter build web --dart-define API_URL="<<<API_URL>>>" --dart-define API_TOKEN="<<<API_TOKEN>>>"
echo Build associated server
cd player-server && cargo build --release && cd ..
rm -rf image
mkdir image
cp -r build/web image
cp player-server/target/release/player-server image
echo Build Docker image
docker build image -f Dockerfile -t pierre42100/new_web_music_player

2
player-server/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target
.idea

1376
player-server/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

13
player-server/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "player-server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-files = "0.6.0"
actix-web = "4"
env_logger = "0.9.0"
log = "0.4"
clap = { version = "3.1.6", features = ["derive", "env"] }

59
player-server/src/main.rs Normal file
View File

@ -0,0 +1,59 @@
use std::path::Path;
use actix_files::Files;
use actix_web::{App, HttpResponse, HttpServer, middleware::Logger, web};
use clap::Parser;
/// Music web player server
#[derive(Parser, Debug, Clone)]
#[clap(author, version, about, long_about = None)]
struct Args {
/// Listen address
#[clap(short, long, env, default_value = "0.0.0.0:8000")]
listen_address: String,
/// Path to files
#[clap(short, long)]
files_path: String,
/// Api URL
#[clap(long, env)]
api_url: String,
/// Api token
#[clap(long, env)]
api_token: String,
}
/// Serve main file, modifying api url & token values
async fn main_js_file(data: web::Data<Args>) -> HttpResponse {
let path = Path::new(&data.files_path).join("main.dart.js");
let file = std::fs::read_to_string(path).unwrap();
let script = file
.replace("<<<API_URL>>>", &data.api_url)
.replace("<<<API_TOKEN>>>", &data.api_token);
HttpResponse::Ok()
.content_type("application/javascript")
.body(script)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let args: Args = Args::parse();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at {}", args.listen_address);
let args_cp = args.clone();
HttpServer::new(move || {
App::new()
.service(web::resource("main.dart.js").to(main_js_file))
.service(Files::new("/", &args_cp.files_path).index_file("index.html"))
.wrap(Logger::default())
.app_data(web::Data::new(args_cp.clone()))
})
.bind(args.listen_address)?
.run()
.await
}