mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Make pelican.server importable and use it in fab serve
`fab serve` and `make devserver` use different HTTP Handlers and as a result they behave differently. This makes sure `fab serve` also uses the Handler defined in `pelican.server` in order to get rid of the inconsistency.
This commit is contained in:
parent
cc8f835f07
commit
7b4ceb2974
2 changed files with 23 additions and 28 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue