https://github.com/gotz1480/minimal-api-in-memory-db
A minimal RESTful API with in-memory database
https://github.com/gotz1480/minimal-api-in-memory-db
in-memory-database in-memory-db in-memory-http-server in-memory-web-api rest-api restful-api
Last synced: about 2 months ago
JSON representation
A minimal RESTful API with in-memory database
- Host: GitHub
- URL: https://github.com/gotz1480/minimal-api-in-memory-db
- Owner: gotz1480
- License: gpl-3.0
- Created: 2022-08-16T16:02:47.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-20T21:14:17.000Z (almost 3 years ago)
- Last Synced: 2025-04-04T13:13:19.796Z (2 months ago)
- Topics: in-memory-database, in-memory-db, in-memory-http-server, in-memory-web-api, rest-api, restful-api
- Language: C
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# minimal-api-in-memory-db
A minimal RESTful API with in-memory database implemented via linked lists. Coded in C language using Unix websockets for networking and POSIX for multithreading.
## Running on Docker
### Requirements
`docker`
`docker compose`### Running
`docker compose up --build`
## Running locally (Linux)
### Requirements
`make`
`gcc`### Running
`make clean`
`make`## Usage
A simple "User" model (`models.h`) with fields "name" and "surname" is implemented in a in-memory database using linked lists. The first user starts with id = 0 and then is incremented when a new user is created. The user data in .json format can then be retrieved via its id.
### Endpoints
The endpoints are defined in the `routes.h` file. The default port is 8001.
`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### Tests
A simple Python file located in `tests/tests.py` can be used to test each endpoint.
## 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:
`{"name": "Giga", "surname": "Chad"}`
### routes.h
Contains all the accepted routes and a respective accepted HTTP methods.
### linked_list.h
Contains all functions related to the implementation of single linked lists in C, including creating a node, inserting, deleting, etc.
### database.h
Integrates the requests from the HTTP methods to the linked list module, wrapping its functions and serving as a high level module.
### views.h
The views related to each route and its respective HTTP methods.
### server.h
This module contains functions that implement a multi-threaded server using Unix sockets.