Skip to content

Instantly share code, notes, and snippets.

@Leeaandrob
Created September 28, 2023 13:43
Show Gist options
  • Save Leeaandrob/8919ddbd2fda9d781a3de8f579e38c1e to your computer and use it in GitHub Desktop.
Save Leeaandrob/8919ddbd2fda9d781a3de8f579e38c1e to your computer and use it in GitHub Desktop.

Revisions

  1. Leeaandrob created this gist Sep 28, 2023.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    import json
    from flask import Flask, request, Response
    from pymongo import MongoClient

    app = Flask(__name__)
    client = MongoClient("mongodb://localhost:27017")
    news_c = client.portal.news

    @app.route("/", methods=["GET"])
    def root():
    return "WORKING"

    @app.route("/healthcheck", methods=["GET"])
    def healthcheck():
    return "WORKING1"

    @app.route("/api/v1/news", methods=["POST"])
    def create():
    body = request.get_json(force=True)
    post = body["post"]
    news_c.insert_one(post)
    return Response(post, status=201, mimetype="application/json")

    @app.route("/api/v1/news", methods=["GET"])
    def newsList():
    posts = [{"name": p.get("name"), "description": p.get("description"), "thumbnail": p.get("thumbnail"), "_id": p.get("_id").__str__()} for p in news_c.find()]
    return Response(json.dumps(posts), status=200, mimetype="application/json")

    if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)