Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/farhaanaliii/cerver

Cerver is a lightweight HTTP server implemented in C.
https://github.com/farhaanaliii/cerver

c c-programming c-server sockets web-server

Last synced: about 1 month ago
JSON representation

Cerver is a lightweight HTTP server implemented in C.

Awesome Lists containing this project

README

        

# Cerver

Cerver is a lightweight and straightforward HTTP server library written in C, designed to be easy to use and integrate into your projects. Created by Farhan Ali ([@farhaanaliii](https://github.com/farhaanaliii)), this library allows you to quickly set up a server, define routes, and serve static files with minimal setup. Whether you're building a small personal project or a larger application, Cerver provides the essential tools needed to handle HTTP requests efficiently.

![GitHub stars](https://img.shields.io/github/stars/farhaanaliii/cerver.svg)
![GitHub forks](https://img.shields.io/github/forks/farhaanaliii/cerver.svg)
![GitHub issues](https://img.shields.io/github/issues/farhaanaliii/cerver.svg)
![GitHub release](https://img.shields.io/github/release/farhaanaliii/cerver.svg)

## Features

- Simple and easy to use
- Supports adding multiple routes
- Serves static files
- Logs HTTP requests

## Installation

To use `cerver`, clone the repository and include the `server.h` and `server.c` files in your project.

```bash
git clone https://github.com/farhaanaliii/cerver.git
```

## Usage

### Example

```c
#include "server.h"
#include

int main() {
Server server;
init_server(&server, 3000);

add_route("/", "web/index.html");
add_route("/about", "web/about.html");
add_route("/contact", "web/contact.html");

start_server(&server);

return 0;
}
```

## API

### Structs

#### `Server`

```c
typedef struct {
int port;
int server_fd;
int addrlen;
struct sockaddr_in address;
} Server;

```

#### `Route`

```c
typedef struct {
char *route;
char *file_path;
} Route;
```

### Functions

#### `void init_server(Server *server, int port);`

Initializes the server with the specified port.

- `server`: A pointer to the `Server` structure.
- `port`: The port number to listen on.

#### `void add_route(char *route, char *file_path);`

Adds a new route to the server.

- `route`: The URL path for the route.
- `file_path`: The file path to serve when the route is accessed.

#### `void start_server(Server *server);`

Starts the server and listens for incoming connections.

- `server`: A pointer to the `Server` structure.

## Contributing

Feel free to submit issues and pull requests! Contributions are welcome.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.