Мы реализовали бэкэнд «два в одном» с Flask. Для самого сайта (который мы называем tekid
) у нас нет проблем. Но для портала управления (который мы называем tekid-admin
), даже при использовании одних и тех же реестров представления, сервер WSGI (dev и prod) возвращает 404 NOT FOUND для всех запросов.
Связанные коды
Записи CLI зарегистрированы как
tekid
-> tekid.cli.www:cli
tekid-admin
-> tekid.cli.adm:cli
Ниже приведен код, который мы использовали для хранения объекта приложения Flask:
# -*- coding: utf-8 -*-
"""Module entrypoint of TekID website."""
# in `tekid/app/www.py` for `tekid`
from tekid.urls.www import app
# in `tekid/app/adm.py` for `tekid-admin`
from tekid.urls.adm import app
Ниже приведен код, который мы использовали для ввода CLI:
# -*- coding: utf-8 -*-
"""CLI for TekID website."""
import click
import flask
import tekid.core.typing as typing
# in `tekid/cli/www.py` for `tekid`
from tekid.app.www import app
# in `tekid/cli/adm.py` for `tekid-admin`
from tekid.app.adm import app
__all__ = ['cli']
@click.group(cls=flask.cli.FlaskGroup, create_app=lambda: app)
def cli() -> typing.NoneType:
"""Management script for the TekID website."""
Ниже приведен кодмы использовали для маршрутизации реестра (мы используем механизм централизованной регистрации, предложенный самим Flask):
# tekid/urls/www.py & tekid/urls/adm.py (same when testing)
# -*- coding: utf-8 -*-
"""URL routing for TekID website."""
# pylint: disable=wrong-import-position
from tekid.core.macro import FLASK as app
__all__ = ['app']
###############################################################################
# load HTML pages
from tekid.urls.pages import * # pylint: disable=unused-wildcard-import
# index.html
app.add_url_rule('/', view_func=load_index)
... # same routing registry codes
Ожидаемое поведение
NB: tekid
и tekid-admin
должен выдать тот же вывод
Команда routes
выдаст примерно так:
$ tekid routes
# or
$ tekid-admin routes
Endpoint Methods Rule
-------------- ----------------- -----------------------------------
load_contact GET, POST /contact/
load_expertise GET /expertise/
load_index GET /
load_news GET, POST /news/
...
static GET /static/<path:filename>
Мы попробуем curl http://127.0.0.1:5000/
после run
на сайте (tekid
):
$ tekid run
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 332-955-135
/fakepath/.venv/lib/python3.7/site-packages/flask/sessions.py:220: UserWarning: The session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead.
"The session cookie domain is an IP address. This may not work"
127.0.0.1 - - [07/Nov/2019 17:20:03] "GET / HTTP/1.1" 200 -
$ curl http://127.0.0.1:5000
<!--
_______ _ _____ _____ _ _ _
|__ __| | | |_ _| __ \ | | | | | |
| | ___| | __ | | | | | | | | | |_ __| |
| |/ _ \ |/ / | | | | | | | | | __/ _` |
| | __/ < _| |_| |__| | | |___| || (_| |_
|_|\___|_|\_\_____|_____/ |______\__\__,_(_)
-->
... (the actual HTML page)
Фактическое поведение
С теми же настройками, что и выше, мы пробуем curl http://127.0.0.1:5000/
после run
на портале управления (tekid-admin
):
$ tekid-admin run
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: ***-***-***
/fakepath/.venv/lib/python3.7/site-packages/flask/sessions.py:220: UserWarning: The session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead.
"The session cookie domain is an IP address. This may not work"
127.0.0.1 - - [07/Nov/2019 17:06:25] "GET / HTTP/1.1" 404 -
$ curl http://127.0.0.1:5000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
Среда
- Версия Python: 3.7.4
- Версия колбы: 1.1.1
- Версия Werkzeug: 0.16.0