Skip to content

Commit

Permalink
Merge pull request sanic-org#111 from channelcat/reverse-static
Browse files Browse the repository at this point in the history
Reverse static arguments
  • Loading branch information
channelcat authored Oct 25, 2016
2 parents 977081f + 74ae000 commit 6595025
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 14 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Version 0.1
-----------
- 0.1.6 (not released)
- 0.1.7
- Reversed static url and directory arguments to meet spec
- 0.1.6
- Static files
- Lazy Cookie Loading
- 0.1.5
- Cookies
- Blueprint listeners and ordering
Expand Down
4 changes: 2 additions & 2 deletions docs/static_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Both directories and files can be served by registering with static
app = Sanic(__name__)

# Serves files from the static folder to the URL /static
app.static('./static', '/static')
app.static('/static', './static')

# Serves the file /home/ubuntu/test.png when the URL /the_best.png
# is requested
app.static('/home/ubuntu/test.png', '/the_best.png')
app.static('/the_best.png', '/home/ubuntu/test.png')

app.run(host="0.0.0.0", port=8000)
```
2 changes: 1 addition & 1 deletion sanic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .sanic import Sanic
from .blueprints import Blueprint

__version__ = '0.1.6'
__version__ = '0.1.7'

__all__ = ['Sanic', 'Blueprint']
8 changes: 4 additions & 4 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def add_exception(self, handler, *args, **kwargs):
"""
self.app.exception(*args, **kwargs)(handler)

def add_static(self, file_or_directory, uri, *args, **kwargs):
def add_static(self, uri, file_or_directory, *args, **kwargs):
"""
Registers static files to sanic
"""
if self.url_prefix:
uri = self.url_prefix + uri

self.app.static(file_or_directory, uri, *args, **kwargs)
self.app.static(uri, file_or_directory, *args, **kwargs)

def add_middleware(self, middleware, *args, **kwargs):
"""
Expand Down Expand Up @@ -122,8 +122,8 @@ def decorator(handler):
return handler
return decorator

def static(self, file_or_directory, uri, *args, **kwargs):
def static(self, uri, file_or_directory, *args, **kwargs):
"""
"""
self.record(
lambda s: s.add_static(file_or_directory, uri, *args, **kwargs))
lambda s: s.add_static(uri, file_or_directory, *args, **kwargs))
4 changes: 2 additions & 2 deletions sanic/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ def register_middleware(middleware):
return register_middleware

# Static Files
def static(self, file_or_directory, uri, pattern='.+',
def static(self, uri, file_or_directory, pattern='.+',
use_modified_since=True):
"""
Registers a root to serve files from. The input can either be a file
or a directory. See
"""
static_register(self, file_or_directory, uri, pattern,
static_register(self, uri, file_or_directory, pattern,
use_modified_since)

def blueprint(self, blueprint, **options):
Expand Down
2 changes: 1 addition & 1 deletion sanic/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .response import file, HTTPResponse


def register(app, file_or_directory, uri, pattern, use_modified_since):
def register(app, uri, file_or_directory, pattern, use_modified_since):
# TODO: Though sanic is not a file server, I feel like we should atleast
# make a good effort here. Modified-since is nice, but we could
# also look into etags, expires, and caching
Expand Down
2 changes: 1 addition & 1 deletion tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_bp_static():
app = Sanic('test_static')
blueprint = Blueprint('test_static')

blueprint.static(current_file, '/testing.file')
blueprint.static('/testing.file', current_file)

app.blueprint(blueprint)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_static_file():
current_file_contents = file.read()

app = Sanic('test_static')
app.static(current_file, '/testing.file')
app.static('/testing.file', current_file)

request, response = sanic_endpoint_test(app, uri='/testing.file')
assert response.status == 200
Expand All @@ -23,7 +23,7 @@ def test_static_directory():
current_file_contents = file.read()

app = Sanic('test_static')
app.static(current_directory, '/dir')
app.static('/dir', current_directory)

request, response = sanic_endpoint_test(app, uri='/dir/test_static.py')
assert response.status == 200
Expand Down

0 comments on commit 6595025

Please sign in to comment.