Skip to content

Commit

Permalink
Move pyslackers website as subapp under /web
Browse files Browse the repository at this point in the history
  • Loading branch information
ovv committed Jun 21, 2019
1 parent df9987d commit 241ebb4
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 48 deletions.
6 changes: 3 additions & 3 deletions .platform.app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ web:
upstream:
socket_family: unix
commands:
start: "gunicorn --workers 1 --bind unix:$SOCKET --worker-class aiohttp.worker.GunicornWebWorker pyslackersweb:app_factory"
start: "gunicorn --workers 1 --bind unix:$SOCKET --access-logfile - --worker-class aiohttp.worker.GunicornWebWorker pyslackersweb:app_factory"
locations:
"/":
passthru: true
allow: false
"/static":
root: "pyslackersweb/static"
"/web/static":
root: "pyslackersweb/website/static"
expires: 1h
allow: true

Expand Down
6 changes: 6 additions & 0 deletions .platform/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
"https://{default}/":
type: upstream
upstream: "website:http"
redirects:
expires: 1d
paths:
'/':
to: "/web"
prefix: false

"https://www.{default}/":
type: redirect
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ services:
- "8000:8000"
volumes:
- "${PWD}:/app"
command: gunicorn pyslackersweb:app_factory --bind=0.0.0.0:8000 --worker-class=aiohttp.GunicornWebWorker --reload
command: gunicorn pyslackersweb:app_factory --access-logfile - --bind=0.0.0.0:8000 --worker-class=aiohttp.GunicornWebWorker --reload
48 changes: 10 additions & 38 deletions pyslackersweb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import logging
import os
from pathlib import Path

import sentry_sdk
from aiohttp import web
from aiohttp_jinja2 import setup as jinja2_setup, request_processor
from aiohttp_remotes import XForwardedRelaxed, ForwardedRelaxed
from jinja2 import FileSystemLoader
from jinja2.filters import FILTERS
from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.logging import LoggingIntegration

from .contexts import background_jobs, client_session, apscheduler, slack_client
from .filters import formatted_number
from .middleware import request_context_middleware
from .views import routes # , on_oauth2_login
from . import website

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
Expand All @@ -33,37 +25,17 @@
)


async def app_factory() -> web.Application:
package_root = Path(__file__).parent

app = web.Application(
middlewares=[
ForwardedRelaxed().middleware,
XForwardedRelaxed().middleware,
request_context_middleware,
]
)
app.update( # pylint: disable=no-member
static_root_url="/static",
github_repositories=[],
slack_user_count=0,
slack_timezones={},
slack_token=os.getenv("SLACK_TOKEN", os.getenv("SLACK_OAUTH_TOKEN")),
slack_invite_token=os.getenv("SLACK_INVITE_TOKEN", os.getenv("SLACK_OAUTH_TOKEN")),
)
async def index(request: web.Request) -> None:
location = request.app["website_app"].router["index"].url_for()
raise web.HTTPFound(location=location)

jinja2_setup(
app,
context_processors=[request_processor],
loader=FileSystemLoader([package_root / "templates"]),
filters={"formatted_number": formatted_number, **FILTERS},
)

# this ordering is important, before reordering be sure to check
# for dependencies.
app.cleanup_ctx.extend([apscheduler, client_session, slack_client, background_jobs])
async def app_factory() -> web.Application:
app = web.Application()
app.router.add_get("/", index)

app.add_routes(routes)
app.router.add_static(app["static_root_url"], package_root / "static")
website_app = website.app_factory()
app.add_subapp("/web", website_app)
app["website_app"] = website_app

return app
53 changes: 53 additions & 0 deletions pyslackersweb/website/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from pathlib import Path

from aiohttp import web
from aiohttp_jinja2 import setup as jinja2_setup, request_processor
from aiohttp_remotes import XForwardedRelaxed, ForwardedRelaxed
from jinja2 import FileSystemLoader
from jinja2.filters import FILTERS

