Skip to content

Commit

Permalink
Redirect HTTP to HTTPS if provided domain
Browse files Browse the repository at this point in the history
McSinyx committed Dec 24, 2020
1 parent b82110a commit dbda915
Showing 4 changed files with 33 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/acanban/__main__.py
Original file line number Diff line number Diff line change
@@ -17,12 +17,19 @@
# You should have received a copy of the GNU Affero General Public License
# along with Acanban. If not, see <https://www.gnu.org/licenses/>.

from hypercorn.middleware import HTTPToHTTPSRedirectMiddleware
from hypercorn.trio import serve
from hypercorn.typing import ASGIFramework
from trio import run

from . import app
from .config import hypercorn_config, rethinkdb_config
from . import app as acanban
from .config import acanban_config, hypercorn_config, rethinkdb_config

if __name__ == '__main__':
app.rethinkdb_config = rethinkdb_config()
domain = acanban_config().get('domain')
acanban.rethinkdb_config = rethinkdb_config()
if domain is None:
app: ASGIFramework = acanban
else:
app = HTTPToHTTPSRedirectMiddleware(acanban, domain)
run(serve, app, hypercorn_config())
11 changes: 10 additions & 1 deletion src/acanban/config.py
Original file line number Diff line number Diff line change
@@ -26,9 +26,18 @@
TomMapping = MutableMapping[str, Any]

CONFIG_DIRS = user_config_dir('acanban'), site_config_dir('acanban')
ACANBAN_DEFAULT: TomMapping = {}
RETHINKDB_DEFAULT: TomMapping = {'db': 'test'}


def acanban_config(dirs: Sequence[str] = CONFIG_DIRS) -> TomMapping:
"""Return Acanban configuration first found in given directories."""
for directory in dirs:
file = join(directory, 'acanban.toml')
if isfile(file): return toml.load(file)
return ACANBAN_DEFAULT


def hypercorn_config(dirs: Sequence[str] = CONFIG_DIRS) -> HyperConf:
"""Return Hypercorn configuration first found in given directories."""
for directory in dirs:
@@ -38,7 +47,7 @@ def hypercorn_config(dirs: Sequence[str] = CONFIG_DIRS) -> HyperConf:


def rethinkdb_config(dirs: Sequence[str] = CONFIG_DIRS) -> TomMapping:
"""Return Hypercorn configuration first found in given directories."""
"""Return RethinkDB configuration first found in given directories."""
for directory in dirs:
file = join(directory, 'rethinkdb.toml')
if isfile(file): return toml.load(file)
1 change: 1 addition & 0 deletions tests/assets/acanban.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
domain = 'python.org'
13 changes: 12 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -22,17 +22,28 @@
import toml
from pytest import mark, param

from acanban.config import (RETHINKDB_DEFAULT,
from acanban.config import (ACANBAN_DEFAULT, RETHINKDB_DEFAULT, acanban_config,
hypercorn_config, rethinkdb_config)

CONFIG_DIR = abspath(join(dirname(__file__), 'assets'))
EMPTY_DIR = 'this directory does not exist'

ACANBAN_MOCK = toml.load(join(CONFIG_DIR, 'acanban.toml'))
HYPERCORN_MOCK = toml.load(join(CONFIG_DIR, 'hypercorn.toml'))
HYPERCORN_DEFAULT = {'bind': ['127.0.0.1:8000']}
RETHINKDB_MOCK = toml.load(join(CONFIG_DIR, 'rethinkdb.toml'))


@mark.parametrize(('dirs', 'mapping'), (
param([CONFIG_DIR], ACANBAN_MOCK, id='load'),
param([EMPTY_DIR, CONFIG_DIR], ACANBAN_MOCK, id='find'),
param([EMPTY_DIR], ACANBAN_DEFAULT, id='fallback')))
def test_acanban_config(dirs: Sequence[str],
mapping: Mapping[str, Any]) -> None:
"""Test loading Acanban configuration."""
assert acanban_config(dirs) == mapping


@mark.parametrize(('dirs', 'mapping'), (
param([CONFIG_DIR], HYPERCORN_MOCK, id='load'),
param([EMPTY_DIR, CONFIG_DIR], HYPERCORN_MOCK, id='find'),

0 comments on commit dbda915

Please sign in to comment.