1
0
Fork 0
forked from github/pelican

Merge pull request #1679 from avaris/fix_fab_serve

Make `pelican.server` importable and use it in `fab serve`
This commit is contained in:
Justin Mayer 2015-04-05 10:32:05 +09:00
commit b295163142
3 changed files with 25 additions and 30 deletions

View file

@ -61,10 +61,10 @@ Preview your site
-----------------
Open a new terminal session and run the following commands to switch to your
``output`` directory and launch Python's built-in web server::
``output`` directory and launch Pelican's web server::
cd ~/projects/yoursite/output
python -m SimpleHTTPServer # -m http.server if you use python3
python -m pelican.server
Preview your site by navigating to http://localhost:8000/ in your browser.

View file

@ -2,30 +2,22 @@ from __future__ import print_function
import os
import sys
import logging
try:
import SimpleHTTPServer as srvmod
except ImportError:
import http.server as srvmod # NOQA
try:
import SocketServer as socketserver
except ImportError:
import socketserver # NOQA
from six.moves import SimpleHTTPServer as srvmod
from six.moves import socketserver
try:
from magic import from_file as magic_from_file
except ImportError:
magic_from_file = None
PORT = len(sys.argv) in (2, 3) and int(sys.argv[1]) or 8000
SERVER = len(sys.argv) == 3 and sys.argv[2] or ""
SUFFIXES = ['', '.html', '/index.html']
class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
SUFFIXES = ['', '.html', '/index.html']
def do_GET(self):
# Try to detect file by applying various suffixes
for suffix in SUFFIXES:
for suffix in self.SUFFIXES:
if not hasattr(self, 'original_path'):
self.original_path = self.path
@ -56,19 +48,21 @@ class ComplexHTTPRequestHandler(srvmod.SimpleHTTPRequestHandler):
return mimetype
Handler = ComplexHTTPRequestHandler
if __name__ == '__main__':
PORT = len(sys.argv) in (2, 3) and int(sys.argv[1]) or 8000
SERVER = len(sys.argv) == 3 and sys.argv[2] or ""
socketserver.TCPServer.allow_reuse_address = True
try:
httpd = socketserver.TCPServer((SERVER, PORT), Handler)
except OSError as e:
logging.error("Could not listen on port %s, server %s.", PORT, SERVER)
sys.exit(getattr(e, 'exitcode', 1))
socketserver.TCPServer.allow_reuse_address = True
try:
httpd = socketserver.TCPServer((SERVER, PORT), ComplexHTTPRequestHandler)
except OSError as e:
logging.error("Could not listen on port %s, server %s.", PORT, SERVER)
sys.exit(getattr(e, 'exitcode', 1))
logging.info("Serving at port %s, server %s.", PORT, SERVER)
try:
httpd.serve_forever()
except KeyboardInterrupt as e:
logging.info("Shutting down server.")
httpd.socket.close()
logging.info("Serving at port %s, server %s.", PORT, SERVER)
try:
httpd.serve_forever()
except KeyboardInterrupt as e:
logging.info("Shutting down server.")
httpd.socket.close()

View file

@ -3,9 +3,10 @@ import fabric.contrib.project as project
import os
import shutil
import sys
import SimpleHTTPServer
import SocketServer
from pelican.server import ComplexHTTPRequestHandler
# Local path configuration (can be absolute or relative to fabfile)
env.deploy_path = 'output'
DEPLOY_PATH = env.deploy_path
@ -51,7 +52,7 @@ def serve():
class AddressReuseTCPServer(SocketServer.TCPServer):
allow_reuse_address = True
server = AddressReuseTCPServer(('', PORT), SimpleHTTPServer.SimpleHTTPRequestHandler)
server = AddressReuseTCPServer(('', PORT), ComplexHTTPRequestHandler)
sys.stderr.write('Serving on port {0} ...\n'.format(PORT))
server.serve_forever()