A semantic search engine for source code https://bitshift.benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

69 rivejä
1.7 KiB

  1. """
  2. Module to contain all the project's Flask server plumbing.
  3. """
  4. from json import dumps
  5. from flask import Flask, make_response, render_template, request
  6. from bitshift import assets
  7. from bitshift.database import Database
  8. from bitshift.languages import LANGS
  9. from bitshift.query import parse_query, QueryParseException
  10. app = Flask(__name__, static_folder="static", static_url_path="")
  11. app.config.from_object("bitshift.config")
  12. app_env = app.jinja_env
  13. app_env.line_statement_prefix = "="
  14. app_env.globals.update(assets=assets)
  15. database = Database()
  16. @app.route("/")
  17. def index():
  18. return render_template("index.html", autocomplete_languages=LANGS)
  19. @app.route("/search.json")
  20. def search():
  21. def reply(json):
  22. resp = make_response(dumps(json))
  23. resp.mimetype = "application/json"
  24. return resp
  25. query = request.args.get("q")
  26. if not query:
  27. return reply({"error": "No query given"})
  28. try:
  29. tree = parse_query(query)
  30. except QueryParseException as exc:
  31. return reply({"error": exc.args[0]})
  32. page = request.args.get("p", 1)
  33. try:
  34. page = int(page)
  35. except ValueError:
  36. return reply({"error": u"Invalid page number: %s" % page})
  37. highlight = request.args.get("hl", "0")
  38. highlight = highlight.lower() not in ["0", "false", "no"]
  39. count, codelets = database.search(tree, page)
  40. results = [clt.serialize(highlight) for clt in codelets]
  41. return reply({"count": count, "results": results})
  42. @app.route("/about")
  43. def about():
  44. return render_template("about.html")
  45. @app.route("/docs")
  46. def docs():
  47. return render_template("docs.html")
  48. @app.errorhandler(404)
  49. def error404(error):
  50. return render_template("error404.html"), 404
  51. if __name__ == "__main__":
  52. app.run(debug=True)