-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
102f990
commit de62fa0
Showing
87 changed files
with
7,454 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,8 @@ | |
{ | ||
"path": "../lib/bolt", | ||
}, | ||
{ | ||
"path": "../svc", | ||
} | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
infra/default-builds/dockerfiles/test-ds-echo/.dockerignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/rust | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=rust | ||
|
||
### Rust ### | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/rust | ||
|
||
.dockerignore | ||
.gitignore | ||
Dockerfile | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Created by https://www.toptal.com/developers/gitignore/api/rust | ||
# Edit at https://www.toptal.com/developers/gitignore?templates=rust | ||
|
||
### Rust ### | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html | ||
Cargo.lock | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
||
# End of https://www.toptal.com/developers/gitignore/api/rust | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "test-ds-echo" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { version = "1.29", features = ["full"] } | ||
reqwest = "0.11" | ||
anyhow = "1.0" | ||
hyper = { version = "0.14", features = ["server"] } | ||
|
||
[profile.release] | ||
opt-level = 'z' | ||
lto = true | ||
codegen-units = 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM clux/muslrust:1.77.2-stable AS build | ||
RUN cargo new --bin /app | ||
WORKDIR /app | ||
COPY Cargo.toml ./ | ||
RUN cargo build --release | ||
RUN rm -r src | ||
COPY ./src ./src | ||
RUN touch src/main.rs && \ | ||
cargo build --release && \ | ||
strip target/x86_64-unknown-linux-musl/release/test-ds-echo | ||
|
||
FROM alpine:latest | ||
COPY --from=build /app/target/x86_64-unknown-linux-musl/release/test-ds-echo /usr/local/bin/app | ||
RUN chmod +x /usr/local/bin/app | ||
RUN adduser -D app | ||
USER app | ||
CMD ["/usr/local/bin/app"] | ||
|
178 changes: 178 additions & 0 deletions
178
infra/default-builds/dockerfiles/test-ds-echo/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
use std::{convert::Infallible, env, net::SocketAddr, process::Command}; | ||
|
||
use anyhow::{Context, Result}; | ||
use tokio::{ | ||
io::{AsyncBufReadExt, AsyncWriteExt}, | ||
net::{TcpListener, UdpSocket}, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
// Env | ||
let envs: Vec<(String, String)> = env::vars().collect(); | ||
println!("Env:\n{:#?}\n", envs); | ||
|
||
// resolv.conf | ||
let output = Command::new("cat") | ||
.arg("/etc/resolv.conf") | ||
.output() | ||
.expect("Failed to execute command"); | ||
println!( | ||
"resolv.conf:\n{}\n", | ||
String::from_utf8_lossy(&output.stdout) | ||
); | ||
|
||
// TEMP: Expose dev port | ||
tokio::spawn(with_select_term(echo_http_server(28234))); | ||
|
||
// TODO: Add back | ||
// Echo servers (bridge networking) | ||
// if let Ok(http_port) = env::var("PORT_test_http") { | ||
// let http_port: u16 = http_port.parse()?; | ||
// tokio::spawn(with_select_term(echo_http_server(http_port))); | ||
// } | ||
// | ||
// if let Ok(tcp_port) = env::var("PORT_test_tcp") { | ||
// let tcp_port: u16 = tcp_port.parse()?; | ||
// tokio::spawn(with_select_term(echo_tcp_server(tcp_port))); | ||
// } | ||
// | ||
// if let Ok(udp_port) = env::var("PORT_test_udp") { | ||
// let udp_port: u16 = udp_port.parse()?; | ||
// tokio::spawn(with_select_term(echo_udp_server(udp_port))); | ||
// } | ||
// | ||
// // Echo servers (host networking) | ||
// if let Ok(http_port) = env::var("HOST_PORT_HTTP") { | ||
// let http_port: u16 = http_port.parse()?; | ||
// tokio::spawn(with_select_term(echo_http_server(http_port))); | ||
// } | ||
// | ||
// if let Ok(tcp_port) = env::var("HOST_PORT_TCP") { | ||
// let tcp_port: u16 = tcp_port.parse()?; | ||
// tokio::spawn(with_select_term(echo_tcp_server(tcp_port))); | ||
// } | ||
// | ||
// if let Ok(udp_port) = env::var("HOST_PORT_UDP") { | ||
// let udp_port: u16 = udp_port.parse()?; | ||
// tokio::spawn(with_select_term(echo_udp_server(udp_port))); | ||
// } | ||
|
||
// Lobby ready | ||
// lobby_ready().await?; | ||
|
||
// Wait indefinitely | ||
println!("Waiting indefinitely..."); | ||
wait_term().await?; | ||
println!("Ctrl+C pressed. Exiting main..."); | ||
|
||
Ok(()) | ||
} | ||
|
||
// async fn lobby_ready() -> Result<()> { | ||
// let url = format!( | ||
// "{}/matchmaker/lobbies/ready", | ||
// env::var("RIVET_API_ENDPOINT").context("RIVET_API_ENDPOINT")? | ||
// ); | ||
// let token = env::var("RIVET_TOKEN").context("RIVET_TOKEN")?; | ||
// | ||
// let client = reqwest::Client::new(); | ||
// client | ||
// .post(&url) | ||
// .header("Content-Type", "application/json") | ||
// .header("Authorization", format!("Bearer {}", token)) | ||
// .send() | ||
// .await?; | ||
// | ||
// println!("Success, waiting indefinitely"); | ||
// Ok(()) | ||
// } | ||
|
||
/// Waits for the SIGTERM signal. | ||
async fn wait_term() -> Result<()> { | ||
use tokio::signal::unix::{signal, SignalKind}; | ||
|
||
signal(SignalKind::terminate()) | ||
.expect("Failed to set up SIGTERM handler") | ||
.recv() | ||
.await; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Waits until future exits or term. | ||
async fn with_select_term(future: impl std::future::Future<Output = Result<()>>) -> Result<()> { | ||
tokio::select! { | ||
result = future => result, | ||
_ = wait_term() => { | ||
println!("Ctrl+C pressed. Exiting..."); | ||
Ok(()) | ||
}, | ||
} | ||
} | ||
|
||
async fn echo_http_server(port: u16) -> Result<()> { | ||
use hyper::{ | ||
service::{make_service_fn, service_fn}, | ||
Body, Request, Response, Server, | ||
}; | ||
|
||
let addr = SocketAddr::from(([0, 0, 0, 0], port)); | ||
println!("HTTP: {}", port); | ||
|
||
async fn echo(req: Request<Body>) -> Result<Response<Body>, Infallible> { | ||
Ok(Response::new(req.into_body())) | ||
} | ||
|
||
let make_service = make_service_fn(|_conn| async { Ok::<_, Infallible>(service_fn(echo)) }); | ||
Server::bind(&addr) | ||
.serve(make_service) | ||
.await | ||
.expect("hyper server"); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn echo_tcp_server(port: u16) -> Result<()> { | ||
let addr = SocketAddr::from(([0, 0, 0, 0], port)); | ||
println!("TCP: {}", port); | ||
|
||
let listener = TcpListener::bind(&addr).await.context("bind failed")?; | ||
loop { | ||
let (socket, _) = listener.accept().await.context("accept failed")?; | ||
tokio::spawn(async move { | ||
let mut reader = tokio::io::BufReader::new(socket); | ||
let mut line = String::new(); | ||
loop { | ||
let bytes_read = reader.read_line(&mut line).await.expect("read line failed"); | ||
if bytes_read == 0 { | ||
break; | ||
} | ||
|
||
// Echo the line | ||
reader | ||
.get_mut() | ||
.write_all(format!("{line}\n").as_bytes()) | ||
.await | ||
.expect("write failed"); | ||
reader.get_mut().flush().await.expect("flush failed"); | ||
line.clear(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
async fn echo_udp_server(port: u16) -> Result<()> { | ||
let addr = SocketAddr::from(([0, 0, 0, 0], port)); | ||
println!("UDP: {}", port); | ||
|
||
let socket = UdpSocket::bind(&addr).await?; | ||
let mut buf = vec![0u8; 1024]; | ||
loop { | ||
let (size, src) = socket.recv_from(&mut buf).await?; | ||
let data = String::from_utf8_lossy(&buf[..size]); | ||
println!("Received data: {}", data); | ||
|
||
socket.send_to(&buf[..size], &src).await?; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test-ds-echo:1719995033 |
Git LFS file not shown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.