Skip to content

Commit 7c99a55

Browse files
feat: use environment variables to configure (#860)
- improves docker/container experience
1 parent aba12cc commit 7c99a55

File tree

10 files changed

+224
-73
lines changed

10 files changed

+224
-73
lines changed

.dockerignore

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
1+
# Rust build artifacts
12
**/target/
2-
Dockerfile
3+
4+
# Git and development files
35
.git/
6+
.github/
47
.gitignore
8+
.DS_Store
9+
.idea/
10+
.vscode/
11+
.envrc
12+
.pre-commit-config.yaml
13+
.trigger*
14+
15+
# Docker files
16+
Dockerfile
17+
docker-compose.yml
18+
19+
# Configuration and environment
520
.env
6-
.cargo/config.toml
21+
deny.toml
22+
clippy.toml
23+
rustfmt.toml
24+
25+
# Documentation and non-essential files
26+
docs/
27+
CONTRIBUTING.md
28+
LICENSE
29+
README.md
30+
CNAME
31+
32+
# Node.js
33+
node_modules/
34+
pnpm-lock.yaml
35+
36+
# Nix
37+
flake.nix
38+
flake.lock
39+
result/
40+
41+
# Project-specific
42+
run/
43+
.devcontainer.json
44+
libvoidstar.so

Cargo.lock

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,27 @@ COPY --from=build-release /app/build/hyperion-proxy /
100100
LABEL org.opencontainers.image.source="https://github.com/andrewgazelka/hyperion" \
101101
org.opencontainers.image.description="Hyperion Proxy Server" \
102102
org.opencontainers.image.version="0.1.0"
103-
EXPOSE 8080
103+
EXPOSE 25565
104+
ENV HYPERION_PROXY_PROXY_ADDR="0.0.0.0:25565" \
105+
HYPERION_PROXY_SERVER="127.0.0.1:35565"
104106
ENTRYPOINT ["/hyperion-proxy"]
105-
CMD ["0.0.0.0:8080"]
106107

107108
FROM runtime-base AS tag
108109
COPY --from=build-release /app/build/tag /
109110
LABEL org.opencontainers.image.source="https://github.com/andrewgazelka/hyperion" \
110111
org.opencontainers.image.description="Hyperion Tag Event" \
111112
org.opencontainers.image.version="0.1.0"
113+
ENV TAG_IP="0.0.0.0" \
114+
TAG_PORT="35565"
112115
ENTRYPOINT ["/tag"]
113-
CMD ["--ip", "0.0.0.0", "--port", "35565"]
114116

115117
FROM runtime-base AS rust-mc-bot
116118
COPY --from=build-release /app/build/rust-mc-bot /
117119
LABEL org.opencontainers.image.source="https://github.com/andrewgazelka/rust-mc-bot" \
118120
org.opencontainers.image.description="Rust Minecraft Bot" \
119121
org.opencontainers.image.version="0.1.0"
122+
ENV BOT_SERVER="hyperion-proxy:25565" \
123+
BOT_BOT_COUNT="500" \
124+
BOT_THREADS="4"
120125
ENTRYPOINT ["/rust-mc-bot"]
121-
CMD ["hyperion-proxy:25565", "500", "4"]
122126

crates/hyperion-proxy/Cargo.toml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
[dependencies]
2-
colored = {workspace = true}
3-
kanal = {workspace = true}
4-
papaya = {workspace = true}
5-
rkyv = {workspace = true}
6-
rustc-hash = {workspace = true}
7-
tokio = {workspace = true, features = ["full", "tracing"]}
8-
tokio-util = {workspace = true, features = ["full"]}
9-
anyhow = {workspace = true}
10-
bvh = {workspace = true}
11-
bytes = {workspace = true}
12-
clap = {workspace = true}
13-
glam = {workspace = true}
14-
heapless = {workspace = true}
15-
hyperion-proto = {workspace = true}
16-
more-asserts = {workspace = true}
17-
slotmap = {workspace = true}
18-
tracing = {workspace = true}
19-
tracing-subscriber = {workspace = true}
2+
colored = { workspace = true }
3+
kanal = { workspace = true }
4+
papaya = { workspace = true }
5+
rkyv = { workspace = true }
6+
rustc-hash = { workspace = true }
7+
tokio = { workspace = true, features = ["full", "tracing"] }
8+
tokio-util = { workspace = true, features = ["full"] }
9+
anyhow = { workspace = true }
10+
bvh = { workspace = true }
11+
bytes = { workspace = true }
12+
clap = { workspace = true }
13+
glam = { workspace = true }
14+
heapless = { workspace = true }
15+
hyperion-proto = { workspace = true }
16+
more-asserts = { workspace = true }
17+
slotmap = { workspace = true }
18+
tracing = { workspace = true }
19+
tracing-subscriber = { workspace = true }
20+
serde = { version = "1.0", features = ["derive"] }
21+
envy = "0.4"
22+
dotenvy = "0.15"
2023

2124
[lints]
2225
workspace = true

crates/hyperion-proxy/src/main.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::{fmt::Debug, net::SocketAddr, path::PathBuf};
22

33
use clap::Parser;
44
use hyperion_proxy::run_proxy;
5+
use serde::Deserialize;
56
use tokio::net::TcpListener;
67
#[cfg(unix)]
78
use tokio::net::UnixListener;
@@ -12,19 +13,29 @@ use tracing_subscriber::EnvFilter;
1213
#[global_allocator]
1314
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
1415

15-
#[derive(Parser)]
16+
#[derive(Deserialize, Debug, Parser)]
1617
#[clap(version)]
1718
struct Params {
1819
/// The address for the proxy to listen on. Can be either:
1920
/// - A TCP address like "127.0.0.1:25565"
2021
/// - A Unix domain socket path like "/tmp/minecraft.sock" (Unix only)
22+
#[serde(default = "default_proxy_addr")]
2123
proxy_addr: String,
2224

2325
/// The address of the target Minecraft game server to proxy from/to
2426
#[clap(short, long, default_value = "127.0.0.1:35565")]
27+
#[serde(default = "default_server")]
2528
server: String,
2629
}
2730

31+
fn default_proxy_addr() -> String {
32+
"0.0.0.0:25565".to_string()
33+
}
34+
35+
fn default_server() -> String {
36+
"127.0.0.1:35565".to_string()
37+
}
38+
2839
#[derive(Debug)]
2940
enum ProxyAddress {
3041
Tcp(SocketAddr),
@@ -78,8 +89,25 @@ fn setup_logging() {
7889

7990
#[tokio::main]
8091
async fn main() -> Result<(), Box<dyn std::error::Error>> {
92+
// Load .env file if available
93+
dotenvy::dotenv().ok();
94+
8195
setup_logging();
82-
let params = Params::parse();
96+
97+
// Try to load params from environment variables
98+
let params = match envy::prefixed("HYPERION_PROXY_").from_env::<Params>() {
99+
Ok(params) => {
100+
info!("Loaded configuration from environment variables");
101+
params
102+
}
103+
Err(e) => {
104+
info!(
105+
"Failed to load from environment: {}, falling back to command line arguments",
106+
e
107+
);
108+
Params::parse()
109+
}
110+
};
83111

84112
let proxy_addr = ProxyAddress::parse(&params.proxy_addr)?;
85113

docker-compose.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ services:
77
target: hyperion-proxy
88
ports:
99
- "25565:25565"
10-
command: [ "--server", "tag:35565", "0.0.0.0:25565" ]
10+
command: []
1111
restart: "no"
1212
environment:
1313
- RUST_LOG=info
14+
- HYPERION_PROXY_PROXY_ADDR=0.0.0.0:25565
15+
- HYPERION_PROXY_SERVER=tag:35565
1416
networks:
1517
- proxy-network
1618
depends_on:
@@ -25,10 +27,12 @@ services:
2527
- "27750:27750"
2628
expose:
2729
- "35565"
28-
command: [ ]
30+
command: []
2931
restart: "no"
3032
environment:
3133
- RUST_LOG=info
34+
- TAG_IP=0.0.0.0
35+
- TAG_PORT=35565
3236
networks:
3337
- proxy-network
3438
rust-mc-bot:
@@ -37,12 +41,15 @@ services:
3741
context: .
3842
dockerfile: Dockerfile
3943
target: rust-mc-bot
40-
command: [ "hyperion-proxy:25565", "500", "2" ]
44+
command: []
4145
restart: "no"
4246
depends_on:
4347
- hyperion-proxy
4448
environment:
4549
- RUST_LOG=info
50+
- BOT_SERVER=hyperion-proxy:25565
51+
- BOT_BOT_COUNT=500
52+
- BOT_THREADS=2
4653
networks:
4754
- proxy-network
4855
profiles:

events/tag/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ uuid = { workspace = true }
2929
hyperion-respawn = { workspace = true }
3030
valence_protocol = { workspace = true }
3131
valence_server = { workspace = true }
32+
serde = { version = "1.0", features = ["derive"] }
33+
envy = "0.4"
3234

3335
[dev-dependencies]
3436
tracing = { workspace = true, features = ["release_max_level_info"] }

events/tag/src/main.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::net::SocketAddr;
22

33
use clap::Parser;
4+
use serde::Deserialize;
45
use tag::init_game;
56
use tracing_subscriber::{EnvFilter, Registry, layer::SubscriberExt};
67
// use tracing_tracy::TracyLayer;
@@ -10,16 +11,27 @@ use tracing_subscriber::{EnvFilter, Registry, layer::SubscriberExt};
1011
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
1112

1213
/// The arguments to run the server
13-
#[derive(Parser)]
14+
#[derive(Parser, Deserialize, Debug)]
1415
struct Args {
1516
/// The IP address the server should listen on. Defaults to 0.0.0.0
1617
#[clap(short, long, default_value = "0.0.0.0")]
18+
#[serde(default = "default_ip")]
1719
ip: String,
20+
1821
/// The port the server should listen on. Defaults to 25565
1922
#[clap(short, long, default_value = "35565")]
23+
#[serde(default = "default_port")]
2024
port: u16,
2125
}
2226

27+
fn default_ip() -> String {
28+
"0.0.0.0".to_string()
29+
}
30+
31+
const fn default_port() -> u16 {
32+
35565
33+
}
34+
2335
fn setup_logging() {
2436
tracing::subscriber::set_global_default(
2537
Registry::default()
@@ -41,9 +53,22 @@ fn main() {
4153

4254
setup_logging();
4355

44-
let Args { ip, port } = Args::parse();
56+
// Try to load config from environment variables
57+
let args = match envy::prefixed("TAG_").from_env::<Args>() {
58+
Ok(args) => {
59+
tracing::info!("Loaded configuration from environment variables");
60+
args
61+
}
62+
Err(e) => {
63+
tracing::info!(
64+
"Failed to load from environment: {}, falling back to command line arguments",
65+
e
66+
);
67+
Args::parse()
68+
}
69+
};
4570

46-
let address = format!("{ip}:{port}");
71+
let address = format!("{ip}:{port}", ip = args.ip, port = args.port);
4772
let address = address.parse::<SocketAddr>().unwrap();
4873

4974
init_game(address).unwrap();

0 commit comments

Comments
 (0)