To migrate from legacy versions of WebHost (Or Guavacado 1.X) to Guavacado 2.0, the following considerations must be made:
guavacado.WebHost.start_service()
no longer blocks indefinitely. Instead, it spawns a new thread for each port that is open, then returns. If your app previously relied onguavacado.WebHost.start_service()
blocking indefinitely, you can callguavacado.wait_for_keyboardinterrupt()
to efficiently block until Ctrl+C is pressed in the terminal.- Please note that while
guavacado.WebInterface
can still be inherited, it is now encouraged to instead instantiate it as a member variable of your class, as follows:
class MyInterface(object):
def __init__(self):
self.host = guavacado.WebHost() # note this will be automatically instantiated in WebInterface if not passed in as an argument
self.host.add_addr()
self.interface = guavacado.WebInterface(host=self.host)
self.interface.connect('/test/',self.GET_TEST,'GET')
def GET_TEST(self):
return "Test"
guavacado.WebInterface
now requires the__init__()
constructor to run. If your class inherits from guavacado.WebInterface, make sure that it callsWebInterface.__init__()
in its own__init__()
method. An example inherited implementation follows:
class MyInterface(guavacado.WebInterface):
def __init__(self):
self.host = guavacado.WebHost() # note this will be automatically instantiated in WebInterface if not set here before calling __init__()
self.host.add_addr()
guavacado.WebInterface.__init__(self)
self.connect('/test/',self.GET_TEST,'GET')
def GET_TEST(self):
return "Test"
- The
addr
andport
arguments toguavacado.WebHost
's constructor have now been removed. This functionality is now replaced by theadd_addr()
method ofguavacado.WebHost
, which accepts the same optional arguments, along with an optionalTLS
argument to provide a tuple of the filenames of the certfile and keyfile for HTTPS. While omitting the call toadd_addr()
will default to HTTP on port 80, this will result in a warning, and it is discouraged to rely on this default functionality. - Guavacado 2.0 now supports HTTPS (HTTP through TLS) and automatic redirection from HTTP to HTTPS. An HTTPS interface can be implemented with
add_addr(port=443, TLS=('TLS_keys/certfile.crt', 'TLS_keys/keyfile.key'))
after creating a certificate and key, stored inTLS_keys/certfile.crt
andTLS_keys/keyfile.key
, respectively. Self-signed keys can be generated by the scripttls_keygen.sh
, or a signed key for a public server can be obtained from a certificate authority, such as Let's Encrypt, that is trusted by most browsers. When using HTTPS, you can also easily redirect HTTP traffic to the equivalent HTTPS page withadd_addr(port=80, disp_type=('redirect', 'https://domain/'))
, (withdomain
replaced with your domain name). guavacado.WebHost
no longer hosts the 'static
' directory by default. You must now manually instantiateguavacado.WebFileInterface
to host a directory. This allows for implementation of REST APIs without enabling direct file access when that feature is not needed. Example "files only" implementation:
host = guavacado.WebHost(loglevel='INFO')
host.add_addr(port=80, disp_type=('redirect', 'https://domain/'))
host.add_addr(port=443, TLS=('TLS_keys/certfile.crt', 'TLS_keys/keyfile.key'))
files = guavacado.WebFileInterface(host)
host.start_service()
wait_for_keyboardinterrupt()
host.stop_service()