Last active
November 14, 2025 16:53
-
-
Save NatanR-dev/46fb9ac7a1a053356cd56dff78e540ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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++; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void app(struct Request* req, struct Response* res) { | |
| handleRoutes(req, res); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bind(serverSocket, (struct sockaddr*)&addr, sizeof(addr)); | |
| listen(serverSocket, 5); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct sockaddr_in addr; | |
| addr.sin_family = AF_INET; | |
| addr.sin_port = htons(port); | |
| addr.sin_addr.s_addr = inet_addr(host); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void parseRequest(struct Request* req) { | |
| sscanf(req->buffer, "%s %s", req->method, req->url); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Request { | |
| char buffer[1024]; | |
| char method[8]; | |
| char url[256]; | |
| }; | |
| struct Response { | |
| int socket; | |
| int statusCode; | |
| char headers[512]; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int serverSocket = socket(AF_INET, SOCK_STREAM, 0); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| docker build -t c-webserver . | |
| docker run -p 3000:3000 c-webserver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int main() { | |
| addRoute("/", helloRoute); | |
| addRoute("/api", apiRoute); | |
| createServer("0.0.0.0", 3000, app); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int main() { | |
| createServer("0.0.0.0", 3000, app); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define MAX_ROUTES 10 | |
| struct Route routes[MAX_ROUTES]; | |
| int routeCount = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Route { | |
| char path[256]; | |
| void (*handler)(struct Request*, struct Response*); | |
| };route-Struct.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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