Commit 3cc5b337 authored by Antoine Beaupré's avatar Antoine Beaupré

merge serve into search engine

parent 654d7568
......@@ -4,13 +4,7 @@ from __future__ import print_function
import logging
log = logging.getLogger(__name__)
try:
import http.server as http
import socketserver
except ImportError:
import SimpleHTTPServer as http
import SocketServer as socketserver
import os
import os.path
import click
......@@ -19,6 +13,7 @@ from debmans.logger import setup_logging, levels
from debmans.utils import find_static_file
from debmans.extractor import extract, PackageMirror
from debmans.renderer import render, site
from debmans.search import serve
@click.group(chain=True)
......@@ -111,25 +106,6 @@ def debmans(ctx, loglevel, syslog, dryrun, plugin, theme,
debmans.add_command(extract)
debmans.add_command(render)
debmans.add_command(site)
@click.command()
@click.option('-p', '--port', default=8000, show_default=True,
help='port to start the server on')
@click.pass_obj
def serve(obj, port):
'''serve deployed files in a basic web browser
this is used for development purposes and is not designed to be a
fully-fledged webserver
'''
output = obj['output']
os.chdir(output)
handler = http.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(("", port), handler)
host, port = httpd.socket.getsockname()
log.info('Serving %s over HTTP on %s port %d...', output, host, port)
httpd.serve_forever()
debmans.add_command(serve)
......
......@@ -18,25 +18,26 @@
from __future__ import division, absolute_import
from __future__ import print_function
import logging
logger = logging.getLogger(__name__)
import os
import os.path
import re
import click
from flask import Flask
app = Flask(__name__)
from flask import Flask, Blueprint, current_app
from jinja2 import Template
from debmans.utils import find_static_file
from debmans.renderer import find_files, ManpageRenderer
# we need to use a blueprint because we dynamically create the Flask
# app with a :attr:`static_folder` that is based on the `--output`
# flag or whatever outside config
blueprint = Blueprint('search', __name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/<pattern>')
@blueprint.route('/search/<pattern>')
def search(pattern):
'''respond to searches requests
......@@ -62,14 +63,15 @@ def search(pattern):
pattern = re.sub('\(\?P<name>[^)]*\)',
pattern, # XXX: wildcard or not?
ManpageRenderer.pattern)
app.logger.debug('searching for pattern: %s', pattern)
logger.debug('searching for pattern: %s', pattern)
patterns = {}
# XXX: this doesn't belong here? should be command-level patterns
patterns[re.compile(pattern)] = None
# XXX: not sure this is the best structure
manpages = {} # suite -> (name, section)
i = 0
for path in find_files(app.config['output'], patterns):
assert current_app.static_folder
for path in find_files(current_app.static_folder, patterns):
i += 1
# XXX: again
m = re.search(ManpageRenderer.pattern, path)
......@@ -79,7 +81,7 @@ def search(pattern):
section = m.group('section')
if section not in manpages:
manpages[section] = set()
app.logger.debug('found manpage %s (%s) in %s', name, section, suite)
logger.debug('found manpage %s (%s) in %s', name, section, suite)
manpages[section].add((name, suite))
# XXX: should extend the normal template instead
# see http://flask.pocoo.org/docs/0.11/tutorial/templates/#login-html
......@@ -106,11 +108,16 @@ def search(pattern):
@click.command()
@click.option('-p', '--port', default=8000, show_default=True,
help='port to start the server on')
@click.pass_obj
def devsearch(obj):
'''search the given output directory
def serve(obj, port):
'''serve deployed files in a basic web browser
.. todo:: this should probably replace serve
also supports basic searching
this is used for development purposes and is not designed to be a
fully-fledged webserver
obviously, a production setup would not have this as a click
command, but as a CGI or WSGI app. see:
......@@ -124,16 +131,22 @@ def devsearch(obj):
and CGI works everywhere.
see also:
see also:
https://blog.pythonanywhere.com/36/ nginx+uWSGI vs Apache - why we switched.
'''
main(obj['output'])
main(obj['output'], port)
@blueprint.route('/')
def root():
return current_app.send_static_file('index.html')
def main(output):
# XXX: not sure this is right, see http://flask.pocoo.org/docs/0.11/config/
app.config['output'] = output
app.run(port=8000)
def main(output, port=8000):
output = os.path.abspath(output)
app = Flask(__name__, static_folder=output, static_url_path='')
app.register_blueprint(blueprint)
app.run(port=port)
if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment