Ports, Docker

This commit is contained in:
Bastian Gruber 2023-04-19 14:15:48 +02:00
parent de4795b9a1
commit 4b517ae5a5
No known key found for this signature in database
GPG key ID: BE9F8C772B188CBF
5 changed files with 45 additions and 29 deletions

1
problem_01/.dockerignore Normal file
View file

@ -0,0 +1 @@
/target/

View file

@ -4,6 +4,6 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
env_logger = "^0.10" env_logger = "0.9.0"
log = "^0.4" log = "0.4.0"
tokio = { version = "1.27.0", features = ["full"] } tokio = { version = "1.27.0", features = ["full"] }

View file

@ -1,22 +1,11 @@
FROM rust:latest as builder FROM rust:latest as builder
RUN apt-get update && apt-get -y install ca-certificates cmake musl-tools libssl-dev && rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY . . COPY . .
# Will build and cache the binary and dependent crates in release mode RUN rustup default stable && rustup update
RUN --mount=type=cache,target=/usr/local/cargo,from=rust:latest,source=/usr/local/cargo \ RUN rustup target add x86_64-unknown-linux-musl
--mount=type=cache,target=target \ ENV PKG_CONFIG_ALLOW_CROSS=1
cargo build --release && mv ./target/release/problem_01 ./problem_01 RUN cargo build --target x86_64-unknown-linux-musl --release
FROM scratch
# Runtime image COPY --from=builder /target/x86_64-unknown-linux-musl/release/problem_01 .
FROM debian:bullseye-slim EXPOSE 8080
CMD ["/problem_01"]
# Run as "app" user
RUN useradd -ms /bin/bash app
USER app
WORKDIR /app
# Get compiled binaries from builder's cargo install directory
COPY --from=builder /usr/src/app/problem_01 /app/problem_01
# No CMD or ENTRYPOINT, see fly.toml with `cmd` override.

20
problem_01/fly.toml Normal file
View file

@ -0,0 +1,20 @@
# fly.toml app configuration file generated for restless-bush-123 on 2023-04-19T13:36:26+02:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#
app = "restless-bush-123"
primary_region = "ams"
[experimental]
auto_rollback = true
[env]
ECHO_PORT = "8080"
[[services]]
protocol = "tcp"
internal_port = 8080
[[services.ports]]
port = 8080

View file

@ -1,21 +1,27 @@
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:4040").await?; env_logger::init();
let listener = TcpListener::bind("0.0.0.0:8080").await?;
log::info!("Start TCP server");
loop { loop {
let (mut socket, _) = listener.accept().await?; let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move { tokio::spawn(async move {
let mut buf = [0; 1024]; let mut buf = [0; 1024];
loop {
loop {
let n = match socket.read(&mut buf).await { let n = match socket.read(&mut buf).await {
Ok(n) if n == 0 => return, Ok(n) if n == 0 => {
Ok(n) => n, log::info!("Receiving echo: {}", n);
return;
}
Ok(n) => {
log::info!("Receiving echo: {}", n);
n
}
Err(e) => { Err(e) => {
log::error!("failed to read from socket; err = {:?}", e); log::error!("failed to read from socket; err = {:?}", e);
return; return;