simplest flask server

This commit is contained in:
Andreas Balogh
2021-02-16 15:44:39 +01:00
parent 00a89222be
commit c1d80cabbd
14 changed files with 11 additions and 239 deletions

View File

@@ -1,58 +0,0 @@
build_zeo:
stage: build
tags:
- shell
- ubuntu
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE/zeo:$CI_COMMIT_REF_SLUG
RELEASE_TAG: $CI_REGISTRY_IMAGE/zeo:latest
script:
- cd src/zeo
- docker build -t $IMAGE_TAG .
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker push $IMAGE_TAG
build_flask:
stage: build
tags:
- shell
- ubuntu
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE/webui:$CI_COMMIT_REF_SLUG
RELEASE_TAG: $CI_REGISTRY_IMAGE/webui:latest
script:
- cd src/webui
- docker build -t $IMAGE_TAG .
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker push $IMAGE_TAG
build_tester:
stage: build
tags:
- shell
- ubuntu
script:
- echo "Build api image"
- export
# https://gitlab.com/gitlab-org/gitlab-runner/-/issues/26785
unit_test:
stage: test
services:
- name: $CI_REGISTRY_IMAGE/zeo:$CI_COMMIT_REF_SLUG
alias: zeo
- name: $CI_REGISTRY_IMAGE/webui:$CI_COMMIT_REF_SLUG
alias: webui
image:
name: $CI_REGISTRY_IMAGE/webui:$CI_COMMIT_REF_SLUG
entrypoint: [""]
tags:
- kubernetes
artifacts:
paths:
- var/
script:
- '[ -d var ] || mkdir var'
- cd var
- wget webui:5000 -o wget.log
- echo "Done."

View File

@@ -1,2 +0,0 @@
eclipse.preferences.version=1
encoding//src/webui/flaskr/zcm.py=utf8

View File

@@ -1,3 +1,3 @@
# scraper
# Kubernetes AutoDevOps example
Ripper worming through blogs for videos, images, text.
Example project to test gitlab CI, Docker, Kubernetes and AutoDevOps integration.

View File

@@ -1,26 +0,0 @@
import time
from flask import Flask
import redis
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
def get_hit_count():
retries = 5
while True:
try:
return cache.incr('hits')
except redis.exceptions.ConnectionError as exc:
if retries == 0:
raise exc
retries -= 1
time.sleep(0.5)
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)

View File

@@ -1,94 +0,0 @@
# Copyright (c) 2016 Andreas
# See LICENSE for details.
""" ZODB context manager """
# from BTrees.IOBTree import IOBTree
# from BTrees.OOBTree import OOBTree
# from persistent import Persistent
# from persistent.list import PersistentList as PList
# from persistent.mapping import PersistentMapping as PDict
from ZEO.ClientStorage import ClientStorage
from ZODB import DB
from ZODB.FileStorage import FileStorage
class ZDatabase():
""" Provides a ZODB database context manager """
def __init__(self, uri, **kwargs):
self.storage = create_storage(uri)
self.db = DB(self.storage, **kwargs)
def __enter__(self):
return self.db
def __exit__(self, exc_type, exc_value, traceback):
self.db.close()
return False
class ZConnection():
""" Provides a ZODB connection with auto-abort (default).
Provides a tuple of connection and root object:
with ZConnection(db) as (cx, root):
root.one = "ok"
ZConnection implements a connection context manager.
Transaction context managers in contrast do auto-commit:
a) with db.transaction() as connection, or
b) with cx.transaction_manager as transaction, or
c) with transaction.manager as transaction (for the thread-local transaction manager)
See also http://www.zodb.org/en/latest/guide/transactions-and-threading.html
"""
def __init__(self, db, auto_commit=False, transaction_manager=None):
self.db = db
self.auto_commit = auto_commit
self.transaction_manager = transaction_manager
self.cx = None
def __enter__(self):
if self.transaction_manager:
self.cx = self.db.open(self.transaction_manager)
else:
self.cx = self.db.open()
return self.cx, self.cx.root()
def __exit__(self, exc_type, exc_value, traceback):
if self.auto_commit:
self.cx.transaction_manager.commit()
self.cx.close()
return False
def create_storage(uri):
""" supported URIs
file://e:/workspaces/zeo/bots.fs
zeo://localhost:8001
e:/workspaces/zeo/bots.fs
@see https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
"""
if uri.startswith("file://"):
storage = FileStorage(uri[7:])
elif uri.startswith("zeo://"):
addr, port = uri[6:].split(":")
# addr_ = addr.encode("ASCII")
storage = ClientStorage((addr, int(port)))
else:
storage = FileStorage(uri)
return storage
def database(uri):
""" convenience function for single thread, return one connection from the pool """
storage = create_storage(uri)
db = DB(storage)
return db
def connection(db):
""" Convenience function for multi thread, returns
connection, transaction manager and root
"""
cx = db.open()
return cx, cx.root()

View File

@@ -1,27 +0,0 @@
from ZEO.ClientStorage import ClientStorage
from ZODB import DB
import socket
from flask import Flask
app = Flask(__name__)
ipv4 = socket.gethostbyname("zeo")
storage = ClientStorage((ipv4, 8100))
db = DB(storage)
cx = db.open()
def get_hit_count():
root = cx.root()
if "hits" not in root:
root['hits'] = 0
hits = root['hits']
root['hits'] = hits + 1
cx.transaction_manager.commit()
return hits
@app.route('/')
def hello():
count = get_hit_count()
return 'Hello World! I have been seen {} times.\n'.format(count)

View File

@@ -1,12 +0,0 @@
FROM python:3.9-slim
RUN apt-get update && apt-get install -y \
gcc \
&& rm -rf /var/lib/apt/lists/*
RUN pip install ZODB==5.6.0
RUN pip install ZEO==5.2.1
COPY ./zeo.conf /etc/zeo.conf
COPY ./run.sh /run.sh
RUN chmod +x /run.sh
VOLUME ["/var/zeo/fs", "/var/zeo/blobs"]
EXPOSE 8100
CMD /run.sh

View File

@@ -1,9 +0,0 @@
#!/bin/sh
# We need to add a empty Data.fs if it does not exist
# as zeo will not start without on.
if [ ! -f /var/zeo/fs/Data.fs ]; then
touch /var/zeo/fs/Data.fs
fi
exec /usr/local/bin/runzeo -a 8100 -C /etc/zeo.conf

View File

@@ -1,9 +0,0 @@
<zeo>
address 8100
read-only false
</zeo>
<filestorage>
path /var/zeo/fs/Data.fs
blob-dir /var/zeo/blobs
</filestorage>

8
webui/flaskr/hello.py Normal file
View File

@@ -0,0 +1,8 @@
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello World!\n'

1
webui/requirements.txt Normal file
View File

@@ -0,0 +1 @@
Flask