add dbpath arg

This commit is contained in:
G3G4X5X6 2023-08-30 11:07:55 +08:00
parent 6d472dc2b0
commit 61fda374ca
2 changed files with 10 additions and 4 deletions

View File

@ -111,6 +111,8 @@ class Database(object):
else:
self.cursor.execute(statement)
except sqlite3.OperationalError as ex:
if "already exists" in getSafeExString(ex):
break
if "locked" not in getSafeExString(ex):
raise
else:
@ -680,7 +682,7 @@ def version(token=None):
logger.debug("Fetched version (%s)" % ("admin" if is_admin(token) else request.remote_addr))
return jsonize({"success": True, "version": VERSION_STRING.split('/')[-1]})
def server(host=RESTAPI_DEFAULT_ADDRESS, port=RESTAPI_DEFAULT_PORT, adapter=RESTAPI_DEFAULT_ADAPTER, username=None, password=None):
def server(host=RESTAPI_DEFAULT_ADDRESS, port=RESTAPI_DEFAULT_PORT, adapter=RESTAPI_DEFAULT_ADAPTER, username=None, password=None, dbpath=None):
"""
REST-JSON API server
"""
@ -689,8 +691,11 @@ def server(host=RESTAPI_DEFAULT_ADDRESS, port=RESTAPI_DEFAULT_PORT, adapter=REST
DataStore.username = username
DataStore.password = password
_, Database.filepath = tempfile.mkstemp(prefix=MKSTEMP_PREFIX.IPC, text=False)
os.close(_)
if dbpath:
Database.filepath = dbpath
else:
_, Database.filepath = tempfile.mkstemp(prefix=MKSTEMP_PREFIX.IPC, text=False)
os.close(_)
if port == 0: # random
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:

View File

@ -60,11 +60,12 @@ def main():
apiparser.add_option("--adapter", help="Server (bottle) adapter to use (default \"%s\")" % RESTAPI_DEFAULT_ADAPTER, default=RESTAPI_DEFAULT_ADAPTER, action="store")
apiparser.add_option("--username", help="Basic authentication username (optional)", action="store")
apiparser.add_option("--password", help="Basic authentication password (optional)", action="store")
apiparser.add_option("--database", help="Database file path (optional)", action="store")
(args, _) = apiparser.parse_args()
# Start the client or the server
if args.server:
server(args.host, args.port, adapter=args.adapter, username=args.username, password=args.password)
server(args.host, args.port, adapter=args.adapter, username=args.username, password=args.password, dbpath=args.database)
elif args.client:
client(args.host, args.port, username=args.username, password=args.password)
else: