Skip to content

Commit

Permalink
Merge pull request #3 from nightlyprojects/dev2
Browse files Browse the repository at this point in the history
  • Loading branch information
nightlyprojects authored Jan 7, 2025
2 parents 899ab33 + 6050b27 commit cf16afa
Show file tree
Hide file tree
Showing 11 changed files with 605 additions and 76 deletions.
145 changes: 143 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,143 @@
# simple-config-server
This simple server is used to provide json-configs on request. This could be used to change stored preferences on embedded devices or as a simple replacement for a local DNS.
# Simple Config Server

A simple server for managing and distributing JSON and text-based configurations, ideal for applications such as IoT device management or local network configuration.

The server supports creating, updating, retrieving, and deleting JSON or plain text configuration files identified by unique IDs.

The container for this application is available on Docker Hub:
- For `linux/amd64` architectures: `docker pull nightlyprojects/simple-config-server:amd64-latest`
- For `linux/arm64` architectures: `docker pull nightlyprojects/simple-config-server:arm64-latest`

## Features
- **JSON Management**: Store, retrieve, update, and delete JSON configurations.
- **Text File Support**: Manage plain text files similarly to JSON files.
- **Validation**: Ensures identifiers adhere to a strict naming pattern.
- **Logging**: Logs all actions for debugging and monitoring.
- **Extensibility**: Designed with flexibility for future expansions.

## Usage

### Endpoints
The server exposes RESTful HTTP endpoints for managing files. Each file is identified by a unique `id` that adheres to the following pattern: `^[a-zA-Z0-9][a-zA-Z0-9\-_.]*$`.

### JSON File Operations
1. **Retrieve JSON Data**
- **Endpoint**: `GET /json`
- **Query Parameter**: `id` (required) - The unique identifier of the file.
- **Response**: Returns the JSON content if the file exists.

2. **Create New JSON File**
- **Endpoint**: `POST /json`
- **Query Parameter**: `id` (required) - The unique identifier for the new file.
- **Body**: Valid JSON content.
- **Response**: Creates the file or returns a conflict if the file exists.

3. **Update JSON File**
- **Endpoint**: `PUT /json`
- **Query Parameter**: `id` (required) - The unique identifier of the file.
- **Body**: Valid JSON content.
- **Response**: Updates the file content or creates a new file if it doesn’t exist.

4. **Delete JSON File**
- **Endpoint**: `DELETE /json`
- **Query Parameter**: `id` (required) - The unique identifier of the file.
- **Response**: Deletes the file or returns an error if it doesn’t exist.

### Text File Operations
1. **Retrieve Text Data**
- **Endpoint**: `GET /text`
- **Query Parameter**: `id` (required) - The unique identifier of the file.
- **Response**: Returns the text content if the file exists.

2. **Create New Text File**
- **Endpoint**: `POST /text`
- **Query Parameter**: `id` (required) - The unique identifier for the new file.
- **Body**: Plain text content.
- **Response**: Creates the file or returns a conflict if the file exists.

3. **Update Text File**
- **Endpoint**: `PUT /text`
- **Query Parameter**: `id` (required) - The unique identifier of the file.
- **Body**: Plain text content.
- **Response**: Updates the file content or creates a new file if it doesn’t exist.

4. **Delete Text File**
- **Endpoint**: `DELETE /text`
- **Query Parameter**: `id` (required) - The unique identifier of the file.
- **Response**: Deletes the file or returns an error if it doesn’t exist.

## Running the Server

### Environment Variables
- `DATA_DIR`: Specifies the root directory for storing files (default: `data`).
Don't use the variable to point to a directory outside of the container. Use the docker mechanics to mount the volume to `/data`.
- `LOG_LEVEL`: Sets the logging verbosity (default: `Information`).

### Docker Usage
To run the server using Docker:

1. Pull the appropriate image for your architecture:
```sh
docker pull nightlyprojects/simple-config-server:amd64-latest
docker pull nightlyprojects/simple-config-server:arm64-latest
```

2. Create the working directory outside the container, if you wish to access it from the outside and store persistent data.
It should have the following structure: (If you don't provide the sub-folders, the program will create them for you)
```
data/
├─ storage/
│ ├─ json/
│ │ ├─ your-config-files.json
│ ├─ text/
│ │ ├─ your-config-files.txt
├─ logs/
```

3. Run the container:
```sh
docker run -d -p 24025:24025 \
-v $(pwd)/data:/data \
nightlyprojects/simple-config-server:amd64-latest
```
Replace `$(pwd)/data` with the desired host directory for data storage.
Replace `24025` with your desired port and select the right image for your architecture.

## Examples

### Create a JSON Configuration
```sh
curl -X POST "http://localhost:24025/json?id=example" \
-H "Content-Type: application/json" \
-d '{"key":"value"}'
```

### Retrieve the JSON Configuration
```sh
curl "http://localhost:24025/json?id=example"
```

### Update the JSON Configuration
```sh
curl -X PUT "http://localhost:24025/json?id=example" \
-H "Content-Type: application/json" \
-d '{"key":"newValue"}'
```

### Delete the JSON Configuration
```sh
curl -X DELETE "http://localhost:24025/json?id=example"
```
For the txt-files, this works the same way. Just use the `/text` endpoint instead of the `/json` endpoint.

---
## Security Notice

This server does not implement any security measures. It lacks:

- Authentication and authorization.
- Encryption (e.g., HTTPS).
- Overwrite protection for files.
- Content type validation for the text-endpoint.

Use this server in secure and controlled environments only. Avoid deploying it directly on public networks without additional security layers.
1 change: 0 additions & 1 deletion examples/sample-working-dir/data/configs/post-test.json

This file was deleted.

1 change: 0 additions & 1 deletion examples/sample-working-dir/data/configs/test1.json

This file was deleted.

1 change: 0 additions & 1 deletion examples/sample-working-dir/data/configs/test3.json

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<None Remove="xunit.runner.json" />
</ItemGroup>

<ItemGroup>
<Content Include="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading

0 comments on commit cf16afa

Please sign in to comment.