Initial Rust implementation of time-server

This commit is contained in:
Yehuda Deutsch 2024-02-05 00:32:52 -05:00
commit 44ae0b38aa
Signed by: uda
GPG key ID: 8EF44B89374262A5
4 changed files with 1519 additions and 0 deletions

1455
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

16
Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "time-rs"
version = "0.1.0"
description = ""
license = "MIT"
edition = "2021"
rust-version = "1.75.0"
include = [
"Cargo.toml",
"src/**/*",
]
[dependencies]
actix-web = "4.5.0"
chrono = "0.4.33"

15
Dockerfile Normal file
View file

@ -0,0 +1,15 @@
FROM docker.io/library/rust:1.75 AS build
WORKDIR /code
COPY Cargo.toml Cargo.lock /code/
COPY src/ /code/src/
RUN cargo build --release --bin time-rs
FROM docker.io/library/debian:bookworm
WORKDIR /code
COPY --from=build /code/target/release/time-rs /code/time-rs
CMD ["/code/time-rs"]

33
src/main.rs Normal file
View file

@ -0,0 +1,33 @@
use std::io;
use actix_web::{App, HttpServer, rt, web};
use chrono::{Datelike, Utc};
async fn status_view() -> io::Result<String> {
Ok("OK".into())
}
async fn now_view() -> io::Result<String> {
let now = Utc::now();
Ok(now.format("%Y-%m-%d %H:%M:%S").to_string().into())
}
async fn ordinal_view() -> io::Result<String> {
let now = Utc::now();
Ok(now.ordinal().to_string().into())
}
fn main() -> io::Result<()> {
rt::System::new().block_on(
HttpServer::new(|| {
App::new()
.route("/", web::get().to(now_view))
.route("/diy", web::get().to(ordinal_view))
.route("/status", web::get().to(status_view))
})
.bind(("0.0.0.0", 8000))?
.workers(1)
.run()
)
}