From 664fbda6539a69cbc8225037686fe5a0aa7eba3f Mon Sep 17 00:00:00 2001 From: Martin Dimitrov Date: Tue, 10 Jun 2025 18:23:03 -0700 Subject: [PATCH] set up local env vars --- .gitignore | 1 - packages/doorman-api/.env.local | 36 +++++++++++++++++++ packages/doorman-api/.gitignore | 1 - packages/doorman-api/README.md | 52 ++++++++++++++++++++++++++- packages/doorman-api/package.json | 4 +-- packages/doorman-api/src/utils/ddb.ts | 2 +- 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 packages/doorman-api/.env.local diff --git a/.gitignore b/.gitignore index d285efb..04cba20 100644 --- a/.gitignore +++ b/.gitignore @@ -100,7 +100,6 @@ web_modules/ .env.development.local .env.test.local .env.production.local -.env.local # parcel-bundler cache (https://parceljs.org/) diff --git a/packages/doorman-api/.env.local b/packages/doorman-api/.env.local new file mode 100644 index 0000000..86f563c --- /dev/null +++ b/packages/doorman-api/.env.local @@ -0,0 +1,36 @@ +# twilio stuff (not needed for dev) +# ACCOUNT_SID= +# AUTH_TOKEN= + +# aws creds (not needed for dev) +# AWS_ACCESS_KEY= +# AWS_SECRET_ACCESS_KEY= + +# discord stuff (needed for notify, edit, onboarding) +# DISCORD_BOT_TOKEN= +# DISCORD_CLIENT_SECRET= + +# use local ddb +AWS_ENDPOINT=http://localhost:5000 + +DISCORD_GUILD_ID=1299812960553795655 +DISCORD_CLIENT_ID=1299810962366398494 + +# stage is used in metrics / logs, so just set it to username + dev suffix +STAGE=$USER-dev + +# metrics +PUSHGATEWAY_URL=https://metrics.chromart.cc +PUSHGATEWAY_USER=doorman +PUSHGATEWAY_PW=doormanmetrics + +# logs +LOKI_URL=https://logs.chromart.cc +LOKI_USER=doorman +LOKI_PW=doormanlogs + +# use local UI endpoint for the redirects in dev +DOORMAN_URL=http://localhost:3005 + +# this isn't really a secret, its just to prevent spam on /notify in prod +NOTIFY_SECRET_KEY=discordnotifyme diff --git a/packages/doorman-api/.gitignore b/packages/doorman-api/.gitignore index 5bb2f0a..4b419e1 100644 --- a/packages/doorman-api/.gitignore +++ b/packages/doorman-api/.gitignore @@ -82,7 +82,6 @@ web_modules/ .env.development.local .env.test.local .env.production.local -.env.local # parcel-bundler cache (https://parceljs.org/) .cache diff --git a/packages/doorman-api/README.md b/packages/doorman-api/README.md index fa24b87..21bc473 100644 --- a/packages/doorman-api/README.md +++ b/packages/doorman-api/README.md @@ -1,4 +1,54 @@ -## deployment is serverless +# doorman-api + +The backend API for Doorman. It is resposible for managing Door state, configuring Door options and sending Discord Notifications. + +## dependency services + +The Doorman API has hard dependencies: +- DynamoDB +- Discord (notify, edit, and onboarding) +- Cloudflare (only in Production) +- Twilio Functions (deployed compute in staging / prod) + +It has some soft dependencies on other services, mostly self hosted monitoring solutions: +- Pushgateway (prometheus metrics) +- Loki (logs) + +## running locally + +First install the necessary dependencies `bun install`. + +When running locally, Doorman API will use [DynamoDBLocal](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html) + +You will likely need java installed on your machine and in your path in order for DynamoDBLocal to work. + +To run the dev server: `bun run start:local-db` + +On startup, the main "doorman" DDB table is created and seeded with the a test Door. + +Note: DynamoDBLocal is configured to use in memory store only, so any changes will not persist when you exit the command. + +The API will run on port 8080 and DynamoDBLocal will use port 5000. + +You can verify the seeding worked by calling `http://localhost:8080/api/door/info?door=test` + +If you need to test Discord or against real DDB, you should set those env vars in your environment in a seperate .env file and source it before running + +## troubleshooting + +### Port 5000 is already in use after restarting server + +When exiting `bun run start:local-db`, we try to close the previous DDB local run, though this is best efforts it may have failed to close the child process. You can manually find and kill the forked process as follows: + +``` +ps -ef | grep DynamoDBLocal + +martin 243091 243061 93 17:57 pts/4 00:00:13 java -Xrs -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -port 5000 -inMemory + +kill 243091 +``` + +## deployment this project deploys the UI and API to twilio functions https://doorman-6741-prod.twil.io diff --git a/packages/doorman-api/package.json b/packages/doorman-api/package.json index f9adecb..028981e 100644 --- a/packages/doorman-api/package.json +++ b/packages/doorman-api/package.json @@ -5,10 +5,10 @@ "scripts": { "integ-test:local": "bun run build && export $(grep -v '^#' .env.twiliotemplate | grep -v '=$' | xargs) && concurrently --success first --kill-others \"bun run start-twilio\" \"bun test --timeout 30000 ./tst/integ-local.test.ts\"", "integ-test:staging": "STAGE=staging bun test --timeout 30000 ./tst/integ-staging.test.ts", - "start-twilio": "twilio-run --load-local-env --live --port 8080", + "start-twilio": "twilio-run --load-local-env --env .env.local --live --port 8080", "watch-build": "bun run --watch src/index.ts", "start": "concurrently \"bun run watch-build\" \"bun run start-twilio\"", - "local-db": "bun run --watch src/local/localDdb.ts", + "local-db": "bun run src/local/localDdb.ts", "start:local-db": "concurrently \"bun run local-db\" \"bun run start\"", "build": "bun run src/index.ts", "deploy": "twilio-run promote --from=staging --to=prod --load-system-env --env .env.twiliotemplate", diff --git a/packages/doorman-api/src/utils/ddb.ts b/packages/doorman-api/src/utils/ddb.ts index b8d5854..9f87ed9 100644 --- a/packages/doorman-api/src/utils/ddb.ts +++ b/packages/doorman-api/src/utils/ddb.ts @@ -15,7 +15,7 @@ export const createDynaBridgeClient = (context: TwilioContext) => { }; // for local DDB - if (context.AWS_ENDPOINT) { + if (context.AWS_ENDPOINT && !context.AWS_ACCESS_KEY && !context.AWS_SECRET_ACCESS_KEY) { config = { endpoint: context.AWS_ENDPOINT, }