Cloud Functions projenize entegre oldukça, kodunuz birçok bağımsız işlev içerecek şekilde genişleyebilir. Tek bir dosyaya makul biçimde sığmayacak kadar çok işleviniz olabilir veya farklı ekipler farklı işlev grupları dağıtarak bir ekibin üzerine yazması veya başka bir ekibin işlevlerinin yanlışlıkla silinmesi riskiyle karşı karşıya kalabilir. Cloud Functions, gezinmenizi ve işlevlerinizi sürdürmenizi kolaylaştırmak amacıyla kodunuzu düzenlemeniz için farklı yollar sunar.
Kod tabanlarındaki işlevleri düzenleme
Tek bir depo monorepo kurulumunda birden fazla depo veya alt paket genelinde geniş bir işlev koleksiyonunu yönetmek için firebase.json
uygulamasındaki işlevler yapılandırma nesnesinin codebase
özelliğini kullanabilirsiniz:
# firebase.json
"functions": {
"codebase": "my-codebase"
# NOTE: Codebase must be less than 63 characters and can contain only
# lowercase letters, numeric characters, underscores, and dashes.
}
codebase
özelliği, Firebase CLI 10.7.1 ve sonraki sürümlerde desteklenir.
Birden fazla depoyu yönetme
codebase
özelliği, birden fazla deponun yönetimini basitleştirebilir. Aynı Firebase projesine işlev dağıtan iki farklı deponuzun olduğu bir durumu inceleyelim:
$ tree .
├── repoA
│ ├── firebase.json
│ └── functions
│ ├── index.js
│ └── package.json
└── repoB
├── firebase.json
└── functions
├── index.js
└── package.json
Kod tabanı ek açıklamaları olmasaydı Firebase CLI, dağıtım sırasında diğer depoda tanımlanan işlevleri silmenizi isterdi:
$ (cd repoA && firebase deploy --only functions)
...
i functions: preparing functions directory for uploading...
✔ functions: functions folder uploaded successfully
The following functions are found in your project but do not exist in your local source code:
fn1FromRepoB
fn2FromRepoB
...
? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. (y/N)
Her proje deposundaki firebase.json
işlevindeki işlev yapılandırma bölümüne benzersiz bir kod tabanı ek açıklaması ekleyerek bu sorunu önleyebilirsiniz:
# repoA/firebase.json
"functions": {
"codebase": "repo-a"
}
# repoB/firebase.json
"functions": {
"codebase": "repo-b"
}
Kod tabanı ek açıklamasıyla, Firebase CLI artık doğrudan deponuzun dışında tanımlanan işlevleri silmenizi istemez:
$ (cd repoA && firebase deploy --only functions)
...
i functions: preparing functions directory for uploading...
✔ functions: functions folder uploaded successfully
# Gleefully ignores functions from repoB
i functions: creating Node.js 16 function fnFromRepoA (us-central1)...
✔ Deploy Complete!
Birden çok kaynak paketi yönetme (monorepo)
codebase
özelliği, tek bir depodaki birden çok kaynak paketin yönetimini basitleştirebilir. İşlev tanımlarının birkaç alt pakete yayıldığı bir firebase proje dizininizin olduğu bir durumu inceleyelim:
$ tree .
├── firebase.json
├── teamA
│ ├── index.js
│ └── package.json
└── teamB
├── index.js
└── package.json
Bu kurulum, aşağıdaki kullanım alanlarına uygundur:
- Monorepo kurulumunuz var ve farklı ekiplerin izole bir pakette kendi işlev tanımlarını yönetmesini sağlıyor.
- Yoğun bir dış bağımlılığı ve uzun süreli başlatması olan bir işleviniz var ve bu işlevi gecikmeye duyarlı diğer işlevlerden ayırmak istiyorsunuz.
Bunun gibi monrepo kurulumunu desteklemek için firebase.json
içinde birden çok işlev yapılandırması tanımlayın:
"functions": [
{
"source": "teamA",
"codebase": "team-a"
},
{
"source": "teamB",
"codebase": "team-b"
},
]
Bu yapılandırmayla Firebase CLI, tüm paketlerdeki işlevleri tek bir dağıtım komutuyla dağıtır:
$ firebase deploy --only functions
i deploying functions
i functions: preparing codebase team-a for deployment
i functions: preparing codebase team-b for deployment
i functions: creating Node.js 16 function team-a:helloATeam(us-central1)...
i functions: creating Node.js 16 function team-b:helloBTeam(us-central1)...
...
Ayrıca belirli bir kod tabanını da dağıtabilirsiniz:
$ firebase deploy --only functions:team-b
i deploying functions
i functions: preparing codebase team-b for deployment
i functions: updating Node.js 16 function team-b:helloBTeam(us-central1)...
...
Birden çok dosyaya işlev yazma
Cloud Functions'ı kullanmaya başlarken ilk birkaç işlevinizi tek bir dosyaya yerleştirebilirsiniz:
index.js
const functions = require('firebase-functions');
exports.foo = functions.https.onRequest((request, response) => {
// ...
});
exports.bar = functions.https.onRequest((request, response) => {
// ...
});
main.py
from firebase_functions import https_fn
@https_fn.on_request()
def foo(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello foo!")
@https_fn.on_request()
def bar(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello bar!")
Birkaç farklı işlevle bunu yönetmek zor olabilir. Bunun yerine, her işlev için tüm mantığınızı kendi dosyasına yerleştirebilir ve kaynak dosyanızı bir dışa aktarma listesi olarak kullanabilirsiniz:
Node.js
foo.js
const functions = require('firebase-functions'); exports.foo = functions.https.onRequest((request, response) => { // ... });
bar.js
const functions = require('firebase-functions'); exports.bar = functions.https.onRequest((request, response) => { // ... });
index.js
const foo = require('./foo'); const bar = require('./bar'); exports.foo = foo.foo; exports.bar = bar.bar;
Python
foo.py
from firebase_functions import https_fn
@https_fn.on_request()
def foo(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello foo!")
bar.py
from firebase_functions import https_fn
@https_fn.on_request()
def bar(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response("Hello foo!")
main.py
from fn_impl.foo import *
from fn_impl.bar import *
Bu kurulumda proje dizini yapısının aşağıdaki gibi olduğu varsayılır:
my-project
├── firebase.json
└── functions
├── fn_impl
│ ├── __init__.py
│ ├── foo.py
│ └── bar.py
├── main.py
└── requirements.txt
fn_impl
: Herhangi bir adı olabilir
__init__.py
: Zorunludur, ancak boş olabilir
Grup işlevleri
Birçok projede işlevler, birlikte dağıtılması ve korunması gereken mantıksal gruplara ayrılabilir. Örneğin, metrikleri raporlamak için kullanılan bir işlev grubunuz olabilir:
metrics.js
const functions = require('firebase-functions'); exports.usageStats = functions.https.onRequest((request, response) => { // ... }); exports.nightlyReport = functions.https.onRequest((request, response) => { // ... });
Şu işlevleri index.js
dosyanızda dışa aktarırken bir gruba yerleştirebilirsiniz:
index.js
// Export both functions from metrics.js in the "metrics" group: // - metrics-usageStats // - metrics-nightlyReport exports.metrics = require('./metrics');
Dağıtıldığında işlevlere gruplarının adı eklenir. Bu örnekte, işlevler metrics-usageStats
ve metrics-nightlyReport
olarak adlandırılır.
İşlevleri dağıtırken işlemi tek bir grupla sınırlandırabilirsiniz:
firebase deploy --only functions:metrics
Sonraki adımlar
Cloud Functions hakkında daha fazla bilgi edinmek için aşağıdaki konulara göz atın: