Add homepage

This commit is contained in:
Khue Doan 2020-06-22 00:27:57 +07:00
parent d48eb1608a
commit e187e56ab8
5 changed files with 63 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target/
Cargo.lock

7
docker-compose.yml Normal file
View File

@ -0,0 +1,7 @@
version: '3'
services:
homepage:
build: ./homepage/
ports:
- "8000:8000"

11
homepage/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "homepage"
version = "0.1.0"
authors = ["Khue Doan <khuedoan98@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "2.0"
actix-rt = "1.0"

25
homepage/Dockerfile Normal file
View File

@ -0,0 +1,25 @@
FROM rust:1.44.1-slim AS base
ENV USER=root
WORKDIR /usr/local/src/homepage/
RUN cargo init
COPY ./Cargo.toml ./Cargo.toml
RUN cargo fetch
COPY ./src/ ./src/
CMD [ "cargo", "test", "--offline" ]
FROM base AS builder
RUN cargo build --release --offline
FROM rust:1.44.1-slim
COPY --from=builder /usr/local/src/homepage/target/release/homepage /usr/local/bin/homepage
EXPOSE 8000
ENTRYPOINT [ "/usr/local/bin/homepage" ]

18
homepage/src/main.rs Normal file
View File

@ -0,0 +1,18 @@
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
async fn greet(req: HttpRequest) -> impl Responder {
let name = req.match_info().get("name").unwrap_or("World");
format!("Hello {}!", &name)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/", web::get().to(greet))
.route("/{name}", web::get().to(greet))
})
.bind("0.0.0.0:8000")?
.run()
.await
}