Skip to content

Instantly share code, notes, and snippets.

@NatanR-dev
Last active November 14, 2025 16:53
Show Gist options
  • Select an option

  • Save NatanR-dev/46fb9ac7a1a053356cd56dff78e540ed to your computer and use it in GitHub Desktop.

Select an option

Save NatanR-dev/46fb9ac7a1a053356cd56dff78e540ed to your computer and use it in GitHub Desktop.
void addRoute(const char* method, const char* path, const char* contentType, const char* body) {
if (routeCount >= MAX_ROUTES) {
printf("Limite de rotas atingido\n");
return;
}
strcpy(routes[routeCount].method, method);
strcpy(routes[routeCount].path, path);
strcpy(routes[routeCount].contentType, contentType);
strcpy(routes[routeCount].body, body);
routeCount++;
}
void addRoute(const char* path, void (*handler)(struct Request*, struct Response*)) {
if (routeCount < MAX_ROUTES) {
strncpy(routes[routeCount].path, path, sizeof(routes[routeCount].path) - 1);
routes[routeCount].handler = handler;
routeCount++;
} else {
printf("Limite de rotas atingido!\n");
}
}
void app(struct Request* req, struct Response* res) {
handleRoutes(req, res);
}
bind(serverSocket, (struct sockaddr*)&addr, sizeof(addr));
listen(serverSocket, 5);
void endResponse(struct Response* res, const char* body) {
char response[2048];
snprintf(response, sizeof(response),
"HTTP/1.1 %d OK\r\n%s\r\n%s",
res->statusCode,
res->headers,
body
);
write(res->socket, response, strlen(response));
close(res->socket);
}
void handleRoutes(struct Request* req, struct Response* res) {
if (strcmp(req->url, "/") == 0) {
res->statusCode = 200;
setHeader(res, "Content-Type", "text/plain");
end(res, "Hello From Index\n");
}
else if (strcmp(req->url, "/api") == 0) {
res->statusCode = 200;
setHeader(res, "Content-Type", "application/json");
end(res, "{ \"message\": \"Hello From API\" }\n");
}
else {
res->statusCode = 404;
setHeader(res, "Content-Type", "text/plain");
end(res, "Not Found\n");
}
}
#include <stdio.h> // Entrada e saída (printf, perror)
#include <string.h> // Manipulação de strings (strlen, strcpy, strcmp)
#include <unistd.h> // Funções Unix (read, write, close)
#include <arpa/inet.h> // Rede: sockets, endereços IP, sockaddr_in
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(host);
void parseRequest(struct Request* req) {
sscanf(req->buffer, "%s %s", req->method, req->url);
}
struct Request {
char buffer[1024];
char method[8];
char url[256];
};
struct Response {
int socket;
int statusCode;
char headers[512];
};
while (1) {
clientSocket = accept(serverSocket, NULL, NULL);
struct Request req = {0};
struct Response res = { clientSocket, 200, "" };
read(clientSocket, req.buffer, sizeof(req.buffer) - 1);
parseRequest(&req);
handler(&req, &res);
}
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
void setHeader(struct Response* res, const char* key, const char* value) {
char headerLine[256];
snprintf(headerLine, sizeof(headerLine), "%s: %s\r\n", key, value);
strncat(res->headers, headerLine, sizeof(res->headers) - strlen(res->headers) - 1);
}
void createServer(const char* host, int port, void (*handler)(struct Request*, struct Response*)) {
int serverSocket, clientSocket;
struct sockaddr_in addr;
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(host);
bind(serverSocket, (struct sockaddr*)&addr, sizeof(addr));
listen(serverSocket, 5);
printf("Server running at http://%s:%d\n", host, port);
while (1) {
clientSocket = accept(serverSocket, NULL, NULL);
struct Request req = {0};
struct Response res = { clientSocket, 200, "" };
read(clientSocket, req.buffer, sizeof(req.buffer) - 1);
parseRequest(&req);
handler(&req, &res);
}
}
void handleRoutes(struct Request* req, struct Response* res) {
for (int i = 0; i < routeCount; i++) {
if (strcmp(req->method, routes[i].method) == 0 &&
strcmp(req->url, routes[i].path) == 0) {
res->statusCode = 200;
setHeader(res, "Content-Type", routes[i].contentType);
end(res, routes[i].body);
return;
}
}
// Se não encontrou rota
res->statusCode = 404;
setHeader(res, "Content-Type", "text/plain");
end(res, "Not Found\n");
}
docker build -t c-webserver .
docker run -p 3000:3000 c-webserver
# Imagem com GCC pré-instalado
FROM n0madic/alpine-gcc:9.2.0
RUN apk add --no-cache gcc musl-dev
# Criar diretório
WORKDIR /app
# Copiar o código-fonte para o container
COPY . .
# Compilar o programa
RUN gcc -o server server.c
# Executar o servidor
CMD ["./server"]
void handleRoutes(struct Request* req, struct Response* res) {
for (int i = 0; i < routeCount; i++) {
if (strcmp(req->url, routes[i].path) == 0) {
routes[i].handler(req, res);
return;
}
}
res->statusCode = 404;
setHeader(res, "Content-Type", "text/plain");
end(res, "Not Found\n");
}
void helloRoute(struct Request* req, struct Response* res) {
res->statusCode = 200;
setHeader(res, "Content-Type", "text/plain");
end(res, "Hello From Root!\n");
}
void apiRoute(struct Request* req, struct Response* res) {
res->statusCode = 200;
setHeader(res, "Content-Type", "application/json");
end(res, "{ \"message\": \"Hello From Json API\" }\n");
}
int main() {
addRoute("/", helloRoute);
addRoute("/api", apiRoute);
createServer("0.0.0.0", 3000, app);
return 0;
}
int main() {
createServer("0.0.0.0", 3000, app);
return 0;
}
int main() {
addRoute("GET", "/hello", "text/plain", "Bem-vindo à página inicial!");
addRoute("GET", "/api", "application/json", "{ \"message\": \"Hello JSON\" }");
createServer("0.0.0.0", 3000, app);
return 0;
}
#define MAX_ROUTES 10
struct Route routes[MAX_ROUTES];
int routeCount = 0;
struct Route {
char path[256];
void (*handler)(struct Request*, struct Response*);
};route-Struct.c
#define MAX_ROUTES 10
#define MAX_PATH 256
#define MAX_BODY 1024
struct Route {
char method[8];
char path[MAX_PATH];
char contentType[32];
char body[MAX_BODY];
};
struct Route routes[MAX_ROUTES];
int routeCount = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment