Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/target
.DS_Store
.DS_Store
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cafetera"
version = "0.2.5"
version = "0.3.0"
edition = "2021"
description = "simple HTTP mock server"
license = "MIT"
Expand All @@ -12,4 +12,5 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.115"
serde_with = "3.7.0"
toml = "0.8.14"
hteapot = "0.2.5"
hteapot = "0.4.2"
#hteapot = { path = "../hteapot" }
77 changes: 76 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Run the server with the following command, replacing <port> with your desired po
```shell
cargo run <port> <config_path>
```
OR
OR

```shell
CAFETERA <port> <config_path>
Expand Down Expand Up @@ -86,11 +86,86 @@ body = '''
"email": ""
}
'''

[[db]]
path = "/db/users"
data = '''
{
"users": [
{
"name": "Jhon",
"surname": "Smith",
"age": 35
}
],
"last_id": "3bed620f-6020-444b-b825-d06240bfa632"
}
'''
```
## Usage

After starting the server, it will listen for HTTP requests on the specified port. The server matches incoming requests against the paths defined in the configuration file and responds with the corresponding status code and body.

### DB mode
The database consists of simple JSON structures that can be accessed and modified using GET, POST, PATCH, and DELETE requests

#### Read Data
```HTTP
GET /db/users/users/0 HTTP/1.1
```
This request retrieves the JSON object at the specified path
```JSON
{
"age": 35,
"name": "Jhon",
"surname": "Smith"
}
```
You can also filter results using query parameters. For example:
for example
```HTTP
GET /db/users/users?name="Jhon" HTTP/1.1
```
This request returns all users matching the specified criteria.


#### Create

```HTTP
PUSH /db/users/users HTTP/1.1

{
"age": 19,
"name": "Sarah",
"surname": "Brown"
}
```
This request adds a new entry to the users array.

Additionally, you can add new attributes to existing objects dynamically.

#### UPDATE

```HTTP
PATCH /db/users/users/1 HTTP/1.1

{"name":"Sara"}
```
This request updates the user at index 1, changing the name from "Sarah" to "Sara".

Using PATCH, only the specified attributes will be modified, while the rest of the object remains unchanged. If the provided attribute does not exist, it will not be added.

#### DELETE

```HTTP
DELETE /db/users/users/1 HTTP/1.1
```

This request removes the user at index 1 from the database.




Available wildcard variables:
- [x] {{path}}: The path of the request
- [ ] {{query}}: The query string of the request
Expand Down
67 changes: 66 additions & 1 deletion demo_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,69 @@ body = '''
"name": "Jane Doe",
"email": ""
}
'''
'''


[[db]]
path = "/db/cafetera"
data = '''
{
"db_name": "messages",
"owner": "Albruiz",
"users": [
{
"id": 0,
"name": "Alberto",
"surname": "Ruiz",
"age": 25,
"admin": true
},
{
"id": 1,
"name": "Eithne",
"surname": "Flor",
"age": 21,
"admin": false
},
{
"id": 2,
"name": "Juan",
"surname": "Perez",
"age": 52,
"admin": false
}
],
"messages": [
{
"id": 1,
"from": 0,
"to": 1,
"content": "Hello, how are you?"
},
{
"id": 2,
"from": 1,
"to": 0,
"content": "I'm good, thanks! How about you?"
},
{
"id": 3,
"from": 2,
"to": 0,
"content": "Hey, what's up?"
},
{
"id": 4,
"from": 0,
"to": 2,
"content": "Not much, just working on a project."
},
{
"id": 5,
"from": 1,
"to": 2,
"content": "Are you free to chat later?"
}
]
}
'''
62 changes: 31 additions & 31 deletions src/config_parser.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@

use std::{collections::HashMap, fs};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs};

use toml;
use crate::utils::compare_path;
use toml;

#[derive(Serialize, Deserialize,Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Endpoint {
pub path: String,
pub status: u16,
pub body: String,
pub path: String,
pub status: u16,
pub body: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct DB {
pub path: String,
pub data: String,
}

pub trait EndpointSearch {
fn get_iter(&self, key: &str) -> Option<Endpoint>;
fn get_iter(&self, key: &str) -> Option<Endpoint>;
}

impl EndpointSearch for Vec<Endpoint> {
fn get_iter(&self, key: &str) -> Option<Endpoint> {
for endpoint in self {
if compare_path(endpoint.path.to_string(), key.to_string()) {
return Some(endpoint.clone());
} else {
continue;
}
fn get_iter(&self, key: &str) -> Option<Endpoint> {
for endpoint in self {
if compare_path(endpoint.path.to_string(), key.to_string()) {
return Some(endpoint.clone());
} else {
continue;
}
}
return None;
}
return None;
}
}



#[derive(Serialize, Debug, Deserialize)]
pub struct Config {
pub endpoints: HashMap<String,Vec<Endpoint>>
pub endpoints: HashMap<String, Vec<Endpoint>>,
pub db: Vec<DB>,
}


impl Config {

pub fn import(path: &str) -> Self {
let config_toml = fs::read_to_string(path).unwrap();
// Parsear el TOML
let config: Config = toml::from_str(&config_toml).unwrap();
return config;

}

}
pub fn import(path: &str) -> Self {
let config_toml = fs::read_to_string(path).unwrap();
// Parsear el TOML
let config: Config = toml::from_str(&config_toml).unwrap();
return config;
}
}
Loading
Loading