diff --git a/.gitea/workflows/publish-docker.yml b/.gitea/workflows/publish-docker.yml new file mode 100644 index 0000000..60ee8e8 --- /dev/null +++ b/.gitea/workflows/publish-docker.yml @@ -0,0 +1,46 @@ +--- +name: Build and Publish Docker Image +on: + push: + branches: + - main + tags: + - 'v*' + +jobs: + build-and-push: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Log in to Gitea Container Registry + uses: docker/login-action@v3 + with: + registry: git.br0tkasten.de + username: ${{ gitea.actor }} + # access token of the user triggering this action + # in gitea -> user settings -> applications -> new token -> write:packages and write:repositories + # then in gitea -> user settings -> actions -> secrets -> add new secret named "TOKEN" with the value of the access token. + password: ${{ secrets.TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v5 + with: + images: git.br0tkasten.de/${{ gitea.repository }} + flavor: | + latest=true + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=sha + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + diff --git a/.gitignore b/.gitignore index 07f838e..3d9cfa1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__/ venv/ instance/ +webvars.db diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..60206be --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +# Use an official Python runtime as a parent image +FROM python:3.11-slim + +# Set the working directory in the container +WORKDIR /app + +# Install curl for health check +RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/* + +# Copy the requirements file into the container +COPY requirements.txt . + +# Install any needed packages specified in requirements.txt +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the current directory contents into the container at /app +COPY . . + +# Make port 80 available to the world outside this container +EXPOSE 80 + +# Add health check +HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD curl http://localhost || exit 1 + +# Run the application using Gunicorn +CMD ["gunicorn", "--bind", "0.0.0.0:80", "--workers", "4", "wsgi:app"] \ No newline at end of file diff --git a/README.md b/README.md index e69de29..a5974a6 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,68 @@ +# WebVars + +WebVars is a lightweight Flask application for storing and retrieving configuration variables. It supports namespacing by project and uses SQLite for persistence. + +## Features + +- **Simple API**: Get and set variables via HTTP GET requests. +- **Namespacing**: Organize variables by namespace and project. +- **Dockerized**: Ready to deploy with Docker and Docker Compose. + +## API Usage + +### Set a Variable +To set a variable, make a GET request to the following pattern: +`GET ////` + +**Example:** +```bash +curl http://localhost:8080/dev/backend/api_key/secret123 +``` + +### Get a Variable +To retrieve a variable, make a GET request to: +`GET ///` + +**Example:** +```bash +curl http://localhost:8080/dev/backend/api_key +``` + +## Deployment + +### Using Docker Compose + +The easiest way to run WebVars is with Docker Compose. + +1. **Start the service:** + ```bash + docker compose up -d + ``` + The service will be available at `http://localhost:8080`. + +2. **Persistence:** + The `docker-compose.yml` is configured to mount a local `webvars.db` file to `/app/webvars.db` inside the container. It is recommended to create this file before starting the container to ensure it is treated as a file, not a directory. + + ```bash + touch webvars.db + docker compose up -d + ``` + +### Using Docker CLI + +You can also build and run the container manually. + +1. **Build the image:** + ```bash + docker build -t webvars . + ``` + +2. **Run the container:** + ```bash + touch webvars.db + docker run -d \ + -p 8080:80 \ + -v $(pwd)/webvars.db:/app/webvars.db \ + --name webvars \ + webvars + ``` diff --git a/app.py b/app.py index 4453565..010b467 100644 --- a/app.py +++ b/app.py @@ -31,6 +31,8 @@ def index(namespace,project,name,value): db.session.commit() v = Webvar.query.filter(Webvar.namespace == namespace).filter(Webvar.project == project).filter(Webvar.name == name).first() + if v is None: + return "Not Found", '404', {'Content-Type': 'text/plain; charset=utf-8'} return v.value, '200', {'Content-Type': 'text/plain; charset=utf-8'} if __name__ == "__main__": diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..43698d6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + webvars: + build: + context: . + image: git.br0tkasten.de/br0tkasten/webvars:latest + container_name: webvars + ports: + - "8080:80" + volumes: + - ./webvars.db:/app/webvars.db + restart: unless-stopped