Skip to content

Commit

Permalink
Add nginx server container
Browse files Browse the repository at this point in the history
  • Loading branch information
gthole committed May 30, 2016
1 parent 4a4a2ae commit 2936ea4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ Start up the web server and worker with
$ docker-compose up
```

If you're running a Mac you can go to [http://gedgo.local:8000](http://gedgo.local:8000). Otherwise find out the local ip address of the gedgo docker machine and visit it. For example:
If you're running a Mac you can go to [http://gedgo.local](http://gedgo.local). Otherwise find out the local ip address of the gedgo docker machine and visit it. For example:

```bash
$ docker-machine ip gedgo
192.168.99.101
```

And you would go to [http://192.168.99.101:8000](http://192.168.99.101:8000).
And you would go to [http://192.168.99.101](http://192.168.99.101).

#### Updating Gedcoms
To update your gedcom, you can either use the manage.py command, passing it
Expand Down
11 changes: 10 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ avahi:
image: 'enernoclabs/avahi:latest'
net: 'host'
log_driver: 'none'
restart: 'on-failure'
db:
container_name: 'db'
image: 'mysql'
Expand Down Expand Up @@ -34,6 +33,16 @@ app:
links:
- 'db'
- 'redis'
web:
image: 'nginx'
command: 'nginx -g "daemon off;"'
volumes:
- './gedgo-web.conf:/etc/nginx/conf.d/gedgo-web.conf:ro'
- './files:/src/files:ro'
ports:
- '80:80'
links:
- 'app'
worker:
container_name: 'gedgo_worker'
image: 'gedgo_app'
Expand Down
26 changes: 26 additions & 0 deletions gedgo-web.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
upstream app_server {
server app:8000 fail_timeout=0;
}

server {
listen 80;
# listen [::]:80 default ipv6only=on;
server_name gedgo.local default;
sendfile on;

location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}

location /protected/ {
internal;
alias /src/files/default/;
}
}
72 changes: 34 additions & 38 deletions gedgo/views/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from wsgiref.util import FileWrapper
from django.http import HttpResponse, HttpResponseRedirect
from django.core.files.storage import default_storage
from django.contrib.auth.decorators import login_required
Expand Down Expand Up @@ -39,36 +38,35 @@ def process_comments(request, noun):
Returns a tuple of (form, redirect_response) depending on whether a
new comment has been posted or not.
"""
if request.POST:
form = CommentForm(request.POST)
if form.is_valid():
# Store file uploads
file_names = []
if getattr(settings, 'GEDGO_ALLOW_FILE_UPLOADS', True) is True:
for file_ in request.FILES.getlist('uploads'):
upload_path = 'uploaded/%s/%s/%s' % (
request.user.username,
request.path.strip('/').replace('gedgo/', ''),
file_.name
)
default_storage.save(upload_path, file_)
file_names.append(upload_path)
# Email the comment to the site owners.
form.email_comment(request.user, noun, file_names)
messages.success(
request,
'Your comment has ben sent. Thank you!'
)
else:
# Shouldn't happen, since there's almost no server-side validation
messages.error(
request,
"We're sorry, your comment was not sent."
)
return None, redirect(request.path)
if not request.POST:
return CommentForm(), None

form = CommentForm(request.POST)
if form.is_valid():
# Store file uploads
file_names = []
if getattr(settings, 'GEDGO_ALLOW_FILE_UPLOADS', True) is True:
for file_ in request.FILES.getlist('uploads'):
upload_path = 'uploaded/%s/%s/%s' % (
request.user.username,
request.path.strip('/').replace('gedgo/', ''),
file_.name
)
default_storage.save(upload_path, file_)
file_names.append(upload_path)
# Email the comment to the site owners.
form.email_comment(request.user, noun, file_names)
messages.success(
request,
'Your comment has ben sent. Thank you!'
)
else:
form = CommentForm()
return form, None
# Shouldn't happen, since there's almost no server-side validation
messages.error(
request,
"We're sorry, your comment was not sent."
)
return None, redirect(request.path)


def render(request, template, context):
Expand Down Expand Up @@ -117,14 +115,12 @@ def serve_content(storage, name):
if not storage.__class__.__name__ == 'FileSystemStorage':
return HttpResponseRedirect(storage.url(name))

# If behind a real server, use send-file
if settings.GEDGO_SENDFILE_HEADER:
response = HttpResponse()
response[settings.GEDGO_SENDFILE_HEADER] = default_storage.path(name)
# Otherwise, serve it ourselves, which should only happen in DEBUG mode
else:
wrapper = FileWrapper(storage.open(name))
response = HttpResponse(wrapper)
# Otherwise we use sendfile
response = HttpResponse()
response[settings.GEDGO_SENDFILE_HEADER] = '%s%s' % (
settings.GEDGO_SENDFILE_PREFIX,
name
)

# Set various file headers and return
base = path.basename(name)
Expand Down
3 changes: 2 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
SERVER_EMAIL = ['[email protected]']

GEDGO_SENDFILE_HEADER = None
GEDGO_SENDFILE_HEADER = 'X-Accel-Redirect'
GEDGO_SENDFILE_PREFIX = '/protected/'
GEDGO_SITE_TITLE = 'My Genealogy Site'
GEDGO_REDIS_SERVER = 'redis'
GEDGO_RESEARCH_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage'
Expand Down

0 comments on commit 2936ea4

Please sign in to comment.