getting started and failing

This commit is contained in:
2025-05-21 22:36:29 +02:00
parent 3a6ce349d9
commit 54d0b65300
2 changed files with 62 additions and 21 deletions

View File

@ -1,33 +1,18 @@
FROM rust:1.85-slim-bookworm AS build FROM gcc AS build
# get x86_64-unknown-linux-musl as a build target
RUN rustup target add x86_64-unknown-linux-musl
# create a new empty shell project
RUN USER=root cargo new --bin http_server
WORKDIR /http_server WORKDIR /http_server
# copy over your manifests COPY ./main.c .
COPY ./Cargo.lock ./Cargo.lock COPY ./libstrops.a .
COPY ./Cargo.toml ./Cargo.toml
# this build step will cache your dependencies RUN gcc -I . -L. -static -ansi -O2 -o http_server main.c -lstrops
RUN cargo build --release
RUN rm src/*.rs
# copy your source tree RUN strip ./http_server
COPY ./src ./src
# build for release as a static-pie linked binary
RUN rm ./target/release/deps/http_server*
ENV RUSTFLAGS='-C target-feature=+crt-static'
RUN cargo build --release --target x86_64-unknown-linux-musl
RUN strip /http_server/target/x86_64-unknown-linux-musl/release/http_server
FROM scratch FROM scratch
# copy the build artifact from the build stage # copy the build artifact from the build stage
COPY --from=build /http_server/target/x86_64-unknown-linux-musl/release/http_server / COPY --from=build /http_server /
COPY ./www /www COPY ./www /www
COPY ./internal /internal COPY ./internal /internal

56
main.c
View File

@ -1,6 +1,62 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
void handle_client(int client) {
ssize_t bytes_read;
size_t bufsize = 1024;
char buf[bufsize];
bytes_read = read(client, buf, bufsize - 1);
if (bytes_read == -1) {
fprintf(stderr, "Failed to read data. Error = %s\n", strerror(errno));
}
printf("%s\n", buf);
}
int main() { int main() {
int ret;
int server = socket(AF_INET, SOCK_STREAM, 0);
if (server == -1) {
fprintf(stderr, "Couldn't create socket. Error = %s\n", strerror(errno));
return 1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
ret = bind(server, (struct sockaddr*)&addr, sizeof(addr));
if (ret == -1) {
fprintf(stderr, "Couldn't bind socket. Error = %s\n", strerror(errno));
return 1;
}
ret = listen(server, 3);
if (ret == -1) {
fprintf(stderr, "Cannot listen on socket. Error = %s", strerror(errno));
return 1;
}
int client;
while(1) {
client = accept(server, (struct sockaddr*)&addr, sizeof(addr));
if (client == -1) {
fprintf(stderr, "Couldn't connect to client. Error = %s\n", strerror(errno));
}
handle_client(client);
close(client);
}
/* Unreacheable Code */
close(server);
return 0; return 0;
} }