From dce1929167242de73a36a00de30feccee6a5f1ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BF=98=E4=B8=8D=E5=A6=82=E4=B8=80=E5=8F=AA=E7=8C=AA?= =?UTF-8?q?=E5=A8=81=E6=AD=A6?= Date: Thu, 11 Apr 2024 17:36:52 +0800 Subject: [PATCH] config --- .env | 3 ++- Cargo.lock | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 8 ++++++++ readme.md | 8 ++++++++ src/main.rs | 22 +++++++++++++++++++-- 5 files changed, 93 insertions(+), 3 deletions(-) diff --git a/.env b/.env index 2d42d5f..b817013 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ -DATABASE_URL=D:/Share/Map/MBTiles/中国4-10层卫星地图(G).mbtiles \ No newline at end of file +TILE_BASE_PATH=D:/Share/Map/MBTiles/中国4-10层卫星地图(G).mbtiles +TERRAIN_BASE_PATH=D:/Share/Map/terrain \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index b13d91f..5ac8d45 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,6 +34,29 @@ dependencies = [ "smallvec", ] +[[package]] +name = "actix-files" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf0bdd6ff79de7c9a021f5d9ea79ce23e108d8bfc9b49b5b4a2cf6fad5a35212" +dependencies = [ + "actix-http", + "actix-service", + "actix-utils", + "actix-web", + "bitflags 2.5.0", + "bytes", + "derive_more", + "futures-core", + "http-range", + "log", + "mime", + "mime_guess", + "percent-encoding", + "pin-project-lite", + "v_htmlescape", +] + [[package]] name = "actix-http" version = "3.6.0" @@ -673,6 +696,12 @@ dependencies = [ "itoa", ] +[[package]] +name = "http-range" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573" + [[package]] name = "httparse" version = "1.8.0" @@ -787,6 +816,7 @@ name = "mbtiles_server" version = "0.1.0" dependencies = [ "actix-cors", + "actix-files", "actix-web", "dotenvy", "env_logger", @@ -808,6 +838,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "miniz_oxide" version = "0.7.2" @@ -1292,6 +1332,15 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" @@ -1340,6 +1389,12 @@ dependencies = [ "rand", ] +[[package]] +name = "v_htmlescape" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e8257fbc510f0a46eb602c10215901938b5c2a7d5e70fc11483b1d3c9b5b18c" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index ccbeb75..b2f48f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,8 +3,16 @@ name = "mbtiles_server" version = "0.1.0" edition = "2021" +[target.x86_64-unknown-linux-musl] +linker = "x86_64-linux-musl-gcc" + +# [target.x86_64-unknown-linux-musl] +# linker = "rust-lld" +# rustflags = ["-C", "linker-flavor=ld.lld"] + [dependencies] actix-cors = "0.7.0" +actix-files = "0.6.5" actix-web = "4.5.1" dotenvy = "0.15.7" env_logger = "0.11.3" diff --git a/readme.md b/readme.md index 69f1056..64b6449 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,11 @@ # 地图瓦片服务 mbtiles 发布服务 + +## 使用 + +``` +TILE_BASE_PATH=mbtiles瓦片文件路径 +TERRAIN_BASE_PATH=terrain文件存储路径 + +``` diff --git a/src/main.rs b/src/main.rs index 1e55316..348cc27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ type Pool = ::r2d2::Pool<::r2d2_sqlite::SqliteConnectionManager>; pub type Connection = ::r2d2::PooledConnection<::r2d2_sqlite::SqliteConnectionManager>; fn initialize_db_pool() -> Pool { - let conn_spec = std::env::var("DATABASE_URL").expect("DATABASE_URL should be set"); + let conn_spec = std::env::var("TILE_BASE_PATH").expect("TILE_BASE_PATH should be set"); let manager = ::r2d2_sqlite::SqliteConnectionManager::file(conn_spec); Pool::new(manager).unwrap() } @@ -43,7 +43,7 @@ async fn execute( Ok(data) } -#[::actix_web::get("/{z}/{x}/{y}.png")] +#[::actix_web::get("/tile/{z}/{x}/{y}.png")] async fn get_tile( pool: ::actix_web::web::Data, info: ::actix_web::web::Path<(u32, i64, i64)>, @@ -68,6 +68,16 @@ async fn get_tile( .body(data) } +#[::actix_web::get("/terrain/{tail:.*}")] +async fn get_terrain( + terrain: ::actix_web::web::Data, + info: ::actix_web::web::Path, +) -> impl ::actix_web::Responder { + let info = info.into_inner(); + ::log::info!("{}", terrain.to_string() + &info); + ::actix_files::NamedFile::open(terrain.to_string() + &info) +} + #[::actix_web::main] async fn main() -> std::io::Result<()> { ::dotenvy::dotenv().ok(); @@ -76,6 +86,12 @@ async fn main() -> std::io::Result<()> { let pool = initialize_db_pool(); + let mut terrain = std::env::var("TERRAIN_BASE_PATH").expect("TERRAIN_BASE_PATH should be set"); + + if !terrain.ends_with("/") { + terrain.push('/'); + } + ::log::info!("starting HTTP server at http://127.0.0.1:8080"); ::actix_web::HttpServer::new(move || { @@ -96,7 +112,9 @@ async fn main() -> std::io::Result<()> { .max_age(3600), ) .app_data(::actix_web::web::Data::new(pool.clone())) + .app_data(::actix_web::web::Data::new(terrain.clone())) .service(get_tile) + .service(get_terrain) }) .bind(("127.0.0.1", 8080))? .workers(2)