Skip to content

PedroFnseca/rest-api-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Last Commit Language Hits

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.

Database Schema

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.
email TEXT User's email address.

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:

    • Enter the folder scripts:

      cd scripts
    • Using Docker (Linux):

       ./docker_run.sh
    • Without Docker (Linux):

      ./compiler.sh --run
  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.