Skip to content
This repository has been archived by the owner on Dec 25, 2019. It is now read-only.

Commit

Permalink
Add a UWSGI app and a flask app
Browse files Browse the repository at this point in the history
  • Loading branch information
NyanKiyoshi committed Sep 16, 2018
1 parent fcc019e commit c55197e
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 8 deletions.
60 changes: 60 additions & 0 deletions .ci/test_servers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""We want to ensure that users can run Texrrow through UWSGI without issues.
This script is ran by Travis and AppVeyor to ensure the compatibility.
This is a heavy test and incompatible test with pytest.
Thus we keep it separated from the rest of the tests."""
import os
import signal
import sys
import time

import mock
import requests
import requests.exceptions

from texrrow import __main__ as main

TEST_PORT = 12345


def _knock_url_and_wait(url, timeout_per_request=1, global_timeout=10):
status = None
end_time = time.time() + global_timeout
while status is None:
try:
response = requests.head(url, timeout=timeout_per_request)
status = response.status_code
except requests.exceptions.ConnectionError: # noqa
if time.time() > end_time:
raise
assert status == 200


def _run_and_test(to_run):
child_pid = os.fork()
if child_pid == 0:
to_run()
sys.exit(0)
try:
test_url = 'http://127.0.0.1:%d' % TEST_PORT
time.sleep(0.1)
pid, status = os.waitpid(child_pid, os.P_NOWAIT)

# ensure the process did not exit
assert not os.WEXITSTATUS(pid)

# try to ping uwsgi and get a HTTP 200
_knock_url_and_wait(test_url)
finally:
os.kill(child_pid, signal.SIGTERM)
os.wait()


@mock.patch.object(main, 'PORT', new=TEST_PORT)
def test_start_servers():
_run_and_test(main.start_uwsgi)
_run_and_test(main.start_flask)


if __name__ == '__main__':
test_start_servers()
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ python:
- "pypy"
- "pypy3"
install:
- pip install codecov pytest-cov
- pip install codecov pytest-cov uwsgi
- nvm install 8
- npm i
- npm run build-assets --production
- python setup.py develop
matrix:
include:
- python: "3.7"
Expand All @@ -26,10 +27,12 @@ matrix:
dist: xenial
allow_failures:
- python: "nightly"
- python: "pypy"
- python: "pypy3"
fast_finish: true
script:
- pip install -r requirements.txt
- pip install -r requirements-dev.txt
- pytest --cov --cov-report=
- python .ci/test_servers.py
after_success:
- codecov
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ coverage = "*"
blinker = "*"
flask-debugtoolbar = "*"
mock = "*"
requests = "*"

[requires]
python_version = "3"
Expand Down
32 changes: 31 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

A simple python web-server to remotely control a LaTeX presentation from a mobile phone to give dynamic and powerful speeches.

## Usage
```
texrrow [PORT=5000]
```
Run `texrrow` in your shell and open your browser in [http://127.0.0.1:5000](http://127.0.0.1:5000)!


## Development Usage
### Install Requirements
```shell
Expand Down
4 changes: 4 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ atomicwrites==1.2.1
attrs==18.2.0
blinker==1.4
certifi==2018.8.24
chardet==3.0.4
click==6.7
coverage==5.0a2
flask-debugtoolbar==0.10.1
flask==1.0.2
idna==2.7
itsdangerous==0.24
jinja2==2.10
markupsafe==1.0
Expand All @@ -29,7 +31,9 @@ pipenv==2018.7.1
pluggy==0.7.1
py==1.6.0
pytest==3.8.0
requests==2.19.1
six==1.11.0
urllib3==1.23
virtualenv-clone==0.3.0
virtualenv==16.0.0
werkzeug==0.14.1
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jinja2==2.10
keyboard==0.13.2
markupsafe==1.0
pytz==2018.5
six==1.11.0
six>=1.10.0
visitor==0.1.3
werkzeug==0.14.1
wtforms==2.2.1
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ omit =
*/test_*.py
tests/*.py
*/commands/populatedb.py
*/__main__.py
.ci/*
*/wsgi*
source = texrrow


Expand Down
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
requirements = []
if isfile('requirements.txt'):
with open('requirements.txt') as fp:
requirements = fp.readlines()
requirements = [line.strip() for line in fp.readlines()]


setup(
Expand All @@ -31,13 +31,16 @@
maintainer='NyanKiyoshi',
entry_points={
'console_scripts': [
'texrrow=texrrow.__main__:main']},
'texrrow-uwsgi=texrrow.__main__:start_uwsgi',
'texrrow=texrrow.__main__:start_flask']},
packages=find_packages(exclude=['tests']),
include_package_data=True,
data_files=[
('', ['README.md', 'LICENSE', 'requirements.txt'])],
keywords=[],
install_requires=requirements,
extras_require={
'uwsgi': ['uwsgi']},
classifiers=[
'Development Status :: 1 - Planning',
'License :: OSI Approved :: MIT License',
Expand Down
25 changes: 23 additions & 2 deletions texrrow/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
def main():
import os
import os.path
import sys

PORT = 5000
UWSGI_PATH = os.path.realpath(os.path.join(sys.executable, os.pardir, 'uwsgi'))


def _get_port_from_argv():
if len(sys.argv) > 1:
port = int(sys.argv[1])
return port
return PORT


def start_flask():
from .application import create_app
create_app().run()
create_app().run(host='0.0.0.0', port=_get_port_from_argv(), debug=False)


def start_uwsgi():
port = ':%d' % _get_port_from_argv()
argv = ['uwsgi', '--http', port, '--module', 'texrrow.wsgi:application']
os.execve(UWSGI_PATH, argv, os.environ)
13 changes: 13 additions & 0 deletions texrrow/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from texrrow.application import create_app


def health_check(_application, health_url):
def health_check_wrapper(environ, start_response):
if environ.get('PATH_INFO') == health_url:
start_response('200 OK', [('Content-Type', 'text/plain')])
return []
return _application(environ, start_response)
return health_check_wrapper


application = health_check(create_app(), '/health/')

0 comments on commit c55197e

Please sign in to comment.