Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple web apps in one deployment #52

Open
ccrighton opened this issue Jun 8, 2023 · 2 comments
Open

Support for multiple web apps in one deployment #52

ccrighton opened this issue Jun 8, 2023 · 2 comments

Comments

@ccrighton
Copy link

Hi,

I have an application that has two web apps. One for the initial AP configuration of the device and the second for the ongoing maintenance. Unfortunately, with phew the decorators store the routes in a global.

Phew could support this capability if the routes were stored as instance variables of a Phew server class. You'll see the similarity to Flask. The following tweak of the basic example demonstrates the idea:

from phew import server, connect_to_wifi

connect_to_wifi("<ssid>", "<password>")

webapp = server.Phew()

@webapp.route("/random", methods=["GET"])
def random_number(request):
  import random
  min = int(request.query.get("min", 0))
  max = int(request.query.get("max", 100))
  return str(random.randint(min, max))

@webapp.catchall()
def catchall(request):
  return "Not found", 404

webapp.run()

It would still be possible to not break existing apps by using a default instance of Phew in the case that the existing methods are called. In the implementation the server.route method etc. would apply to a default Phew instance stored in a server.py global variable.

@ccrighton
Copy link
Author

ccrighton commented Jun 8, 2023

@ccrighton
Copy link
Author

Currently, the run method takes control of the asyncio loop. That means nothing else can be added as a task. As a result, it's not possible to run anything but a web app. I've introduced a run_as_task method that doesn't ceed control of the loop. That means that Phew can be run alongside other functions such as an mqtt client, interrupt handlers and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant