🎥 Video | YoutubePC-WELT: Diese KI sieht, was wir zocken! HMX 6(17.09.2026 um 15:45 Uhr)
🔧 ProgrammierungJulian Goldie SEO: NEW Flet 1.0 Just Dropped! 🤯(17.09.2026 um 16:00 Uhr)
🕵️ SicherheitslückenSecurity Weekly - A CRA Resource: AI Is Outrunning Your Patch Program(17.09.2026 um 16:00 Uhr)
🍏 iOS / Mac OSNach dem CEO-Wechsel: Cook trifft Trump und Xi(17.09.2026 um 14:57 Uhr)
🍏 iOS / Mac OSApple Music testing out countdown clocks for upcoming albums(17.09.2026 um 15:38 Uhr)
🎥 Video | YoutubePC-WELT: Diese KI sieht, was wir zocken! HMX 6(17.09.2026 um 15:45 Uhr)
🔧 ProgrammierungJulian Goldie SEO: NEW Flet 1.0 Just Dropped! 🤯(17.09.2026 um 16:00 Uhr)
🕵️ SicherheitslückenSecurity Weekly - A CRA Resource: AI Is Outrunning Your Patch Program(17.09.2026 um 16:00 Uhr)
🍏 iOS / Mac OSNach dem CEO-Wechsel: Cook trifft Trump und Xi(17.09.2026 um 14:57 Uhr)
🍏 iOS / Mac OSApple Music testing out countdown clocks for upcoming albums(17.09.2026 um 15:38 Uhr)
🔧 Programmierung 🕛 vor 2 Jahren 10 Min Lesezeit
0

From legacy to cloud serverless - Part 4

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Note: This article was originally published on Feb 5, 2024



Hello and welcome to this article in a journey of migrating a legacy-built app to the cloud. In this section, we will focus on three aspects: interfacing the application with Cloud Firestore, automating deployment, and exploring how Binary Authorization can reinforce supply chain security while aligning with security policies.



If you're joining us midway, I encourage you to take a look at the previous articles to get up to speed. Otherwise, let's dive in! 😊






integrating the app with Cloud Firestore



Our previous code interacted with MongoDB. With the migration to Google Cloud, we are transitioning away from MongoDB in favor of Firestore, which is Google Cloud's managed NoSQL document database built for automatic scaling, high performance, and ease of application development. To achieve this, we'll need to make modifications to our code, ensuring that our application seamlessly integrates and functions with Firestore.



We will replace the old MongoDB code with the following Firestore integration:



Old:




CODE
from pymongo import MongoClient
from bson.objectid import ObjectId
import mongomock
...
if os.environ.get('TESTING'):
client = mongomock.MongoClient()
else:
client = MongoClient(os.environ['MONGO_URI'])
db = client.flask_db
todos = db.todos






New:




CODE
from google.auth import compute_engine
from google.cloud import firestore
...
credentials = compute_engine.Credentials()
db = firestore.Client(credentials=credentials)
todos = db.collection('todos')







  1. from google.auth import compute_engine: This line imports the compute_engine module from the google.auth library, which is used for authentication in Google Cloud environments


  2. from library, enabling interaction with Google Cloud Firestore.


  3. The compute_engine.Credentials() call retrieves the default credentials provided by Google Cloud in its environment. These credentials are essential for authenticating with Firestore. In a local or non-Google Cloud service environment, you would need to generate a service account key before being able to authenticate with Firestore. However, in our case, since the code will be deployed on Cloud Run, authentication will be handled using the default service account of Cloud Run.


  4. todos = db.collection('todos'). Here, we're defining a Firestore collection. Collections are used to organize documents in Firestore.




Data Insertion: When a POST request is made, the new todo item is added to the Firestore collection 'todos' using the add method. The data is stored as a dictionary.




CODE
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
content = request.form.get('content')
degree = request.form.get('degree')
todos.add({'content': content, 'degree': degree})






Old:




CODE
todos.insert_one({'content': content, 'degree': degree})






New:




CODE
todos.add({'content': content, 'degree': degree})






This modification reflects the adjustment needed in the code for Firestore, moving from the insert_one method in MongoDB to the add method in Firestore for adding documents.



Data Retrieval: In the new code, we utilize () to iterate over documents and convert them to a dictionary format for retrieval. The '_id' field represents the document ID in Firestore.



Data Deletion: In the new code, we employ todos.document(id).delete() to remove a document from the Firestore collection. In the old code, we used todos.delete_one({"_id": ObjectId(id)}) to delete a document from the MongoDB collection.



Old:




CODE
todos.delete_one({"_id": ObjectId(id)})






New:




CODE
todos.document(id).delete()






The todos.document(id).delete() method is used to delete a specific document by its ID in Firestore.



After all these updates, the new ()), and deletion (todos.document(id).delete()), along with integrating the appropriate syntax for Firestore operations.






Testing the new code



To ensure the correctness of the new '': Specifies the Docker image to be used for this step, which, in this case, is the Google Cloud SDK image.


  • entrypoint: 'gcloud': Sets the Docker entrypoint to 'gcloud,' the command-line interface for Google Cloud Platform.



  • args: A list of arguments passed to the 'gcloud' command.





    • 'run' 'deploy' '$_SERVICE_NAME': Deploys a new revision of the Cloud Run service identified by $_SERVICE_NAME.


    • '--image' '$_ for the setup of Binary Authorization."



      This concludes the article. Thank you for reading. You can find the configurations and code for this project in the following Git repository.

      Vollständiger Original-Artikel
      Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
      ↗ Original-Artikel auf dev.to lesen
  • Wie bewertest du diesen Beitrag?
    1 Klick Feedback
    Teilen mit Netzwerk & Team:

    Community-Analysen & Experten-Meinungen 0

    Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
    Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
    Community Pulse: Relevanz-Einschätzung
    1 Klick Experten-Votum
    🔴 Akute Relevanz 0%
    🟡 In Evaluierung 0%
    🟢 Keine Auswirkung 0%
    Spannende Innovation 0%
    Verwandte Story-Cluster & Quellen (Vektor-KI)
    Port 8095 Engine
    1 Quelle
    Revolut Data Breach: 5 Months, 680 High-Profile Accounts, $3M Ransom
    1 Quelle
    Mehr Angriffe, mehr Regulatorik, weniger Personal
    1 Quelle
    U.S. Seizes NightmareStresser Domains Linked to Hundreds of Thousands of DDoS Attacks
    Ähnliche Beiträge
    🔍 Verwandte News

    Auch interessante Nachrichten From legacy to cloud serverless - Part 4

    Thematisch verwandte Begriffe: From, legacy, cloud, serverless · 6 Treffer

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...

    Laden...

    Beiträge werden geladen ...

    Laden...

    Videos werden geladen ...