Merge pull request #111 from channelcat/reverse-static

Reverse static arguments
This commit is contained in:
Channel Cat 2016-10-25 02:52:21 -07:00 committed by GitHub
commit 65950250d9
8 changed files with 17 additions and 14 deletions

View File

@ -1,7 +1,10 @@
Version 0.1 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 - Static files
- Lazy Cookie Loading
- 0.1.5 - 0.1.5
- Cookies - Cookies
- Blueprint listeners and ordering - Blueprint listeners and ordering

View File

@ -8,11 +8,11 @@ Both directories and files can be served by registering with static
app = Sanic(__name__) app = Sanic(__name__)
# Serves files from the static folder to the URL /static # 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 # Serves the file /home/ubuntu/test.png when the URL /the_best.png
# is requested # 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) app.run(host="0.0.0.0", port=8000)
``` ```

View File

@ -1,6 +1,6 @@
from .sanic import Sanic from .sanic import Sanic
from .blueprints import Blueprint from .blueprints import Blueprint
__version__ = '0.1.6' __version__ = '0.1.7'
__all__ = ['Sanic', 'Blueprint'] __all__ = ['Sanic', 'Blueprint']

View File

@ -33,14 +33,14 @@ class BlueprintSetup:
""" """
self.app.exception(*args, **kwargs)(handler) 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 Registers static files to sanic
""" """
if self.url_prefix: if self.url_prefix:
uri = self.url_prefix + uri 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): def add_middleware(self, middleware, *args, **kwargs):
""" """
@ -122,8 +122,8 @@ class Blueprint:
return handler return handler
return decorator return decorator
def static(self, file_or_directory, uri, *args, **kwargs): def static(self, uri, file_or_directory, *args, **kwargs):
""" """
""" """
self.record( self.record(
lambda s: s.add_static(file_or_directory, uri, *args, **kwargs)) lambda s: s.add_static(uri, file_or_directory, *args, **kwargs))

View File

@ -95,13 +95,13 @@ class Sanic:
return register_middleware return register_middleware
# Static Files # Static Files
def static(self, file_or_directory, uri, pattern='.+', def static(self, uri, file_or_directory, pattern='.+',
use_modified_since=True): use_modified_since=True):
""" """
Registers a root to serve files from. The input can either be a file Registers a root to serve files from. The input can either be a file
or a directory. See or a directory. See
""" """
static_register(self, file_or_directory, uri, pattern, static_register(self, uri, file_or_directory, pattern,
use_modified_since) use_modified_since)
def blueprint(self, blueprint, **options): def blueprint(self, blueprint, **options):

View File

@ -7,7 +7,7 @@ from .exceptions import FileNotFound, InvalidUsage
from .response import file, HTTPResponse 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 # 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 # make a good effort here. Modified-since is nice, but we could
# also look into etags, expires, and caching # also look into etags, expires, and caching

View File

@ -156,7 +156,7 @@ def test_bp_static():
app = Sanic('test_static') app = Sanic('test_static')
blueprint = Blueprint('test_static') blueprint = Blueprint('test_static')
blueprint.static(current_file, '/testing.file') blueprint.static('/testing.file', current_file)
app.blueprint(blueprint) app.blueprint(blueprint)

View File

@ -10,7 +10,7 @@ def test_static_file():
current_file_contents = file.read() current_file_contents = file.read()
app = Sanic('test_static') 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') request, response = sanic_endpoint_test(app, uri='/testing.file')
assert response.status == 200 assert response.status == 200
@ -23,7 +23,7 @@ def test_static_directory():
current_file_contents = file.read() current_file_contents = file.read()
app = Sanic('test_static') 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') request, response = sanic_endpoint_test(app, uri='/dir/test_static.py')
assert response.status == 200 assert response.status == 200