git clone https://github.com/a-thousand-worlds/a-thousand-worlds-2/
npm install
cd functions && npm install && cd ..
npm install -g firebase-tools
- Create new project at firebase.google.com
- Create web app in Firebase project
- Save Firebase config into
.env.local
- App → Firebase SDK snippet → Config
- Create Realtime Database in Firebase project
- Enable Firebase Storage
- Configure CORS policy for storage bucket
- Copy the storage folder path which appears at the top of the Storage → Files table, e.g.
gs://a-thousand-worlds.appspot.com
- Open the GCP console and start a cloud terminal session by clicking the >_ icon button in the top navbar.
- Click the "Open Editor" button with the pencil icon.
- Create
cors.json
with the contents offirebase.storage.cors.json
. - Click the "Open Terminal" button and run
gsutil cors set cors.json gs://a-thousand-worlds.appspot.com
. Use the storage folder path you copied in the first step above.
- Copy the storage folder path which appears at the top of the Storage → Files table, e.g.
- Enable Email/Password authentication
- Authentication → Sign-in method
- Add
firebase.rules.json
to Realtime Database access rules and publish- Realtime Database → Rules
firebase login
- Set active Firebase app:
firebase use --add
- Install Firebase function dependencies:
cd functions && npm i
- Generate service account key
- Firebase Project -> Settings -> Service Accounts -> Generate new private key
- Save to
/functions/serviceAccountKey.json
- Add Goodreads API key for firebase function
firebase functions:config:set goodreads.api_key="YOUR_API_KEY"
- Deploy Firebase functions:
firebase deploy --only functions
- Add Request URL from Firebase Functions dashboard to
VUE_APP_SEARCH_SERVICE_URL
in .env.local - Set administrator email functions config:
firebase functions:config:set project.admin_email="ADMIN_EMAIL"
- Set allowed email recipients (optional):
- Allows unauthorized users to send emails to site owners/admin
- Defaults to config.project.admin_email
firebase functions:config:set project.allowed_email_recipients.0="EMAIL"
- Copy firebase config for local emulator:
firebase functions:config:get > functions/.runtimeconfig.json
- Navigate to
FUNCTIONS_URL/buildCache
to buildpublic/dbcache.js
.- Get full endpoint URL at Firebase Functions dashboard (same baseUrl as
VUE_APP_SEARCH_SERVICE_URL
) - File
dbcache.js
and book covers generated automatically at Firebase hosting—no redeployment required.
- Get full endpoint URL at Firebase Functions dashboard (same baseUrl as
Start the server and navigate to: http://localhost:8080
npm start
*The server must be restarted if .env
changes
Run Firebase functions with the local emulator to enable book search API:
# pull remote config down to be used by emulator
firebase functions:config:get > functions/.runtimeconfig.json
# build and run emulators
npm run build
firebase emulators:start
A static cache of the database is stored at /public/dbcache.js
for performance.
To update your local dbcache with the latest books in production:
npm run update:dbcache .env.local
- Enter email and password for your ATW account (not Firebase).
%ENV_FILE% contains firebase configuration and determines the Firebase database and storage for building local cache production database. Instead, rebuild the production cache (clear or set to false
cache/clearat database and fire
buildCache` firebase function endpoint) to fix it- Book covers are also cached from Firebase Storage to Firebase Hosting (because Hosting is faster).
- The script downloads books covers to
/public/img
, and saves the database cache to/public/dbcache.js
- It is best not to deploy the local cache to production with
firebase deploy
. Instead, use the production instructions below.
- It is best not to deploy the local cache to production with
- Cache rebuilds daily by Firebase pubsub (cron) at 00:00 New York timezone.
- To configure automatic schedule and timezone, update
functions/index.js
file (line 75) and redeploy Firebase functions (firebase deploy --only functions
) - The local cache cannot not updated automatically because firebase emulators to not have cron functionality.
- To configure automatic schedule and timezone, update
- The website uses
cache/clear
in Firebase to check if cache rebuilding is not required.- To manually rebuild the cache, clear the flag in the Firebase console and hit the
buildCache
Firebase function endpoint. update:dbcache
ignorescache/clear
- To manually rebuild the cache, clear the flag in the Firebase console and hit the
- buildCache http entrypoint and cron pubsub function
- To use rebuild cache on Firebase Hosting websites manually there is
FUNCTIONS_URL/buildCache
entrypoint with available query paramhost
. - Default value for
host
param is'all'
but it applies Firebase WebsiteSITE_ID
value if there are many websites use same Firebase Database and functions. - Default automatic cache updatating function updates all websites. To configure this behavior - update
functions/buildCacheCron.js
(line 5). For separate autoupdatess of multiple websites, duplicatefunctions/buildCacheCron.js
and updatefunction/buildCacheCron*.js
andfunctions/index.js
.
- To use rebuild cache on Firebase Hosting websites manually there is
Make sure that local cache files are correct or not deployed.
npm run deploy
The website needs a properly configured and authorized email server to send emails to users on invite, book submission, etc.
- Create and configure mailgun account
- Update DNS sending (TXT) and optionally receiving (MX) records
- Generate sending SMTP credentials for domain
- Maingun dashboard -> Sending -> Domain settings -> SMTP credentials
- For
postmater@DOMAIN
clickReset password
-> on appeared popup clickCopy
button -> temporary save password for later use - Or generate new SMTP user and use this user later replacing postmaster@DOMAIN
- Set firebase function configuration to send emails:
firebase functions:config:set mailgun.user="postmaster@DOMAIN" mailgun.password="POSTMASTER_PASSWORD" mailgun.sender="sender-email@DOMAIN"
(sender email can be any at your domain, for example: bot@DOMAIN)