somewhat working rust baseline

This commit is contained in:
Aaro Koivukangas
2026-01-15 11:16:50 +02:00
parent f92eff7784
commit 02f0d16fc4
4 changed files with 79 additions and 3 deletions
+2 -2
View File
@@ -10,10 +10,10 @@ CMD ["cargo", "run", "--release"]
FROM debian:12-slim AS runner
# RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /app/compiler/target/release/compiler .
CMD ["./compiler"]
EXPOSE 3000
CMD ["./compiler"]
+16
View File
@@ -2,6 +2,22 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "compiler"
version = "0.1.0"
dependencies = [
"base64",
"json",
]
[[package]]
name = "json"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078e285eafdfb6c4b434e0d31e8cfcb5115b651496faca5749b88fafd4f23bfd"
+2
View File
@@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
base64 = "0.22.1"
json = "0.12.4"
+59 -1
View File
@@ -1,3 +1,61 @@
use base64::prelude::*;
use std::{
io::{BufReader, prelude::*},
net::{TcpListener, TcpStream},
};
const PORT: &str = "4000";
const HOST: &str = "127.0.0.1";
fn main() {
println!("Hello, world!");
let address = format!("{}:{}", HOST, PORT);
let listener = TcpListener::bind(&address).unwrap();
println!("Starting server at address {}", address);
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn compile(code: &str) -> &str {
println!("Code: {}", &code);
return code;
}
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&stream);
let http_request: Vec<_> = buf_reader.lines().map(|result| result.unwrap()).collect();
let body = http_request.last().unwrap();
let json = json::parse(body).unwrap();
let command = json["command"].as_str().unwrap();
println!("Request: {http_request:#?}");
println!("Body: {json:#?}");
println!("Command: {command:#?}");
match command {
"ping" => {
println!("Pong!");
let response = "HTTP/1.1 200 OK";
stream.write_all(response.as_bytes()).unwrap();
}
"compile" => {
let code = json["code"].as_str().unwrap();
let executable = compile(code);
let content = BASE64_STANDARD.encode(executable);
let length = content.len();
let response = format!("HTTP/1.1 200 OK\rContent-Length: {length}\r\n\r{content}");
stream.write_all(response.as_bytes()).unwrap();
}
_ => {
let response = "HTTP/1.1 404 NOT FOUND";
stream.write_all(response.as_bytes()).unwrap();
}
}
}