A semantic search engine for source code https://bitshift.benkurtovic.com/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

65 行
1.7 KiB

  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__, static_folder="static", static_url_path="")
  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.errorhandler(404)
  49. def error404(error):
  50. return render_template("error404.html"), 404
  51. if __name__ == "__main__":
  52. app.run(debug=True)