Skip to content

Latest commit

History

History
75 lines (58 loc) 路 2.65 KB

README.md

File metadata and controls

75 lines (58 loc) 路 2.65 KB

A Simple RESTful Web Server in PURE C


RESTful web server in C using libmicrohttpd, a lightweight HTTP server library.

For a detailed example, you can refer to the libmicrohttpd manual.

Why Choose C for This Project?

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.

Postgres Database

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.

How to Run the Server

Prerequisites:

  • Docker installed on your machine (Optional)
  • GCC compiler installed
  • Make utility installed

Step-by-Step:

  1. Clone the Repository

    git clone https://github.com/PedroFnseca/rest-api-C.git
  2. Navigate to the Project Directory

    cd rest-api-C
  3. Update the Database Credentials

    • Open the pg.h file in the src directory.
    • Update the variables with your database credentials.
  4. 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
  5. Access the API at http://localhost:8080

Endpoints

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.