Skip to content

Commit

Permalink
Http (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmielchen authored Jan 19, 2024
1 parent ae7f2d9 commit d5011ab
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 145 deletions.
5 changes: 3 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
LOG_LEVEL = "trace"
DATA_DIR = { value = "target/tmp/dev.bin", relative = true}
PORT = "8654"
CACHE_SIZE = "10000"
CACHE_SIZE = "4096"
CACHE_TTL = "300"
CACHE_TTI = "60"
CACHE_TTI = "60"
CORS_ALLOWED_ORIGINS = "*"
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "varia-db"
version = "0.0.1-snapshot"
version = "0.0.2"
edition = "2021"

[dependencies]
Expand Down
92 changes: 72 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,92 @@ VariaDB | Key-Value Storage

## What is VariaDB?

VariaDB is a key-value storage system that is designed to be fast, lightweight, and easy to use. It is written in Rust.
VariaDB is a fast, lightweight key-value storage system, programmed in Rust, known for its ease of use. In typical scenarios, a client – like a browser or an application – interacts directly with VariaDB. The client sends requests to VariaDB, and VariaDB processes these requests and provides the relevant responses.

A key feature of VariaDB is that it is not designed to be used behind a server application in the backend. Instead, VariaDB is intended to be used directly in the frontend. This allows applications to communicate directly with the database, eliminating the need for a separate backend server as an intermediary.


## How to use VariaDB?

Put data into VariaDB:
```json
{
"Put": [
"key1",
{
"Text": "text"
}
]
}
Quick Start:

```bash
docker pull ghcr.io/maxmielchen/varia-db:latest
docker run -p 8654:8654 ghcr.io/maxmielchen/varia-db:latest
```
> Note: you can also use Number, Boolean, Array, Map instead of Text.

Get data from VariaDB:
Environment Variables:

| Variable | Default | Description |
| --- | --- | --- |
| `LOG_LEVEL` | `info` | The log level to use |
| `DATA_DIR` | `/data/varia.bin` | The file to store the data in |
| `PORT` | `8654` | The port to listen on |
| `CACHE_SIZE` | `4096` | The size in mb of the cache |
| `CACHE_TTL` | `3600` | The time in seconds to keep items in the cache |
| `CACHE_TTI` | `600` | The time in seconds to keep items in the cache if they are not accessed |
| `CORS_ALLOW_ORIGIN` | `*` | The origin to allow CORS requests from |

## Protocol

VariaDB can store key-value pairs. The key is a string, and the value is a typed.
A value can be a string, a number, a boolean, or a list of values, or a map of values.
[OpenAPI Documentation](openapi.yaml)

#### Values Examples

```json
{
"Get": "key1"
}
{"Text": "Hello, world!"}
```

Delete data from VariaDB:
```json
{
"Del": "key1"
"Array": [
{"Text": "Hello, world!"},
{"Number": 42},
{"Boolean": true}
]
}
```

List all keys in VariaDB:
```json
{
"List": null
"Map": [
["key2", {"Text": "Hello, world!"}],
["key1", {"Number": 42}],
["key3", {"Boolean": true}]
]
}
```

#### Operations

Put:
```curl
curl -X 'PUT' \
'http://localhost:8654/put/hello' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{"Text": "world"}'
```

Get:
```curl
curl -X 'GET' \
'http://localhost:8654/get/hello' \
-H 'accept: application/json'
```

Delete:
```curl
curl -X 'DELETE' \
'http://localhost:8654/del/hello' \
-H 'accept: application/json'
```

List:
```curl
curl -X 'GET' \
'http://localhost:8654/list' \
-H 'accept: application/json'
```
3 changes: 2 additions & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ FROM debian:bookworm-slim
ENV LOG_LEVEL=info
ENV DATA_DIR=/data/varia.bin
ENV PORT=8654
ENV CACHE_SIZE=10000
ENV CACHE_SIZE=4096
ENV CACHE_TTL=3600
ENV CACHE_TTI=600
ENV CORS_ALLOW_ORIGIN=*
COPY --from=builder /usr/local/cargo/bin/varia-db /usr/local/bin/varia-db
VOLUME /data
EXPOSE 8654
Expand Down
142 changes: 142 additions & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
openapi: "3.0.0"

info:
title: VariaDB
version: 0.0.2

servers:
- url: http://localhost:8654

paths:
/put/{key}:
put:
summary: Put a value
parameters:
- name: key
in: path
required: true
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Value'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Value'
'400':
description: Bad Request
'500':
description: Internal Server Error

/get/{key}:
get:
summary: Get a value
parameters:
- name: key
in: path
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Value'
'400':
description: Bad Request
'500':
description: Internal Server Error

/del/{key}:
delete:
summary: Delete a value
parameters:
- name: key
in: path
required: true
schema:
type: string
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Value'
'400':
description: Bad Request
'500':
description: Internal Server Error

/list:
get:
summary: List all keys
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
type: string
'500':
description: Internal Server Error

components:
schemas:
Value:
oneOf:
- $ref: '#/components/schemas/TextValue'
- $ref: '#/components/schemas/NumberValue'
- $ref: '#/components/schemas/BooleanValue'
- $ref: '#/components/schemas/ArrayValue'
- $ref: '#/components/schemas/MapValue'

TextValue:
type: object
properties:
Text:
type: string

NumberValue:
type: object
properties:
Number:
type: integer

BooleanValue:
type: object
properties:
Boolean:
type: boolean

ArrayValue:
type: object
properties:
Array:
type: array
items:
$ref: '#/components/schemas/Value'

MapValue:
type: object
properties:
Map:
type: array
items:
type: object
properties:
key:
type: string
value:
$ref: '#/components/schemas/Value'
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() {

let engine = setup::setup_engine(secondary, primary);

let engine_service = setup::setup_engine_service(engine);
let engine_service = setup::setup_engine_service(engine, configuration.cors_allowed_origins);

let web_server = setup::setup_web_server(engine_service, configuration.port).await;

Expand Down
Loading

0 comments on commit d5011ab

Please sign in to comment.