Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Update dockerfile; prepare for CF workers
Vasek Mlejnsky09f094208c6a
diff --git a/api-service/.dockerignore b/api-service/.dockerignoreindex 1dafa3f6e..02f586c03 100644--- a/api-service/.dockerignore+++ b/api-service/.dockerignore@@ -7,3 +7,10 @@ __pycache__.pytest_cachedist.gitignore++./cf-worker-template/node_modules+./cf-worker-template/dist+./cf-worker-template/README.md+./cf-worker-template/.prettierrc+./cf-worker-template/.editorconfig+./cf-worker-template/.gitdiff --git a/api-service/Dockerfile b/api-service/Dockerfileindex 584304666..7965b40a7 100644--- a/api-service/Dockerfile+++ b/api-service/Dockerfile@@ -1,4 +1,4 @@-FROM python:3.10-slim+FROM nikolaik/python-nodejsENV PYTHONFAULTHANDLER=1 \PYTHONHASHSEED=random \@@ -19,6 +19,16 @@ RUN --mount=type=cache,target=/home/.cache/pypoetry/cache \--mount=type=cache,target=/home/.cache/pypoetry/artifacts \poetry install --without dev --no-root --no-ansi --no-interaction+# --mount requires DOCKER_BUILDKIT=1 -> didn't work locally for me (mlejva)+# RUN target=/home/.cache/pypoetry/cache \+# target=/home/.cache/pypoetry/artifacts \+# poetry install --without dev --no-root --no-ansi --no-interaction+COPY . ./++WORKDIR /app/cf-worker-template+RUN npm i --omit=dev++WORKDIR /appCMD exec poetry run gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 --timeout 0 app:appdiff --git a/api-service/app.py b/api-service/app.pyindex 07f05901b..dc03799b8 100644--- a/api-service/app.py+++ b/api-service/app.py@@ -13,69 +13,63 @@ def dump(obj):print("obj.%s = %r" % (attr, getattr(obj, attr)))-cf_email = os.environ["CLOUDFLARE_EMAIL"]-cf_api_key = os.environ["CLOUDFLARE_API_KEY"]+# cf_email = os.environ["CLOUDFLARE_EMAIL"]+# cf_api_key = os.environ["CLOUDFLARE_API_KEY"]# cf_api_cert = os.environ["CLOUDFLARE_API_CERTKEY"]# cf = CloudFlare.CloudFlare(# email=cf_email, key=cf_api_key, certtoken=cf_api_cert, debug=False# )-cf_token = os.environ.get("CLOUDFLARE_API_TOKEN")-cf = CloudFlare.CloudFlare(debug=True)+# cf_token = os.environ.get("CLOUDFLARE_API_TOKEN")+# cf = CloudFlare.CloudFlare(debug=True)-cf_acc_id = os.environ.get("CLOUDFLARE_ACC_ID")+# cf_acc_id = os.environ.get("CLOUDFLARE_ACC_ID")# ws = cf.accounts.workers.scripts.get(cf_acc_id)# print(ws)-data = {- "index.js": str.encode(- """export default {{- async fetch(request) {{- return new Response("Hello World!");- }},-}};"""- ),- "metadata": {"main_module": "index.js"},-}--js_code = str.encode(- """-module.exports = {{- async fetch(request) {{- return new Response("Hello World!", {{ headers: {{'Content-Type': 'text/html'}} }});- }},-}};-"""-)--script_name = "my_script_new"--cf_url = "https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/services/{script_name}/environments/production".format(- account_id=cf_acc_id, script_name=script_name-)-headers = {- "X-Auth-Email": cf_email,- "X-Auth-Key": cf_api_key,-}+# js_code = str.encode(+# """+# module.exports = {{+# async fetch(request) {{+# return new Response("Hello World!", {{ headers: {{'Content-Type': 'text/html'}} }});+# }},+# }};+# """+# )++# script_name = "my_script_new"++# cf_url = "https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/services/{script_name}/environments/production".format(+# account_id=cf_acc_id, script_name=script_name+# )+# headers = {+# "X-Auth-Email": cf_email,+# "X-Auth-Key": cf_api_key,+# }# >>>>>>>>> THIS WORKS-response = requests.put(- cf_url,- headers=headers,- files={- "worker.js": (- "worker.js",- open("worker.js", "rb"),- "application/javascript+module",- ),- "metadata": (- None,- str.encode('{"bindings": [], "main_module": "worker.js"}'),- "application/json",- ),- },-)+# response = requests.put(+# cf_url,+# headers=headers,+# files={+# "worker.js": (+# "worker.js",+# open("worker.js", "rb"),+# "application/javascript+module",+# ),+# "package.json": (+# "package.json",+# open("package.json", "rb"),+# "application/json",+# ),+# "metadata": (+# None,+# str.encode('{"bindings": [], "main_module": "worker.js"}'),+# "application/json",+# ),+# },+# )# response = requests.put(# cf_url,# headers=headers,@@ -89,31 +83,31 @@ response = requests.put(# )# },# )-print("RESPONSE 1")-print(response.request.headers)-print(response.content)-print(response.json())+# print("RESPONSE 1")+# print(response.request.headers)+# print(response.content)+# print(response.json())# Enable subdomain, eg: https://my_script.devbook.workers.dev/-time.sleep(5)-zone_id = "72aff3fa4f548f5fc3bbcc223037f444"-cf_url = "https://api.cloudflare.com/client/v4/zones/{zone_id}/workers/routes".format(- zone_id=zone_id-)-print(cf_url)-response = requests.post(- cf_url,- headers=headers,- json={- "pattern": "{script_name}.devbook.workers.dev".format(script_name=script_name),- "script": script_name,- },-)-# response = requests.post(cf_url, headers=headers, json={"enabled": "true"})-print("RESPONSE 2")-print(response.request.headers)-# print(response.content)-print(response.json())+# time.sleep(5)+# zone_id = "72aff3fa4f548f5fc3bbcc223037f444"+# cf_url = "https://api.cloudflare.com/client/v4/zones/{zone_id}/workers/routes".format(+# zone_id=zone_id+# )+# print(cf_url)+# response = requests.post(+# cf_url,+# headers=headers,+# json={+# "pattern": "{script_name}.devbook.workers.dev".format(script_name=script_name),+# "script": script_name,+# },+# )+# # response = requests.post(cf_url, headers=headers, json={"enabled": "true"})+# print("RESPONSE 2")+# print(response.request.headers)+# # print(response.content)+# print(response.json())app = Flask(__name__)diff --git a/api-service/cf-worker-template/.editorconfig b/api-service/cf-worker-template/.editorconfignew file mode 100644index 000000000..64ab2601f--- /dev/null+++ b/api-service/cf-worker-template/.editorconfig@@ -0,0 +1,13 @@+# http://editorconfig.org+root = true++[*]+indent_style = tab+tab_width = 2+end_of_line = lf+charset = utf-8+trim_trailing_whitespace = true+insert_final_newline = true++[*.yml]+indent_style = spacediff --git a/api-service/cf-worker-template/.gitignore b/api-service/cf-worker-template/.gitignorenew file mode 100644index 000000000..f11596098--- /dev/null+++ b/api-service/cf-worker-template/.gitignore@@ -0,0 +1,7 @@+.DS_Store+/node_modules+*-lock.*+*.lock+*.log++/distdiff --git a/api-service/cf-worker-template/.prettierrc b/api-service/cf-worker-template/.prettierrcnew file mode 100644index 000000000..557d4d5a7--- /dev/null+++ b/api-service/cf-worker-template/.prettierrc@@ -0,0 +1,19 @@+{+ "semi": true,+ "printWidth": 100,+ "singleQuote": true,+ "bracketSpacing": true,+ "insertPragma": false,+ "requirePragma": false,+ "jsxSingleQuote": false,+ "bracketSameLine": false,+ "embeddedLanguageFormatting": "auto",+ "htmlWhitespaceSensitivity": "css",+ "vueIndentScriptAndStyle": true,+ "quoteProps": "consistent",+ "proseWrap": "preserve",+ "trailingComma": "es5",+ "arrowParens": "avoid",+ "useTabs": true,+ "tabWidth": 2+}diff --git a/api-service/cf-worker-template/README.md b/api-service/cf-worker-template/README.mdnew file mode 100644index 000000000..74029076a--- /dev/null+++ b/api-service/cf-worker-template/README.md@@ -0,0 +1,19 @@+# Template: worker++[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/worker)++A simple template for kick starting a Cloudflare worker project.++## Setup++To create a `my-project` directory using this template, run:++```sh+$ npm init cloudflare my-project worker+# or+$ yarn create cloudflare my-project worker+# or+$ pnpm create cloudflare my-project worker+```++> **Note:** Each command invokes [`create-cloudflare`](https://www.npmjs.com/package/create-cloudflare) for project creation.diff --git a/api-service/cf-worker-template/index.js b/api-service/cf-worker-template/index.jsnew file mode 100644index 000000000..095a717c5--- /dev/null+++ b/api-service/cf-worker-template/index.js@@ -0,0 +1,37 @@+import { Router } from 'itty-router'+++const router = Router()++async function handlepostRequest(request) {+ // Get the email from the request JSON payload+ const b = await request.json()+ const email = b.email++ // Check if the email is valid+ const isValidEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)++ // If the email isn't valid, respond with JSON containing the error message+ if (!isValidEmail) {+ return new Response(JSON.stringify({+ error: 'Invalid email'+ }), {+ status: 400+ })+ }++ // If the email is valid respond with JSON and payload containing "Ok"+ return new Response(JSON.stringify({+ message: 'Ok'+ }), {+ status: 200+ })+}++router.post('/', handlepostRequest)+router.get('*', () => new Response('Not found', { status: 404 }))++export default {+ fetch: router.handle+}+diff --git a/api-service/cf-worker-template/package.json b/api-service/cf-worker-template/package.jsonnew file mode 100644index 000000000..93b0a1762--- /dev/null+++ b/api-service/cf-worker-template/package.json@@ -0,0 +1,16 @@+{+ "private": true,+ "version": "0.0.0",+ "scripts": {+ "deploy": "wrangler publish index.js",+ "dev": "wrangler dev index.js --local",+ "test": "vitest"+ },+ "devDependencies": {+ "vitest": "^0.24.5"+ },+ "dependencies": {+ "wrangler": "^2.12.2",+ "itty-router": "^3.0.11"+ }+}\ No newline at end of filediff --git a/api-service/cf-worker-template/wrangler.toml b/api-service/cf-worker-template/wrangler.tomlnew file mode 100644index 000000000..f45b719d7--- /dev/null+++ b/api-service/cf-worker-template/wrangler.toml@@ -0,0 +1,5 @@+name = "cf-worker-template" # todo+main = "./index.js"+# route = ''+compatibility_date = "2022-05-03"+send_metrics = falsediff --git a/api-service/worker.js b/api-service/worker.jsdeleted file mode 100644index 19370b22f..000000000--- a/api-service/worker.js+++ /dev/null@@ -1,5 +0,0 @@-export default {- async fetch(request, env) {- return new Response('Hello world 4')- }-}\ No newline at end of file