Skip to content

Commit

Permalink
New bug fixes and linux/arm64 build
Browse files Browse the repository at this point in the history
- Multi platform build w/ QEMU
- Bump Rust version to use sparse registry
- /mc/matches route
- Configurable ports
- Make sure punishment is actually active
- Fix join sound route
  • Loading branch information
chatasma committed Sep 25, 2023
1 parent 3b14a13 commit 9d24fc7
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

Expand All @@ -36,6 +38,7 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.67
FROM rust:1.72
WORKDIR /usr/src/mars-api
RUN mkdir /app
COPY . .
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ The resulting program can be found in `target/release/mars_api_rs`. See [the ref

## Notes

Currently, the websocket listens on port 7000 and the HTTP API listens on port 8000.
Currently, the websocket listens on port 7000 and the HTTP API listens on port 8000. This can be changed using the environment variables `MARS_WS_PORT` and `MARS_HTTP_PORT` respectively.
1 change: 1 addition & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ impl Database {

pub async fn get_active_player_punishments(&self, player: &Player) -> Vec<Punishment> {
let mut puns : Vec<Punishment> = self.get_player_punishments(player).await;
puns.retain(|p| p.is_active());
puns.sort_by(|p1, p2| {
p1.issued_at.partial_cmp(&(p2.issued_at)).unwrap_or(std::cmp::Ordering::Equal)
});
Expand Down
20 changes: 19 additions & 1 deletion src/database/models/punishment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use mars_api_rs_derive::IdentifiableDocument;
use mars_api_rs_macro::IdentifiableDocument;
use serde::{Serialize, Deserialize};
use strum_macros::Display;
use crate::database::CollectionOwner;
use crate::{database::CollectionOwner, util::time::get_u64_time_millis};

use super::player::SimplePlayer;

Expand All @@ -29,6 +29,24 @@ pub struct Punishment {
pub server_id: Option<String>
}

impl Punishment {
pub fn expires_at(&self) -> i64 {
if self.action.length == -1 {
-1
} else {
(self.issued_at as i64) + self.action.length
}
}

pub fn is_active(&self) -> bool {
if self.reversion.is_some() {
return false;
} else {
return self.action.length == -1 || (get_u64_time_millis() as i64) < self.expires_at()
}
}
}

impl CollectionOwner<Punishment> for Punishment {
fn get_collection(database: &crate::database::Database) -> &mongodb::Collection<Punishment> {
&database.punishments
Expand Down
20 changes: 20 additions & 0 deletions src/http/match/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use rocket::{State, Build, Rocket};
use crate::{database::models::r#match::Match, MarsAPIState, util::{responder::JsonResponder, error::ApiErrorResponder, r#macro::unwrap_helper}};

#[get("/<match_id>")]
pub async fn matches(
state: &State<MarsAPIState>,
match_id: &str
) -> Result<JsonResponder<Match>, ApiErrorResponder> {
let match_id = match_id.to_lowercase();
let cached_match =
unwrap_helper::return_default!(
state.match_cache.get(&state.database, &match_id).await,
Err(ApiErrorResponder::validation_error())
);
Ok(JsonResponder::ok(cached_match))
}

pub fn mount(rocket_build: Rocket<Build>) -> Rocket<Build> {
rocket_build.mount("/mc/matches", routes![matches])
}
1 change: 1 addition & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod report;
pub mod leaderboard;
pub mod tag;
pub mod perks;
pub mod r#match;
4 changes: 2 additions & 2 deletions src/http/perks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn get_join_sounds(
Json(&state.config.data.join_sounds)
}

#[post("/<player_id>/sound", format = "json", data = "<set_join_req>")]
#[post("/join_sounds/<player_id>/sound", format = "json", data = "<set_join_req>")]
async fn update_join_sound(
state: &State<MarsAPIState>,
player_id: &str,
Expand Down Expand Up @@ -41,4 +41,4 @@ pub fn mount(rocket_build: Rocket<Build>) -> Rocket<Build> {
get_join_sounds,
update_join_sound
])
}
}
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ fn rocket(state: MarsAPIState) -> Rocket<Build> {
&http::punishment::mount,
&http::perks::mount,
&http::leaderboard::mount,
&http::report::mount
&http::report::mount,
&http::r#match::mount
];
let is_debug = env::var("MARS_DEBUG").unwrap_or("false".to_owned()).parse::<bool>().unwrap_or(false);
let http_port = env::var("MARS_HTTP_PORT").unwrap_or("8000".to_owned()).parse::<u32>().unwrap_or(8000);
let config : Config = Figment::from(
if is_debug { Config::debug_default() } else { Config::release_default() }
).merge::<(&str, IpAddr)>(("address", Ipv4Addr::new(0, 0, 0, 0).into())).extract().unwrap();
)
.merge::<(&str, IpAddr)>(("address", Ipv4Addr::new(0, 0, 0, 0).into()))
.merge(("port", http_port))
.extract().unwrap();
let mut rocket_build = rocket::custom(config).manage(state);

rocket_build = mounts.iter().fold(rocket_build, |mut build, mount_fn| {
Expand Down Expand Up @@ -161,12 +166,13 @@ async fn main() -> Result<(), String> {
leaderboards
};

let ws_port = env::var("MARS_WS_PORT").unwrap_or("7000".to_owned()).parse::<u32>().unwrap_or(7000);
let res = tokio::try_join!(
setup_rocket(state.clone()),
setup_socket(
SocketState {
api_state: Arc::new(state.clone())
}, 7000
}, ws_port
)
);

Expand Down

0 comments on commit 9d24fc7

Please sign in to comment.