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.
- 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:
- Using Docker:
docker build -t rest-api-c . docker run -d -p 8080:80 --name rest-api-c rest-api-c
- Without Docker (Linux):
./main_run.sh
- Without Docker (Windows):
make ./bin/main
- Using Docker:
-
Access the API at http://localhost:8080
This RESTful web server provides the following endpoints (please note that this section is a work in progress):
- 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.
These endpoints will serve as a starting point for interacting with the API once the development is complete. You can use tools like curl
or web browsers to perform requests and test the server's functionality. Each endpoint adheres to the principles of a RESTful API, providing clear and predictable behavior.