From 93cfb3dfac2069943ea137f83de73f9710360a6a Mon Sep 17 00:00:00 2001 From: Nadav Tasher Date: Tue, 2 Apr 2024 21:42:55 +0300 Subject: [PATCH] Added an example --- Makefile | 3 +- .../001-simple-clicker/application/Dockerfile | 9 +++ .../configurations/entrypoint.conf | 2 + .../application/configurations/nginx.conf | 0 .../application/src/backend/app.py | 34 ++++++++++ .../src/frontend/application/application.css | 21 +++++++ .../src/frontend/application/application.js | 24 +++++++ .../application/src/frontend/index.html | 62 +++++++++++++++++++ .../001-simple-clicker/docker-compose.yaml | 16 +++++ 9 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 examples/001-simple-clicker/application/Dockerfile create mode 100644 examples/001-simple-clicker/application/configurations/entrypoint.conf create mode 100644 examples/001-simple-clicker/application/configurations/nginx.conf create mode 100644 examples/001-simple-clicker/application/src/backend/app.py create mode 100644 examples/001-simple-clicker/application/src/frontend/application/application.css create mode 100644 examples/001-simple-clicker/application/src/frontend/application/application.js create mode 100644 examples/001-simple-clicker/application/src/frontend/index.html create mode 100644 examples/001-simple-clicker/docker-compose.yaml diff --git a/Makefile b/Makefile index 107307e..60baeff 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ DOCKER ?= $(shell which docker) IMAGE_PATH := image BUNDLE_PATH := bundle +EXAMPLES_PATH := examples BACKEND_PATH := $(IMAGE_PATH)/src/backend FRONTEND_PATH := $(IMAGE_PATH)/src/frontend @@ -26,7 +27,7 @@ IMAGE_SOURCES := $(shell find $(IMAGE_PATH) -type f) prerequisites: $(PYTHON) -m pip install jinja2-cli yapf -format: $(wildcard $(BACKEND_PATH)/*.py) $(ENTRYPOINT_PATH) | prerequisites +format: $(wildcard $(BACKEND_PATH)/*.py) $(ENTRYPOINT_PATH) $(wildcard $(EXAMPLES_PATH)/*/application/src/backend/*.py) | prerequisites $(PYTHON) -m yapf -i $^ --style "{based_on_style: google, column_limit: 400, indent_width: 4}" image: $(IMAGE_PATH)/Dockerfile-$(IMAGE_TAG) | format $(IMAGE_SOURCES) diff --git a/examples/001-simple-clicker/application/Dockerfile b/examples/001-simple-clicker/application/Dockerfile new file mode 100644 index 0000000..df45181 --- /dev/null +++ b/examples/001-simple-clicker/application/Dockerfile @@ -0,0 +1,9 @@ +# Select the base image +FROM webhood/3.8 + +# Copy configurations +COPY configurations/nginx.conf /etc/nginx/conf.d/nginx.conf +COPY configurations/entrypoint.conf /etc/entrypoint/conf.d/entrypoint.conf + +# Copy sources +COPY src /application \ No newline at end of file diff --git a/examples/001-simple-clicker/application/configurations/entrypoint.conf b/examples/001-simple-clicker/application/configurations/entrypoint.conf new file mode 100644 index 0000000..760e177 --- /dev/null +++ b/examples/001-simple-clicker/application/configurations/entrypoint.conf @@ -0,0 +1,2 @@ +[worker] +replication=1 diff --git a/examples/001-simple-clicker/application/configurations/nginx.conf b/examples/001-simple-clicker/application/configurations/nginx.conf new file mode 100644 index 0000000..e69de29 diff --git a/examples/001-simple-clicker/application/src/backend/app.py b/examples/001-simple-clicker/application/src/backend/app.py new file mode 100644 index 0000000..2ff3392 --- /dev/null +++ b/examples/001-simple-clicker/application/src/backend/app.py @@ -0,0 +1,34 @@ +import time + +# Import utilities +from fsdicts import * +from runtypes import * +from guardify import * + +# Import the router +from router import router, initialize + +# Initialize value storage +DATABASE = localdict("/opt/database") + +# Set defaults +DATABASE.setdefaults(count=0, timestamp=0) + + +@router.post("/api/count") +def count_request(): + return DATABASE.count + + +@router.post("/api/advance") +def advance_request(): + # Make sure enough time has passed + assert time.time() - DATABASE.timestamp > 10, "Must wait 10 seconds between clicks" + + # Advance the value + DATABASE.count += 1 + DATABASE.timestamp = time.time() + + +# Initialize the application +app = initialize() diff --git a/examples/001-simple-clicker/application/src/frontend/application/application.css b/examples/001-simple-clicker/application/src/frontend/application/application.css new file mode 100644 index 0000000..543a541 --- /dev/null +++ b/examples/001-simple-clicker/application/src/frontend/application/application.css @@ -0,0 +1,21 @@ +@media (prefers-color-scheme: dark) { + :root { + --text: #ffffff; + --theme: #0f172a; + --active: #7674af; + --passive: #1e293b; + } +} + +@media (prefers-color-scheme: light) { + :root { + --text: #707070; + --theme: #ffffff; + --active: #c0c0c0; + --passive: #f0f0f0; + } +} + +body { + max-width: 500px; +} diff --git a/examples/001-simple-clicker/application/src/frontend/application/application.js b/examples/001-simple-clicker/application/src/frontend/application/application.js new file mode 100644 index 0000000..2060279 --- /dev/null +++ b/examples/001-simple-clicker/application/src/frontend/application/application.js @@ -0,0 +1,24 @@ +window.addEventListener("load", async function () { + await update(); +}); + +async function update() { + // Get current count from server + const currentClicks = await call("count"); + + // Update UI + $("#count").write(`Clicks: ${currentClicks}`); +} + +async function advance() { + try { + // Try advancing the counter + await call("advance"); + + // Update UI + await update(); + } catch (error) { + // Display the error + await alertDialog(error); + } +} diff --git a/examples/001-simple-clicker/application/src/frontend/index.html b/examples/001-simple-clicker/application/src/frontend/index.html new file mode 100644 index 0000000..04e9e6f --- /dev/null +++ b/examples/001-simple-clicker/application/src/frontend/index.html @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Simple Clicker + + + + + + + + + +

Loading...

+ + + + + \ No newline at end of file diff --git a/examples/001-simple-clicker/docker-compose.yaml b/examples/001-simple-clicker/docker-compose.yaml new file mode 100644 index 0000000..ccde772 --- /dev/null +++ b/examples/001-simple-clicker/docker-compose.yaml @@ -0,0 +1,16 @@ +version: "3" + +# Configure applcation service +services: + application: + build: application + restart: unless-stopped + ports: + - 80:80 + - 443:443 + volumes: + - data:/opt + +# Configure persistent data volume +volumes: + data: \ No newline at end of file