-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
68 lines (49 loc) · 1.74 KB
/
web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Built-in imports
from time import sleep
import threading
# Relative imports
from src.repo_harvester import load_repos_from_config, log
from src.repo_harvester.linkeddata import add_repos_to_triplestore
from src.microservice import get_template
import src.microservice
# Package imports
from flask import stream_with_context, Response
"""
Entrypoint for the repo-harvester:
- Defines endpoints
- Defines repo & image sources
app should not be defined here, as this is handled by mu-python-template
"""
@app.route("/")
def index():
"""Simple status page to check if the repo harvester works"""
return get_template()
@app.route("/update", methods=["GET", "POST"])
def update():
"""Calls add_repos_to_triplestore with init, initialising the database"""
src.microservice.listening = True
thread = threading.Thread(target=run)
thread.start()
return "Init..."
return add_repos(init=True)
def run():
repos = load_repos_from_config()
add_repos_to_triplestore(repos, True)
add_repos_to_triplestore(repos, False)
src.microservice.listening = False
def add_repos(init=False):
src.microservice.listening = True
repos = load_repos_from_config()
thread = threading.Thread(target=run, args=(repos, init,))
thread.start()
return "Updating..."
@app.route("/listen", methods=["GET", "POST"])
def listen():
"""Calls add_repos_to_triplestore without init, updating the database"""
def generator():
sleep(2) # Give the request some time
while src.microservice.listening:
sleep(0.5)
yield src.microservice.send_to_html
src.microservice.send_to_html = ""
return Response(stream_with_context(generator()), content_type="text/event-stream")