|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +from dataset import connect |
| 7 | + |
| 8 | +from configurable_http_proxy.store import BaseStore |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class DatabaseStore(BaseStore): |
| 14 | + """A DBMS storage backend for configurable-http-proxy |
| 15 | +
|
| 16 | + This enables chp to run multiple times and serve routes from a central |
| 17 | + DBMS. It uses SQLAlchemy as the database backend. |
| 18 | +
|
| 19 | + Usage: |
| 20 | + Set the CHP_DATABASE_URL env var to any db URL supported by SQLAlchemy. |
| 21 | + The default is "sqlite://chp.sqlite". |
| 22 | +
|
| 23 | + $ export CHP_DATABASE_URL="sqlite:///chp.sqlite" |
| 24 | + $ configurable-http-proxy --storage-backend configurable_http_proxy.dbstore.DatabaseStore |
| 25 | +
|
| 26 | + Optionally you may set the table name by setting the CHP_DATABASE_TABLE. |
| 27 | + The default is 'chp_routes' |
| 28 | +
|
| 29 | + $ export CHP_DATABASE_TABLE="chp_routes" |
| 30 | +
|
| 31 | + See Also: |
| 32 | + * Valid URLs https://docs.sqlalchemy.org/en/14/core/engines.html#database-urls |
| 33 | + """ |
| 34 | + |
| 35 | + default_db_url = "sqlite:///chp.sqlite" |
| 36 | + default_db_table = "chp_routes" |
| 37 | + |
| 38 | + def __init__(self): |
| 39 | + super().__init__() |
| 40 | + db_url = os.environ.get("CHP_DATABASE_URL", self.default_db_url) |
| 41 | + db_table = os.environ.get("CHP_DATABASE_TABLE", self.default_db_table) |
| 42 | + self.routes: TableTrie = TableTrie(db_url, table=db_table) |
| 43 | + log.info(f"Using database {db_url}") |
| 44 | + for route, data in self.get_all().items(): |
| 45 | + log.info(f'Restoring {route} => {data.get("target", "<no target>")}') |
| 46 | + |
| 47 | + def clean(self): |
| 48 | + # remove all information stored so far |
| 49 | + self.routes.clean() |
| 50 | + |
| 51 | + def get_target(self, path: str): |
| 52 | + # return the data for the most specific matching route |
| 53 | + return self.routes.get(self.clean_path(path), trie=True) |
| 54 | + |
| 55 | + def get_all(self): |
| 56 | + # return all routes as route => data |
| 57 | + return self.routes.all() |
| 58 | + |
| 59 | + def add(self, path: str, data): |
| 60 | + # add a new route /path, storing data |
| 61 | + if self.get(path): |
| 62 | + self.update(path, data) |
| 63 | + else: |
| 64 | + self.routes.add(path, data) |
| 65 | + |
| 66 | + def update(self, path: str, data): |
| 67 | + # update an existing route |
| 68 | + self.routes.update(self.clean_path(path), data) |
| 69 | + |
| 70 | + def remove(self, path: str): |
| 71 | + # remove an existing route |
| 72 | + path = self.clean_path(path) |
| 73 | + route = self.routes.get(path) |
| 74 | + if route: |
| 75 | + self.routes.remove(path) |
| 76 | + return route |
| 77 | + |
| 78 | + def get(self, path): |
| 79 | + # return the data for the exact match |
| 80 | + return self.routes.get(self.clean_path(path)) |
| 81 | + |
| 82 | + |
| 83 | +class TableTrie: |
| 84 | + """A URLtrie-like backed by a database |
| 85 | +
|
| 86 | + This stores URL-path => data mappings. On retrieving, it will try |
| 87 | + to retrieve all subpaths up to the default path. |
| 88 | +
|
| 89 | + Usage: |
| 90 | +
|
| 91 | + # create mapping |
| 92 | + routes = TableTrie('sqlite:///:memory:') |
| 93 | + routes.add('/', {'some': 'default'}) |
| 94 | + routes.add('/foo/bar', {'some': 'value'}) |
| 95 | +
|
| 96 | + # query a mapping that exists |
| 97 | + routes.get('/foo/bar/baz') |
| 98 | + => { |
| 99 | + 'prefix': '/foo/bar', |
| 100 | + 'some': 'value' |
| 101 | + } |
| 102 | +
|
| 103 | + # query a mapping that does not exist |
| 104 | + routes.get('/fox/bax') |
| 105 | + => { |
| 106 | + 'prefix': '/', |
| 107 | + 'some': 'default' |
| 108 | + } |
| 109 | +
|
| 110 | + How values are stored: |
| 111 | +
|
| 112 | + Routes are stored in the given table (defaults to 'chp_routes'). |
| 113 | + The table has the following columns: |
| 114 | +
|
| 115 | + id: integer (primary key) |
| 116 | + key: varchar(128, unique) |
| 117 | + data: varchar |
| 118 | +
|
| 119 | + The data is the serialized JSON equivalent of the dictionary stored |
| 120 | + by TableTrie.add() or .update(). The rationale for storing a serialized |
| 121 | + version of the dict instead of using the sqlalchemy JSON support directly |
| 122 | + is to improve compatibility across db dialects. |
| 123 | +
|
| 124 | + DB backend: |
| 125 | +
|
| 126 | + The backend is any database supported by SQLAlchemy. To simplify |
| 127 | + implementation this uses the dataset library, which provides a very |
| 128 | + straight-forward way of working with tables created from Python dicts. |
| 129 | + """ |
| 130 | + |
| 131 | + def __init__(self, url, table=None): |
| 132 | + table = table or "chp_routes" |
| 133 | + self.db = connect(url) |
| 134 | + self.table = self.db[table] |
| 135 | + self.table.create_column("path", self.db.types.string(length=128), unique=True) |
| 136 | + |
| 137 | + def get(self, path, trie=False): |
| 138 | + # return the data store for path |
| 139 | + # -- if trie is False (default), will return data for the exact path |
| 140 | + # -- if trie is True, will return the data and the matching prefix |
| 141 | + try_routes = self._split_routes(path) if trie else [path] |
| 142 | + for path in try_routes: |
| 143 | + doc = self.table.find_one(path=path, order_by="id") |
| 144 | + if doc: |
| 145 | + if not trie: |
| 146 | + data = self._from_json(doc["data"]) |
| 147 | + else: |
| 148 | + data = doc |
| 149 | + data["data"] = self._from_json(doc["data"]) |
| 150 | + data["prefix"] = path |
| 151 | + break |
| 152 | + else: |
| 153 | + data = None |
| 154 | + return attrdict(data) if data else None |
| 155 | + |
| 156 | + def add(self, path, data): |
| 157 | + # add the data for the given exact path |
| 158 | + self.table.insert({"path": path, "data": self._to_json(data)}) |
| 159 | + |
| 160 | + def update(self, path, data): |
| 161 | + # update the data for the given exact path |
| 162 | + doc = self.table.find_one(path=path, order_by="id") |
| 163 | + doc["data"] = self._from_json(doc["data"]) |
| 164 | + doc["data"].update(data) |
| 165 | + doc["data"] = self._to_json(doc["data"]) |
| 166 | + self.table.update(doc, "id") |
| 167 | + |
| 168 | + def remove(self, path): |
| 169 | + # remove all matching routes for the given path, except default route |
| 170 | + for subpath in self._split_routes(path): |
| 171 | + if subpath == "/" and path != "/": |
| 172 | + continue |
| 173 | + self.table.delete(path=subpath) |
| 174 | + |
| 175 | + def all(self): |
| 176 | + # return all data for all paths |
| 177 | + return {item["path"]: self._from_json(item["data"]) for item in self.table.find(order_by="id")} |
| 178 | + |
| 179 | + def _to_json(self, data): |
| 180 | + # simple converter for serializable data |
| 181 | + for k, v in dict(data).items(): |
| 182 | + if isinstance(v, datetime): |
| 183 | + data[k] = f"_dt_:{v.isoformat()}" |
| 184 | + elif isinstance(v, dict): |
| 185 | + data[k] = self._to_json(v) |
| 186 | + return json.dumps(data) |
| 187 | + |
| 188 | + def _from_json(self, data): |
| 189 | + # simple converter from serialized data |
| 190 | + data = json.loads(data) if isinstance(data, (str, bytes)) else data |
| 191 | + for k, v in dict(data).items(): |
| 192 | + if isinstance(v, str) and v.startswith("_dt_:"): |
| 193 | + data[k] = datetime.fromisoformat(v.split(":", 1)[-1]) |
| 194 | + elif isinstance(v, dict): |
| 195 | + data[k] = self._from_json(v) |
| 196 | + return data |
| 197 | + |
| 198 | + def _split_routes(self, path): |
| 199 | + # generator for reverse tree of routes |
| 200 | + # e.g. /path/to/document |
| 201 | + # => yields /path/to/document, /path/to, /path, / |
| 202 | + levels = path.split("/") |
| 203 | + for i, e in enumerate(levels): |
| 204 | + yield "/".join(levels[: len(levels) - i + 1]) |
| 205 | + # always yield top level route |
| 206 | + yield "/" |
| 207 | + |
| 208 | + def clean(self): |
| 209 | + self.table.delete() |
| 210 | + |
| 211 | + |
| 212 | +class attrdict(dict): |
| 213 | + # enable .attribute for dicts |
| 214 | + def __init__(self, *args, **kwargs): |
| 215 | + super().__init__(*args, **kwargs) |
| 216 | + self.__dict__ = self |
0 commit comments