Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/farhaanaliii/cerver
- Owner: farhaanaliii
- License: mit
- Created: 2024-07-26T13:53:32.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-11-18T10:37:52.000Z (about 2 months ago)
- Last Synced: 2024-11-18T12:09:06.223Z (about 2 months ago)
- Topics: c, c-programming, c-server, sockets, web-server
- Language: C
- Homepage:
- Size: 11.7 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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"
#includeint 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.