Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MS-753] feat: Push saleor events to sqs and setup tmp local proxy #4

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# drizzlePlugin config
DATABASE_URL=postgresql+asyncpg://example:example@localhost:5555/example
SALEOR_URL=https://your.eu.saleor.cloud
STATIC_URL=https://your.storefront.com/public/emails
SQS_QUEUE_URL=

# awsSecretManagerPlugin config & SQS related logic
AWS_REGION
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY

SQS_QUEUE_URL=https://localhost:3001

# app config
SALEOR_URL=https://your.instance.eu.saleor.cloud
NODE_ENV=development
LOG_LEVEL=info
ENVIRONMENT=local
AWS_ACCESS_KEY_ID=
AWS_REGION=
AWS_SECRET_ACCESS_KEY=
SECRET_MANAGER_APP_CONFIG_PATH=nimara-mailer
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WORKDIR /app

RUN apk add --no-cache bash
RUN corepack enable
RUN corepack prepare pnpm@9.6.0 --activate
RUN corepack prepare pnpm@9.10.0 --activate

COPY --from=builder /app/package.json /app/package.json
COPY --from=builder /app/types/ /app/types/
Expand Down
232 changes: 29 additions & 203 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,214 +1,40 @@
## Local development

Setup required envs:

```
SALEOR_URL=https://your.eu.saleor.cloud
```

Build the app:

`$ docker compose build`

Now, you can run all of the containers in the background:

`$ docker compose up -d`

And show the output of the app:

`$ docker compose logs app -f`

### Debugging

