RESTful web server in C using libmicrohttpd, a lightweight HTTP server library.
For a detailed example, you can refer to the libmicrohttpd manual.
I've deliberately chosen the C programming language for this project to challenge myself and revisit my programming roots. C was my first language, and it remains my favorite. Additionally, C finds extensive use in embedded systems and real-time applications, which are areas I particularly enjoy exploring as a hobbyist.
My primary goal was to gain a deeper understanding of how APIs function under the hood and how they communicate with the underlying operating system. The C language is exceptionally suited for this purpose due to its low-level nature and direct interaction with hardware and system resources.
In this project, I'm using a Postgres database to store user information. The database is hosted on cloud in the supabase, and the server communicates with it using the libpq library.
The database schema consists of a single table named users
. The table has the following columns:
Column Name | Data Type | Description |
---|---|---|
id | SERIAL | Unique identifier for each user. |
name | TEXT | User's name. |
TEXT | User's email address. |
- Docker installed on your machine (Optional)
- GCC compiler installed
- Make utility installed
-
Clone the Repository
git clone https://github.com/PedroFnseca/rest-api-C.git
-
Navigate to the Project Directory
cd rest-api-C
-
Update the Database Credentials
- Open the
pg.h
file in thesrc
directory. - Update the variables with your database credentials.
- Open the
-
Running the Server:
-
Enter the folder
scripts
:cd scripts
-
Using Docker (Linux):
./docker_run.sh
-
Without Docker (Linux):
./compiler.sh --run
-
-
Access the API at http://localhost:8080
This RESTful web server provides the following endpoints.
- GET /users: Retrieve a list of users.
- GET /users/{id}: Retrieve detailed information about a specific user.
- POST /users: Create a new user.
- PUT /users/{id}: Update information for a specific user.
- DELETE /users/{id}: Delete a user.