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)