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

httpserver: Add support for WSGI apps #1521

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

httpserver: Add support for WSGI apps #1521

wants to merge 3 commits into from

Conversation

progval
Copy link
Owner

@progval progval commented Oct 31, 2022

For example, with Flask:

from supybot import utils, plugins, ircutils, callbacks, httpserver
from supybot.commands import *
from supybot.i18n import PluginInternationalization

_ = PluginInternationalization('TestWsgi')

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route('/login/', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] == 'root' \
                and request.form['password'] == 'admin':
            return 'Hello root'
        else:
            return 'Error: Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return '''
        <form method="POST" action=".">
            <label for="username">Username:
                <input name="username" id="username" />
            </label>
            <label for="password">Password:
                <input name="password" id="password" type="password" />
            </label>
            <input type="Submit" />
        </form>
    '''

class TestWsgi(callbacks.Plugin):
    """Test Flask"""
    def __init__(self, irc):
        self.__parent = super(TestWsgi, self)
        callbacks.Plugin.__init__(self, irc)

        httpserver.hook('testwsgi', app)

    def die(self):
        self.__parent.die()
        httpserver.unhook('testwsgi')

Class = TestWsgi

For example, with Flask:

```
from supybot import utils, plugins, ircutils, callbacks, httpserver
from supybot.commands import *
from supybot.i18n import PluginInternationalization

_ = PluginInternationalization('TestWsgi')

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route('/login/', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] == 'root' \
                and request.form['password'] == 'admin':
            return 'Hello root'
        else:
            return 'Error: Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return '''
        <form method="POST" action=".">
            <label for="username">Username:
                <input name="username" id="username" />
            </label>
            <label for="password">Password:
                <input name="password" id="password" type="password" />
            </label>
            <input type="Submit" />
        </form>
    '''

class TestWsgi(callbacks.Plugin):
    """Test Flask"""
    def __init__(self, irc):
        self.__parent = super(TestWsgi, self)
        callbacks.Plugin.__init__(self, irc)

        httpserver.hook('testwsgi', app)

    def die(self):
        self.__parent.die()
        httpserver.unhook('testwsgi')

Class = TestWsgi
```
@progval progval marked this pull request as draft October 31, 2022 21:14
@progval progval changed the base branch from testing to master May 5, 2024 15:52
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

Successfully merging this pull request may close these issues.

1 participant