Active-x_Rust_StaticFiles_Server_Docker

Write a simple Rust Server that can server MarkDown files from wwwroot directory, and a Dockerfile to run this program?

Sure! Below is a simple example of a Rust server that serves Markdown files from a wwwroot directory, along with a Dockerfile to run this program.

Step 1: Setup the Rust Project

First, create a new Rust project:

cargo new markdown_server
cd markdown_server

Step 2: Add Dependencies

Open the Cargo.toml file and add the following dependencies:

[dependencies]
actix-web = "4.0"
tokio = { version = "1", features = ["full"] }

Step 3: Implement the Server

Create a directory named wwwroot in the project root and put your Markdown files in it.

Then, create the Rust server in src/main.rs:

use actix_files as fs;
use actix_web::{App, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(fs::Files::new("/", "./wwwroot").show_files_listing())
    })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}

This code sets up a basic HTTP server using actix-web and actix-files to serve static files from the wwwroot directory.

Step 4: Create the Dockerfile

Create a Dockerfile in the project root:

# Step 1: Build the Rust binary
FROM rust:latest as builder

# Create a new empty shell project
RUN USER=root cargo new --bin markdown_server
WORKDIR /markdown_server

# Copy our manifests
COPY Cargo.toml Cargo.lock ./

# This build step will cache dependencies
RUN cargo build --release
RUN rm src/*.rs

# Copy the source files
COPY ./src ./src

# Build our project
RUN cargo build --release

# Step 2: Create a minimal docker image to run our server
FROM debian:buster-slim

# Copy the build artifact from the build stage
COPY --from=builder /markdown_server/target/release/markdown_server /usr/local/bin/markdown_server

# Copy the wwwroot directory
COPY ./wwwroot /wwwroot

# Set the working directory
WORKDIR /wwwroot

# Service must listen to $PORT environment variable
EXPOSE 8080

# Run the binary
CMD ["markdown_server"]

Step 5: Build and Run the Docker Image

Build the Docker image:

docker build -t markdown_server .

Run the Docker container:

docker run -p 8080:8080 markdown_server

Your Rust server should now be running and serving Markdown files from the wwwroot directory. You can access it by navigating to http://localhost:8080 in your web browser.

Summary

  • Rust Server: Uses actix-web and actix-files to serve static files.

  • Dockerfile: Multi-stage build to create a minimal Docker image containing only the necessary files to run the server.

Feel free to adjust the configuration as per your requirements!

Last updated