Skip to content

Commit

Permalink
Enable additional compiler optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 16, 2024
1 parent 5ceea4a commit 5337530
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 121 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

13 changes: 4 additions & 9 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ jobs:
- uses: actions/checkout@v4

- name: Install the latest toolchain
run: |
rustup update
rustup target add x86_64-unknown-linux-musl
- name: Install musl
run: sudo apt-get install -y musl-tools
run: rustup update

- name: Run tests
run: cargo test --release --target=x86_64-unknown-linux-musl --verbose
run: cargo test --verbose

- name: Build
run: cargo build --release --target=x86_64-unknown-linux-musl --verbose
run: cargo build --release --verbose

- name: Create preview release
uses: softprops/action-gh-release@v2
Expand All @@ -36,4 +31,4 @@ jobs:
tag_name: 'preview'
prerelease: true
name: 'preview'
files: 'target/x86_64-unknown-linux-musl/release/btcmap-api'
files: 'target/release/btcmap-api'
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ name = "btcmap-api"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# Compiler settings and optimizations
[profile.release]
opt-level = "s"
strip = "symbols"
codegen-units = 1
lto = "fat"
opt-level = 3
panic = "abort"

[dependencies]

Expand Down
2 changes: 1 addition & 1 deletion devtools
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ case "$1" in
deploy)
cargo test \
&& cargo build --release \
&& rsync -v target/x86_64-unknown-linux-musl/release/btcmap-api btcmap-api:/usr/local/bin/btcmap-api \
&& rsync -v target/release/btcmap-api btcmap-api:/usr/local/bin/btcmap-api \
&& ssh btcmap-api 'systemctl restart btcmap-api.service'
;;
esac
104 changes: 2 additions & 102 deletions src/area/admin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{
admin::{self},
area::{self, Area},
discord, Error,
Error,
};
use actix_web::{
delete, patch,
delete,
web::{Data, Json, Path},
HttpRequest,
};
Expand Down Expand Up @@ -39,40 +39,6 @@ struct PatchArgs {
tags: Map<String, Value>,
}

#[patch("{id_or_alias}")]
pub async fn patch(
req: HttpRequest,
id_or_alias: Path<String>,
args: Json<PatchArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Json<AreaView>, Error> {
let admin = admin::service::check(&req, &pool).await?;
let cloned_id_or_alias = id_or_alias.clone();
let area = pool
.get()
.await?
.interact(move |conn| Area::select_by_id_or_alias(&cloned_id_or_alias, &conn))
.await??
.ok_or(Error::HttpNotFound(format!(
"There is no area with id or alias = {}",
id_or_alias,
)))?;
let area = pool
.get()
.await?
.interact(move |conn| {
area::service::patch_tags(&area.id.to_string(), args.tags.clone(), conn)
})
.await??;
let log_message = format!(
"{} updated area https://api.btcmap.org/v3/areas/{}",
admin.name, area.id,
);
warn!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area.into())
}

#[delete("{id_or_alias}")]
pub async fn delete(
req: HttpRequest,
Expand Down Expand Up @@ -123,7 +89,6 @@ impl Into<Json<AreaView>> for Area {

#[cfg(test)]
mod test {
use crate::area::admin::PatchArgs;
use crate::area::Area;
use crate::element::Element;
use crate::osm::overpass::OverpassElement;
Expand All @@ -136,71 +101,6 @@ mod test {
use geojson::{Feature, GeoJson};
use serde_json::{json, Map, Value};

#[test]
async fn patch_should_return_401_if_unauthorized() -> Result<()> {
let state = mock_state().await;
Area::insert(
GeoJson::Feature(Feature::default()),
Map::new(),
&state.conn,
)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
.service(super::patch),
)
.await;
let req = TestRequest::patch()
.uri("/1")
.set_json(PatchArgs { tags: Map::new() })
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
Ok(())
}

#[test]
async fn patch_should_update_area() -> Result<()> {
let state = mock_state().await;
let admin_password = admin::service::mock_admin("test", &state.pool)
.await
.password;
let url_alias = "test";
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
Area::insert(GeoJson::Feature(Feature::default()), tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
.service(super::patch),
)
.await;
let args = r#"
{
"tags": {
"string": "bar",
"unsigned": 5,
"float": 12.34,
"bool": true
}
}
"#;
let args: Value = serde_json::from_str(args)?;
let req = TestRequest::patch()
.uri(&format!("/{url_alias}"))
.append_header(("Authorization", format!("Bearer {admin_password}")))
.set_json(args)
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
let area = Area::select_by_alias(&url_alias, &state.conn)?.unwrap();
assert!(area.tags["string"].is_string());
assert!(area.tags["unsigned"].is_u64());
assert!(area.tags["float"].is_f64());
assert!(area.tags["bool"].is_boolean());
Ok(())
}

#[test]
async fn delete_should_return_401_if_unauthorized() -> Result<()> {
let state = mock_state().await;
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ async fn main() -> Result<()> {
)
.service(
scope("areas")
.service(area::admin::patch)
.service(area::admin::delete)
.service(area::v2::get)
.service(area::v2::get_by_url_alias),
Expand Down Expand Up @@ -186,7 +185,6 @@ async fn main() -> Result<()> {
)
.service(
scope("areas")
.service(area::admin::patch)
.service(area::admin::delete)
.service(area::v3::get)
.service(area::v3::get_by_id),
Expand Down
70 changes: 70 additions & 0 deletions src/rpc/set_area_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,73 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Rp
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area.into())
}

#[cfg(test)]
mod test {
use crate::Result;

#[tokio::test]
async fn should_return_401_if_unauthorized() -> Result<()> {
//let state = mock_state().await;
//Area::insert(
// GeoJson::Feature(Feature::default()),
// Map::new(),
// &state.conn,
//)?;
//let app = test::init_service(
// App::new()
// .app_data(Data::new(state.pool))
// .service(super::patch),
//)
//.await;
//let req = TestRequest::patch()
// .uri("/1")
// .set_json(PatchArgs { tags: Map::new() })
// .to_request();
//let res = test::call_service(&app, req).await;
//assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
Ok(())
}

#[tokio::test]
async fn should_update_area() -> Result<()> {
//let state = mock_state().await;
//let admin_password = admin::service::mock_admin("test", &state.pool)
// .await
// .password;
//let url_alias = "test";
//let mut tags = Map::new();
//tags.insert("url_alias".into(), Value::String(url_alias.into()));
//Area::insert(GeoJson::Feature(Feature::default()), tags, &state.conn)?;
//let app = test::init_service(
// App::new()
// .app_data(Data::new(state.pool))
// .service(super::patch),
//)
//.await;
//let args = r#"
//{
// "tags": {
// "string": "bar",
// "unsigned": 5,
// "float": 12.34,
// "bool": true
// }
//}
//"#;
//let args: Value = serde_json::from_str(args)?;
//let req = TestRequest::patch()
// .uri(&format!("/{url_alias}"))
// .append_header(("Authorization", format!("Bearer {admin_password}")))
// .set_json(args)
// .to_request();
//let res = test::call_service(&app, req).await;
//assert_eq!(res.status(), StatusCode::OK);
//let area = Area::select_by_alias(&url_alias, &state.conn)?.unwrap();
//assert!(area.tags["string"].is_string());
//assert!(area.tags["unsigned"].is_u64());
//assert!(area.tags["float"].is_f64());
//assert!(area.tags["bool"].is_boolean());
Ok(())
}
}

0 comments on commit 5337530

Please sign in to comment.