Skip to content

PedroFnseca/rest-api-C

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Simple RESTful Web Server in PURE C

Language Hits License Stars Forks

This project implements a RESTful web server in C, leveraging the libmicrohttpd library. libmicrohttpd is a lightweight, designed for simplicity and efficiency, making it an excellent choice for building low-level web servers.

The server handles HTTP requests, manages routing, and interacts with a PostgreSQL database using the libpq library. It demonstrates core concepts such as:

  • HTTP request/response handling
  • Database connectivity (via PostgreSQL)
  • Memory management in C
  • Low-level socket programming

For a deeper dive into the implementation, refer to the official libmicrohttpd manual, which provides a detailed example of a simple HTTP server.

Why Choose C for This Project?

The C programming language was chosen for this project due to its low-level capabilities, which provide fine-grained control over system resources and memory management. By implementing a RESTful web server in C, this project demonstrates how to handle HTTP requests, manage sockets, and interact with system libraries like libmicrohttpd and libpq for database connectivity.

C is particularly well-suited for this type of application because:

  • Performance: C allows for highly optimized code, making it ideal for building lightweight and efficient servers.
  • Portability: C code can be compiled and run on a wide range of platforms, from embedded systems to modern servers.
  • Direct System Interaction: C provides direct access to system calls and hardware, enabling a deeper understanding of how web servers and APIs operate at a lower level.
  • Minimal Dependencies: Unlike higher-level languages, C minimizes reliance on external frameworks, resulting in a smaller and more self-contained application.

✏️ Note:: This project serves as a practical example of how to build a RESTful API in a language that is often overlooked for web development but remains foundational in systems programming.

Key Benefits of Using C:

  • Efficiency: Low memory footprint and high execution speed.
  • Control: Full control over memory allocation, pointers, and system resources.
  • Learning Opportunity: A deeper understanding of networking, HTTP protocols, and database interactions without the abstraction of higher-level frameworks.

Database Configuration

This project uses a PostgreSQL database to store user information. The database is hosted on Supabase, a cloud-based platform that provides a fully managed PostgreSQL database. The server communicates with the database using the libpq library, which is the official C API for PostgreSQL.

Database Schema

The database consists of a single table named users, designed to store user data. Below is the schema for the users table:

Column Name Data Type Description
id SERIAL Unique identifier for each user.
name TEXT The name of the user.
email TEXT The email address of the user.

How to Run the Server

Prerequisites

Before running the server, ensure you have the following installed:

  • GCC Compiler: Required to compile the C code.
  • Make Utility: Used to simplify the build process.
  • Docker (Optional): For running the server in a containerized environment.

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.
      #define DB_HOST "your_database_host"
      #define DB_NAME "your_database_name"
      #define DB_USER "your_database_user"
      #define DB_PASSWORD "your_database_password"
      #define DB_PORT "your_database_port"
  4. Running the Server:
    • Enter the folder scripts:
      cd scripts
      
    • Option 1: Using Docker (Recommended for Linux):
       ./docker_run.sh
      
    • Option 2: 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.

Related Articles

Here are some articles I’ve written about this project:


Stargazers repo roster for @PedroFnseca/Rest-Api-C