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

Rewrite frontend in React #41

Merged
merged 4 commits into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.cache
.direnv
.env*
.envrc
.floo*
.pytest_cache
build
node_modules
pip-cache
site-packages
venv
webpack-stats-actions.json
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
.vscode

npm-debug.log*
yarn-debug.log*
yarn-error.log*
17 changes: 15 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ WORKDIR /app/
RUN groupadd --gid 10001 app && useradd -g app --uid 10001 --shell /usr/sbin/nologin app

RUN apt-get update && \
apt-get install -y gcc apt-transport-https
apt-get install -y gcc apt-transport-https curl gnupg

# Install Node and Yarn
RUN curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - && \
echo 'deb https://deb.nodesource.com/node_10.x stretch main' > /etc/apt/sources.list.d/nodesource.list && \
echo 'deb-src https://deb.nodesource.com/node_10.x stretch main' >> /etc/apt/sources.list.d/nodesource.list && \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo 'deb https://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list && \
apt-get update && \
apt-get install -y nodejs yarn

COPY ./requirements.txt /app/requirements.txt
COPY ./requirements-constraints.txt /app/requirements-constraints.txt
COPY ./package.json /app/package.json
COPY ./yarn.lock /app/yarn.lock

RUN pip install -U 'pip>=8' && \
pip install --no-cache-dir -r requirements.txt
pip install --no-cache-dir -r requirements.txt && \
yarn install --non-interactive --prod

# Install the app
COPY . /app/
RUN yarn build --prod

# Set Python-related environment variables to reduce annoying-ness
ENV PYTHONUNBUFFERED 1
Expand Down
24 changes: 19 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Development
-----------

You can either do development with Docker (recommended) or with a plain
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even use Docker locally when I do dev. :)

Python `virtualenv`.
Python `virtualenv` and `Yarn`.

**Docker**

Expand All @@ -44,20 +44,34 @@ If it doesn't close properly when you `Ctrl-C` and you get the
docker-compose stop
```

Remember, if you change your `docker-compose.yml` or `requirements.txt`
you can rebuild with:
Remember, if you change your `docker-compose.yml`, `requirements.txt`, or
`package.json` you can rebuild with:
```
docker-compose build web
```

**Virtualenv**
**Virtualenv and Yarn**

To install dependencies:

```
pip install -r requirements.txt
yarn install
```

To run the app, first start the backend:

```
DEBUG=1 SQLALCHEMY_DATABASE_URI='postgres:///whatsdeployed' ./app.py
```

Then, go to http://localhost:5000/
and then in a separate terminal, start the frontend:

```
yarn start
```

This will automatically open your browser to http://localhost:3000/

To avoid hitting rate limits on GitHub's API you can go to
[Personal access tokens](https://github.com/settings/tokens) and generate
Expand Down
26 changes: 20 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from collections import defaultdict

import requests
import werkzeug
from requests.exceptions import ReadTimeout
from flask import (
Flask,
Expand All @@ -18,6 +19,8 @@
send_file,
abort,
redirect,
send_from_directory,
send_file,
)
from flask.views import MethodView
from flask_sqlalchemy import SQLAlchemy
Expand All @@ -42,7 +45,8 @@
"GITHUB_AUTH_TOKEN is NOT available. Worry about rate limits."
)

app = Flask(__name__)
# Set static_folder=None to suppress the standard static server
app = Flask(__name__, static_folder=None)
app.config['SQLALCHEMY_DATABASE_URI'] = config(
'SQLALCHEMY_DATABASE_URI',
'postgres://localhost/whatsdeployed'
Expand All @@ -69,10 +73,6 @@ def __repr__(self):
return '<Shortlink %r>' % self.link


@app.route('/')
def index_html():
return send_file('index.html')


def extract_sha(content):
content = content.strip()
Expand Down Expand Up @@ -328,7 +328,7 @@ def post(self):
class ShortenView(MethodView):

def post(self):
url = request.form['url']
url = request.json['url']
qs = urlparse(url).query
parsed = cgi.parse_qs(qs)
owner = parsed['owner'][0]
Expand Down Expand Up @@ -467,6 +467,20 @@ def get(self, thing):
'/s-<string:link>', view_func=ShortlinkRedirectView.as_view('shortlink')
)

@app.route('/', defaults={'path': 'index.html'})
@app.route('/<path:path>')
def index_html(path):
# try to serve static files out of ./build/
try:
return send_from_directory('build', path)
except werkzeug.exceptions.NotFound as e:
# try to serve index.html in the requested path
if path.endswith("/"):
return send_from_directory('build', path + 'index.html')
else:
# fall back to index.html
return send_file('build/index.html')


if __name__ == '__main__':
db.create_all()
Expand Down
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ services:
- GITHUB_AUTH_TOKEN=${GITHUB_AUTH_TOKEN}
ports:
- "5000:5000"
- "3000:3000"
volumes:
- $PWD:/app
- $PWD/app.py:/app/app.py
- $PWD/public:/app/public
- $PWD/src:/app/src
command: bash -c 'python app.py & PORT=3000 yarn start'
stop_signal: kill
Loading