summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-09-20 21:45:18 +0200
committermetamuffin <metamuffin@disroot.org>2024-09-20 22:57:39 +0200
commit9ddb4d7786509bf5995bd5e254c611a05ea50eba (patch)
treede606bbb04f56e201eebdbf1a075595149441998
parentf2836d359942a6b42b1db9d1fd7624499798d925 (diff)
downloadhurrycurry-9ddb4d7786509bf5995bd5e254c611a05ea50eba.tar
hurrycurry-9ddb4d7786509bf5995bd5e254c611a05ea50eba.tar.bz2
hurrycurry-9ddb4d7786509bf5995bd5e254c611a05ea50eba.tar.zst
reg: move structs to protocol crate
-rw-r--r--Cargo.lock1
-rw-r--r--server/protocol/src/lib.rs1
-rw-r--r--server/protocol/src/registry.rs20
-rw-r--r--server/registry/Cargo.toml2
-rw-r--r--server/registry/src/list.rs9
-rw-r--r--server/registry/src/main.rs20
-rw-r--r--server/registry/src/register.rs13
7 files changed, 36 insertions, 30 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1aeb8d8d..f6ab641a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1058,6 +1058,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"env_logger",
+ "hurrycurry-protocol",
"log",
"markup",
"rocket",
diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs
index bf7fc3d1..56ddc7a8 100644
--- a/server/protocol/src/lib.rs
+++ b/server/protocol/src/lib.rs
@@ -26,6 +26,7 @@ use std::{collections::HashSet, fmt::Display};
pub use glam;
pub mod movement;
+pub mod registry;
pub const VERSION: (u32, u32) = (7, 0);
diff --git a/server/protocol/src/registry.rs b/server/protocol/src/registry.rs
new file mode 100644
index 00000000..f1e9a083
--- /dev/null
+++ b/server/protocol/src/registry.rs
@@ -0,0 +1,20 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Serialize)]
+pub struct Entry {
+ pub name: String,
+ pub address: Vec<String>,
+ pub players_online: usize,
+ pub last_game: i64,
+ pub version: (usize, usize),
+}
+
+#[derive(Debug, Deserialize, Serialize)]
+pub struct Submission {
+ pub secret: u128,
+ pub name: String,
+ pub players: usize,
+ pub last_game: i64,
+ pub version: (usize, usize),
+ pub uri: String,
+}
diff --git a/server/registry/Cargo.toml b/server/registry/Cargo.toml
index 5433106a..2db74981 100644
--- a/server/registry/Cargo.toml
+++ b/server/registry/Cargo.toml
@@ -12,3 +12,5 @@ tokio = { version = "1.39.2", features = ["full"] }
serde_json = "1.0.128"
markup = "0.15.0"
serde = { version = "1.0.210", features = ["derive"] }
+
+hurrycurry-protocol = { path = "../protocol" }
diff --git a/server/registry/src/list.rs b/server/registry/src/list.rs
index eb1ddfbf..67f2ec2a 100644
--- a/server/registry/src/list.rs
+++ b/server/registry/src/list.rs
@@ -1,5 +1,6 @@
-use crate::{PublicEntry, Registry};
+use crate::Registry;
use anyhow::Result;
+use hurrycurry_protocol::registry::Entry;
use rocket::{
get,
http::MediaType,
@@ -22,15 +23,15 @@ pub(super) async fn r_list(
}
}
-pub(super) fn generate_json_list(entries: &[PublicEntry]) -> Result<Arc<str>> {
+pub(super) fn generate_json_list(entries: &[Entry]) -> Result<Arc<str>> {
Ok(serde_json::to_string(&entries)?.into())
}
-pub(super) fn generate_html_list(entries: &[PublicEntry]) -> Result<Arc<str>> {
+pub(super) fn generate_html_list(entries: &[Entry]) -> Result<Arc<str>> {
Ok(ListPage { entries }.to_string().into())
}
markup::define!(
- ListPage<'a>(entries: &'a [PublicEntry]) {
+ ListPage<'a>(entries: &'a [Entry]) {
@markup::doctype()
html {
head {
diff --git a/server/registry/src/main.rs b/server/registry/src/main.rs
index e979289f..9ba06e49 100644
--- a/server/registry/src/main.rs
+++ b/server/registry/src/main.rs
@@ -1,10 +1,11 @@
pub mod list;
pub mod register;
+use hurrycurry_protocol::registry::Entry;
use list::{generate_html_list, generate_json_list, r_list};
use log::{error, info};
use register::r_register;
-use rocket::{get, routes, serde::Serialize, Config};
+use rocket::{get, routes, Config};
use std::{
cmp::Reverse,
collections::HashMap,
@@ -48,7 +49,7 @@ fn main() {
struct Registry {
json_response: Arc<str>,
html_response: Arc<str>,
- servers: HashMap<u128, Entry>,
+ servers: HashMap<u128, InternalEntry>,
}
impl Registry {
@@ -72,7 +73,7 @@ impl Registry {
let mut list = self
.servers
.values()
- .map(|e| PublicEntry {
+ .map(|e| Entry {
name: e.name.clone(),
address: e.address.keys().cloned().collect(),
last_game: e.last_game,
@@ -99,7 +100,7 @@ impl Registry {
}
#[derive(Debug)]
-struct Entry {
+struct InternalEntry {
name: String,
address: HashMap<String, Instant>,
players_online: usize,
@@ -107,16 +108,7 @@ struct Entry {
version: (usize, usize),
}
-#[derive(Debug, Serialize)]
-pub struct PublicEntry {
- name: String,
- address: Vec<String>,
- players_online: usize,
- last_game: i64,
- version: (usize, usize),
-}
-
-impl Default for Entry {
+impl Default for InternalEntry {
fn default() -> Self {
Self {
address: HashMap::new(),
diff --git a/server/registry/src/register.rs b/server/registry/src/register.rs
index 173361ef..fb1c668a 100644
--- a/server/registry/src/register.rs
+++ b/server/registry/src/register.rs
@@ -1,21 +1,10 @@
use crate::Registry;
+use hurrycurry_protocol::registry::Submission;
use log::{debug, info};
use rocket::{http::hyper::Uri, post, serde::json::Json, State};
-use serde::Deserialize;
use std::{net::IpAddr, str::FromStr, sync::Arc, time::Instant};
use tokio::{net::lookup_host, sync::RwLock};
-#[derive(Debug, Deserialize)]
-pub(super) struct Submission {
- secret: u128,
- name: String,
- players: usize,
- last_game: i64,
- version: (usize, usize),
-
- uri: String,
-}
-
#[post("/v1/register", data = "<submission>")]
pub(super) async fn r_register<'a>(
client_addr: IpAddr,