Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/araujo88/ultra-minimal-fast-rest-api

Ultra minimal and fast RESTful API written in C using Unix websockets, POSIX threads and SQLite database. Ideal for mock APIs in dev environment.
https://github.com/araujo88/ultra-minimal-fast-rest-api

c-api minimal-api mock-api mock-apis mock-server rest-api restful-api sqlite-orm sqlite3 sqlite3-orm

Last synced: 3 months ago
JSON representation

Ultra minimal and fast RESTful API written in C using Unix websockets, POSIX threads and SQLite database. Ideal for mock APIs in dev environment.

Awesome Lists containing this project

README

        

# ultra-minimal-fast-rest-api

A minimal and fast RESTful API potentially useful for developing mock APIs with basic CRUD (create/read/update/delete) functionality. Written in C using Unix websockets, POSIX threads for multi-threaded server and integrated with SQLite.

## Running on Docker

### Requirements

`docker`

`docker compose`

### Running

`./run_container`

## Running locally (Linux)

### Requirements

`libsqlite3-dev`

`make`

`gcc`

### Running

`./run_locally`

## Getting started

You can define your model at the xml file named `models.xml`. A user model is provided as example for a database table:

```

TEXT
TEXT
INT
REAL

```

In the `main.c` file, start the server with `create_server(server_socket, "", , )`. Default IP address is 0.0.0.0, default port is 9002 and default maximum number of simultaneous connections is 10.

### Tests

A simple Python file located in `tests/tests.py` can be used to test each endpoint.

The server should be available at `http://localhost:9002`.

## Project structure overview

### settings.h

Contains the server settings. Currently, the only setting is ALLOWED_HOSTS, which contains an array of strings corresponding to the accepted client IP addresses.

### models.h

Contains the database model. On this example, the model consists of a simple "user" table with fields "name" and "surname". Example of an user entry in JSON format:

```
{
"id": 1,
"name": "Giga",
"surname": "Chad",
"age": 29
}
```

### routes.h

Contains the method to automatically generate basic CRUD routes. Example:

`GET /` - root with "Hello world" message

`GET /users` - lists all users in .json format

`GET /users/` - list user data by its id

`PUT /users/` - update user data by its id

`DELETE /users/` delete user by its id

`POST /users` - creates a new user

### database.h

Integrates the requests from the HTTP methods to the SQLite database and parses JSON data.

### views.h

The views related to each route and its respective HTTP methods, performing calls to the database.

### server.h

Contains functions that implement a multi-threaded server using Unix sockets and POSIX threads.