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.

app.py 1.8 KiB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. Module to contain all the project's Flask server plumbing.
  3. """
  4. import datetime
  5. import flask
  6. from json import dumps
  7. from flask import Flask, make_response, render_template, request
  8. from bitshift import assets
  9. from bitshift.database import Database
  10. from bitshift.languages import LANGS
  11. from bitshift.query import parse_query, QueryParseException
  12. app = Flask(__name__)
  13. app.config.from_object("bitshift.config")
  14. app.jinja_env.globals.update(year=datetime.datetime.now().year)
  15. app_env = app.jinja_env
  16. app_env.line_statement_prefix = "="
  17. app_env.globals.update(assets=assets)
  18. database = Database()
  19. @app.route("/")
  20. def index():
  21. return render_template("index.html", autocomplete_languages=LANGS)
  22. @app.route("/search.json")
  23. def search():
  24. def reply(json):
  25. resp = make_response(dumps(json))
  26. resp.mimetype = "application/json"
  27. return resp
  28. query, page = request.args.get("q"), request.args.get("p", 1)
  29. if not query:
  30. return reply({"error": "No query given"})
  31. try:
  32. tree = parse_query(query)
  33. except QueryParseException as exc:
  34. return reply({"error": exc.args[0]})
  35. try:
  36. page = int(page)
  37. except ValueError:
  38. return reply({"error": u"Invalid page number: %s" % page})
  39. count, codelets = database.search(tree, page)
  40. results = [clt.serialize() 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("/developers")
  46. def developers():
  47. return render_template("developers.html")
  48. @app.route("/robots.txt")
  49. @app.route("/sitemap.xml")
  50. @app.route("/favicon.ico")
  51. def static_from_root():
  52. return flask.send_from_directory(app.static_folder, request.path[1:])
  53. if __name__ == "__main__":
  54. app.run(debug=True)