aboutsummaryrefslogtreecommitdiffstats
path: root/browser/app.py
blob: 1398e931752884f147ff81936ed3725cb206fbe9 (plain)
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
68
69
from dotenv import load_dotenv
load_dotenv()

import os
import sys
import traceback
import tempfile
from flask import Flask, render_template
from flask_compress import Compress # type: ignore

from log_db import DB, Bound

app = Flask(__name__)
Compress(app)

BASE_URL = os.environ["BASE_URL"]
DB_DIR = os.environ["DB_DIR"] if "DB_DIR" in os.environ else tempfile.TemporaryDirectory().name

print("""\n"""
      f"""BASE_URL:      {BASE_URL}\n"""
      f"""DB_DIR:        {DB_DIR}\n""")

db = DB \
    .configure() \
    .data_dir(DB_DIR) \
    .fields(["id", "name"]) \
    .primary_key("id") \
    .secondary_keys(["name"]) \
    .initialize()

def error_page(message: str, code: int = 400):
    return render_template("error.html.j2", error = message), code

@app.errorhandler(404)
def not_found(e: Exception):
    return error_page("Not found", 404)

@app.errorhandler(405)
def method_not_allowed(e: Exception):
    return error_page(str(e), 405)

@app.errorhandler(Exception)
def error_handler(e: Exception):
    traceback.print_exception(e, file=sys.stderr)
    return error_page("Internal server error", 500)

@app.get("/")
def index():
    return page_find()

@app.get("/find")
def page_find():
    rows = db.range_by("id", Bound.unbounded(), Bound.unbounded(), limit=100)

    return render_template('index.html.j2',
        selected_form = "form_find.html.j2",
        field_names = ["id", "name"],
        rows = rows,
    )

@app.get("/range")
def page_range():
    rows = db.range_by("id", Bound.unbounded(), Bound.unbounded(), limit=100)

    return render_template('index.html.j2',
        selected_form = "form_range.html.j2",
        field_names = ["id", "name"],
        rows = rows,
    )