mirror of
https://github.com/actix/examples
synced 2025-06-26 09:17:41 +02:00
restructure folders
This commit is contained in:
1
docker/.dockerignore
Normal file
1
docker/.dockerignore
Normal file
@ -0,0 +1 @@
|
||||
Dockerfile
|
9
docker/Cargo.toml
Normal file
9
docker/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "docker_sample"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "4.0.0-beta.21"
|
||||
env_logger = "0.9"
|
||||
log = "0.4"
|
38
docker/Dockerfile
Normal file
38
docker/Dockerfile
Normal file
@ -0,0 +1,38 @@
|
||||
# NB: This is not a production-grade Dockerfile.
|
||||
|
||||
#################
|
||||
## build stage ##
|
||||
#################
|
||||
FROM rust:1-slim-bullseye AS builder
|
||||
WORKDIR /code
|
||||
|
||||
# Download crates-io index and fetch dependency code.
|
||||
# This step avoids needing to spend time on every build downloading the index
|
||||
# which can take a long time within the docker context. Docker will cache it.
|
||||
RUN USER=root cargo init
|
||||
COPY Cargo.toml Cargo.toml
|
||||
RUN cargo fetch
|
||||
|
||||
# copy app files
|
||||
COPY src src
|
||||
|
||||
# compile app
|
||||
RUN cargo build --release
|
||||
|
||||
###############
|
||||
## run stage ##
|
||||
###############
|
||||
FROM debian:bullseye-slim
|
||||
WORKDIR /app
|
||||
|
||||
# copy server binary from build stage
|
||||
COPY --from=builder /code/target/release/docker_sample docker_sample
|
||||
|
||||
# set user to non-root unless root is required for your app
|
||||
USER 1001
|
||||
|
||||
# indicate what port the server is running on
|
||||
EXPOSE 8080
|
||||
|
||||
# run server
|
||||
CMD [ "/app/docker_sample" ]
|
22
docker/README.MD
Normal file
22
docker/README.MD
Normal file
@ -0,0 +1,22 @@
|
||||
# Docker sample
|
||||
|
||||
## Build image
|
||||
|
||||
```shell
|
||||
docker build -t docker_sample .
|
||||
```
|
||||
|
||||
## Run built image
|
||||
|
||||
```shell
|
||||
docker run -d -p 8080:8080 docker_sample
|
||||
# and the server should start instantly
|
||||
curl http://localhost:8080
|
||||
```
|
||||
|
||||
## Running unit tests
|
||||
|
||||
```shell
|
||||
docker build -t docker_sample:test .
|
||||
docker run --rm docker_sample:test
|
||||
```
|
28
docker/src/main.rs
Normal file
28
docker/src/main.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use actix_web::{get, middleware::Logger, App, HttpResponse, HttpServer, Responder};
|
||||
|
||||
#[get("/")]
|
||||
async fn index() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world!")
|
||||
}
|
||||
|
||||
#[get("/again")]
|
||||
async fn again() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world again!")
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||
|
||||
log::info!("Starting HTTP server: go to http://localhost:8080");
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.service(index)
|
||||
.service(again)
|
||||
})
|
||||
.bind(("0.0.0.0", 8080))?
|
||||
.run()
|
||||
.await
|
||||
}
|
Reference in New Issue
Block a user