Hi all . It’s been a while!
I’m building a CLI for one of my apps. When the admin types the_cli init
, the app server fires up and the admin is prompted to set their username and password.
So far this works but I have a feeling there must be a better design.
Here’s how I’m doing it (forum references here and here):
# the_cli init will run this code
import time
from getpass import getpass
import anvil.server
from anvil.tables import app_tables
import bcrypt
import subprocess
subprocess.Popen(['anvil-app-server', '--app', 'MyApp', '--uplink-key', 'my_uplink_key'])
server_running = False
while not server_running:
try:
anvil.server.connect("my_uplink_key", url="ws://localhost:3030/_/uplink")
server_running = True
except:
print('Waiting for web server')
time.sleep(.5)
# prompt and set up the first user
email = input('Enter your email: ')
password = getpass('Enter your password: ')
new_user = app_tables.users.add_row(email=email, enabled=True)
password_hash = bcrypt.hashpw(bytes(password, encoding='ascii'), bcrypt.gensalt(16))
new_user['password_hash'] = password_hash.decode()
It seems tricky to deal with the subprocess and ugly to have a try
block nested in a while
loop but I couldn’t think of a better deign.
How would you design this kind of thing? Any small tips would be much appreciated.