from .contexts import background_jobs, client_session, apscheduler, slack_client
from .filters import formatted_number
from .middleware import request_context_middleware
from .views import routes # , on_oauth2_login


def app_factory() -> web.Application:
package_root = Path(__file__).parent

website = web.Application(
middlewares=[
ForwardedRelaxed().middleware,
XForwardedRelaxed().middleware,
request_context_middleware,
]
)
website.update( # pylint: disable=no-member
github_repositories=[],
slack_user_count=0,
slack_timezones={},
slack_token=os.getenv("SLACK_TOKEN", os.getenv("SLACK_OAUTH_TOKEN")),
slack_invite_token=os.getenv("SLACK_INVITE_TOKEN", os.getenv("SLACK_OAUTH_TOKEN")),
)

# aiohttp_jinja2 requires this values to be set. Sadly it does not work with subapplication.
# We need to use request.app.router["static"].url_for(filename='images/pyslackers_small.svg')
# in templates to get the correct url for static assets
website["static_root_url"] = "DO-NOT-USE"

jinja2_setup(
website,
context_processors=[request_processor],
loader=FileSystemLoader([package_root / "templates"]),
filters={"formatted_number": formatted_number, **FILTERS},
)

# this ordering is important, before reordering be sure to check
# for dependencies.
website.cleanup_ctx.extend([apscheduler, client_session, slack_client, background_jobs])

website.add_routes(routes)
website.router.add_static("/static", package_root / "static", name="static")

return website
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
<meta property="og:title" content="PySlackers"/>
<meta property="og:description" content="A community dedicated to building and enabling the global Python community by providing a safe
and inclusive place to learn, share, and grow as a Pythonista."/>
<meta property="og:image" content="{{ request.scheme }}://{{ request.host }}{{ static('images/pyslackers_official.svg') }}">
<meta property="og:image" content="{{ request.scheme }}://{{ request.host }}{{ request.app.router["static"].url_for(filename='images/pyslackers_official.svg') }}">
{% endblock %}

<title>{% block title %}PySlackers{% endblock %}</title>

{% block link %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="{{ static('styles.css') }}">
<link rel="stylesheet" href="{{ request.app.router["static"].url_for(filename='styles.css') }}">
{% endblock %}
</head>
<body>
<main>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
{% set path = url('index') %}
<a class="navbar-brand" href="{{ url('index') }}">
<img src="{{ static('images/pyslackers_small.svg') }}" style="max-width: 175px;"/>
<img src="{{ request.app.router["static"].url_for(filename='images/pyslackers_small.svg') }}" style="max-width: 175px;"/>
</a>

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainNav">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% block body %}
<div class="jumbotron jumbotron-fluid bg-dark text-light">
<div class="container text-center">
<img src="{{ static('images/pyslackers_official.svg') }}" class="mx-auto" style="max-width: 550px;"/>
<img src="{{ request.app.router["static"].url_for(filename='images/pyslackers_official.svg') }}" class="mx-auto" style="max-width: 550px;"/>

<h3 class="mt-3">
An open community for <a href="https://python.org">Python</a> programming enthusiasts.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions pyslackersweb/views.py → pyslackersweb/website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ async def get(self):
"projects": self.request.app["github_repositories"],
"sponsors": [
{
"image": "/static/images/sponsor_platformsh.svg",
"image": self.request.app.router["static"].url_for(filename="images/sponsor_platformsh.svg"),
"href": "https://platform.sh/?medium=referral&utm_campaign=sponsored_sites&utm_source=pyslackers", # pylint: disable=line-too-long
},
{
"image": "/static/images/sponsor_sentry.svg",
"image": self.request.app.router["static"].url_for(filename="images/sponsor_sentry.svg"),
"href": "https://sentry.io/?utm_source=referral&utm_content=pyslackers&utm_campaign=community", # pylint: disable=line-too-long
},
],
Expand Down

0 comments on commit 241ebb4

Please sign in to comment.