Open chrome at [chrome://inspect/#devices](chrome://inspect/#devices) and you should se the app running. Click the inspect link and the chrome console should pop out with the debugger being attached.
More info [here](https://blog.risingstack.com/how-to-debug-a-node-js-app-in-a-docker-container/)

### Drizzle

Generate migrations

`$ docker compose run --rm app db:migrate`

Apply migrations

`$ docker compose run --rm app db:push`

### Graphql codegen

`$ docker compose run --rm app codegen`

---

### Serverless

1. Add your credentials to [`~/.aws/credentials`](https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-files.html)

2. Build the package:
`$ ./etc/build.sh

3. Deploy zip to AWS Lambda:
`$ aws lambda update-function-code --profile [YOUR PROFILE] --function-name [YOUR FUNCTION NAME] --zip-file fileb://artifact.zip`

TODO:

- [ ] Remove graphql yoga & related pacakges
- [ ] Proper readme
- [ ] Commitizen for jira
- [ ] esbuild for both lambdas

````

## Plugins

To enable one of the plugins, add it to the `plugins` array in `src/server.ts`.

### AWS Secret Manager

This plugin is used to retrieve secrets from AWS Secrets Manager. It exposes a `secretsManager` client object on the server instance, and `secretsManagerConfigPath` (secret name) on the server instance.

To enable it, add the following to the `plugins` array in `src/server.ts`:

```ts
import AWSSecretManagerPlugin from "@/lib/plugins/awsSecretManagerPlugin";

for (const [plugin, opts] of [
(...)
[AWSSecretManagerPlugin, {}],
(...)
]) {
````

The plugin will look for the following environment variables:
# Nimara Mailer

- `AWS_ACCESS_KEY_ID`
- `AWS_SECRET_ACCESS_KEY`
- `AWS_REGION`

---

### Drizzle

This plugin is used to manage the database connection. It exposes a `db` object on the server instance, which can be used to run postgress queries.

To enable it, add the following to the `plugins` array in `src/server.ts:67`:

```ts
import DrizzlePlugin from "@/lib/plugins/drizzlePlugin";

for (const [plugin, opts] of [
(...)
[DrizzlePlugin, {}],
(...)
]) {
```

The plugin will look for the following environment variables:

- `DB_HOST`
- `DB_PORT`
- `DB_USER`
- `DB_PASSWORD`
- `DB_NAME`

---

### Url

This plugin is used to generate URLs for the app. It exposes a `appUrl` and `urlFull` properties on the server instance, where:

- `appUrl` === `http://domain.com`
- `urlFull` === `http://domain.com/current/path`

To enable it, add the following to the `plugins` array in `src/server.ts:67`:

```ts
import UrlPlugin from "@/lib/plugins/urlPlugin";

for (const [plugin, opts] of [
(...)
[UrlPlugin, {}],
(...)
]) {
```

---

### UrlFor

This plugin is used to generate URLs for the app. When enabled, it will add a `urlFor` function to the request instance, which can be used to generate paths for the app via given route name.
Caveat: this plugin requires `urlPlugin` to be installed.

To enable it, add the following to the `plugins` array in `src/server.ts:67`:

```ts
import UrlForPlugin from "@/lib/plugins/urlForPlugin";

for (const [plugin, opts] of [
(...)
[UrlForPlugin, {}],
(...)
])
```

Usage:

```
fastify.withTypeProvider<ZodTypeProvider>().post(
"/register",
{
name: "saleor:register",
},
```

Then simply in the code:

```
request.urlFor("saleor:register")
```

Which results in:

```
https://your.app.com/register
```

## Providers

### Config providers

The app supports two types of config providers and u can choose the right one for you

### Secrets manager

This provider stores the app secrets and configuration in AWS Secrets Manager. To use enable it:
## Local development

1. Enable `AWSSecretManagerPlugin`.
2. Adjust the `getConfigProvider` in `/src/lib/saleor/config/provider.ts`o
1. Setup required envs. You can copy `.env.example` to `.env` and adjust it.

```ts
import { SaleorSecretsManagerConfigProvider } from "./secretsManager";
```
SALEOR_URL=<e.g. https://your.eu.saleor.cloud>
STATIC_URL=<where from emails static images will be served>
SQS_QUEUE_URL=

export const getConfigProvider = ({ server }: { server: FastifyInstance }) => SaleorSecretsManagerConfigProvider({
secretsManagerConfigPath: server.secretsManagerConfigPath,
secretsManager: server.secretsManager,
});
```
AWS_ACCESS_KEY_ID=
AWS_REGION=
AWS_SECRET_ACCESS_KEY=
SECRET_MANAGER_APP_CONFIG_PATH=<secret manager config name>
```

---
2. [`nvm use`](https://github.com/nvm-sh/nvm) - to set proper node version.
3. [`pnpm install`](https://pnpm.io/installation) - to install dependencies.
4. `pnpm dev` - to start the app.

### Drizzle
## Docker

This provider stores the app secrets and configuration in postgress database. To use enable it:
Alternatively, you can use docker to run the app.

1. Enable `DrizzlePlugin`.
2. Adjust the `getConfigProvider` in `/src/lib/saleor/config/provider.ts`o
1. Setup required envs. You can copy `.env.example` to `.env` and adjust it.

```ts
import { SaleorDrizzleConfigProvider } from "./drizzle";
```
SALEOR_URL=<e.g. https://your.eu.saleor.cloud>
STATIC_URL=<where from emails static images will be served>
SQS_QUEUE_URL=

export const getConfigProvider = ({ server }: { server: FastifyInstance }) =>
SaleorDrizzleConfigProvider({
db: server.db,
});
AWS_ACCESS_KEY_ID=
AWS_REGION=
AWS_SECRET_ACCESS_KEY=
SECRET_MANAGER_APP_CONFIG_PATH=<secret manager config name>
```

```
2. `docker compose build` - build the app.
3. `docker compose run --rm --service-ports app` - run the app.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.637.0",
"@aws-sdk/client-sqs": "^3.658.1",
"@fastify/aws-lambda": "^4.1.0",
"@fastify/error": "^4.0.0",
"dataloader": "^2.2.2",
Expand Down
Loading