Skip to content
Open
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
8 changes: 8 additions & 0 deletions apps/notification-service/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.dockerignore
docker-compose.yml
Dockerfile
dist/
node_modules
.env
.gitignore
.prettierignore
10 changes: 10 additions & 0 deletions apps/notification-service/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BCRYPT_SALT=10
COMPOSE_PROJECT_NAME=amp_cltq6363v0go8mn01wmywe5qg
DB_NAME=my-db
DB_PASSWORD=admin
DB_PORT=5432
DB_URL=postgres://admin:admin@localhost:5432/my-db
DB_USER=admin
JWT_EXPIRATION=2d
JWT_SECRET_KEY=Change_ME!!!
PORT=3000
5 changes: 5 additions & 0 deletions apps/notification-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

/node_modules
/dist
.DS_Store
5 changes: 5 additions & 0 deletions apps/notification-service/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
dist/
prisma/migrations/
package-lock.json
coverage/
68 changes: 68 additions & 0 deletions apps/notification-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# multi-stage: base (build)
FROM node:18.13.0 AS base

# create directory where the application will be built
WORKDIR /app

# copy over the dependency manifests, both the package.json
# and the package-lock.json are copied over
COPY package*.json ./

# installs packages and their dependencies
RUN npm install

# copy over the prisma schema
COPY prisma/schema.prisma ./prisma/

# generate the prisma client based on the schema
RUN npm run prisma:generate

# copy over the code base
COPY . .

# create the bundle of the application
RUN npm run build

# multi-stage: production (runtime)
FROM node:18.13.0-slim AS production

# create arguments of builds time variables
ARG user=amplication
ARG group=${user}
ARG uid=1001
ARG gid=$uid

# [temporary] work around to be able to run prisma
RUN apt-get update -y && apt-get install -y openssl

# create directory where the application will be executed from
WORKDIR /app

# add the user and group
RUN groupadd --gid ${gid} ${user}
RUN useradd --uid ${uid} --gid ${gid} -m ${user}

# copy over the bundled code from the build stage
COPY --from=base /app/node_modules/ ./node_modules
COPY --from=base /app/package.json ./package.json
COPY --from=base /app/dist ./dist
COPY --from=base /app/prisma ./prisma
COPY --from=base /app/scripts ./scripts
COPY --from=base /app/src ./src
COPY --from=base /app/tsconfig* ./

# change ownership of the workspace directory
RUN chown -R ${uid}:${gid} /app/

# get rid of the development dependencies
RUN npm install --production

# set user to the created non-privileged user
USER ${user}

# expose a specific port on the docker container
ENV PORT=3000
EXPOSE ${PORT}

# start the server using the previously build application
CMD [ "node", "./dist/main.js" ]
64 changes: 64 additions & 0 deletions apps/notification-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<p align="right">
<a href="https://amplication.com" target="_blank">
<img alt="amplication-logo" height="70" alt="Amplication Logo" src="https://amplication.com/images/amplication-logo-purple.svg"/>
</a>
</p>

# Introduction

This service was generated with Amplication. The server-side of the generated project. This component provides the different backend services - i.e., REST API, GraphQL API, authentication, authorization, logging, data validation and the connection to the database. Additional information about the server component and the architecture around it, can be found on the [documentation](https://docs.amplication.com/guides/getting-started) site.

# Getting started

## Step 1: Configuration

Configuration for the server component can be provided through the use of environment variables. These can be passed to the application via the use of the `.env` file in the base directory of the generated service. Below a table can be found which show the different variables that can be passed - these are the variables which exist by default, through the use of plugins additional integrations could require additional values. These values are provided default values after generation, change them to the desired values.

| Variable | Description | Value |
| -------------------- | -------------------------------------------- | ------------------------------------------------------------------- |
| BCRYPT_SALT | the string used for hashing | [random-string] |
| COMPOSE_PROJECT_NAME | the identifier of the service plus prefix | amp_[service-identifier] |
| PORT | the port on which to run the server | 3000 |
| DB_URL | the connection url for the database | [db-provider]://[username]:[password]@localhost:[db-port]/[db-name] |
| DB_PORT | the port used by the database instance | [db-provider-port] |
| DB_USER | the username used to connect to the database | [username] |
| DB_PASSWORD | the password used to connect to the database | [password] |
| DB_NAME | the name of the database | [service-name] / [project-name] |
| JWT_SECRET_KEY | the secret used to sign the json-web token | [secret] |
| JWT_EXPIRATION | the expiration time for the json-web token | 2d |

> **Note**
> Amplication generates default values and stores them under the .env file. It is advised to use some form of secrets manager/vault solution when using in production.

## Step 2.1: Scripts - pre-requisites

After configuration of the server the next step would be to run the application. Before running the server side of the component, make sure that the different pre-requisites are met - i.e., node.js [^16.x], npm, docker. After the setup of the pre-requisites the server component can be started.

```sh
# installation of the dependencies
$ npm install

# generate the prisma client
$ npm run prisma:generate
```

## Step 2.2: Scripts - local development

```sh
# start the database where the server component will connect to
$ npm run docker:dev

# initialize the database
$ npm run db:init

# start the server component
$ npm run start
```
By default, your app comes with one user with the username "admin" and password "admin".

## Step 2.2: Scripts - container based development

```shell
# start the server component as a docker container
$ npm run compose:up
```
13 changes: 13 additions & 0 deletions apps/notification-service/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3"
services:
db:
image: postgres:12
ports:
- ${DB_PORT}:5432
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres: ~
49 changes: 49 additions & 0 deletions apps/notification-service/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
version: "3"
services:
server:
build:
context: .
args:
NPM_LOG_LEVEL: notice
ports:
- ${PORT}:3000
environment:
BCRYPT_SALT: ${BCRYPT_SALT}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
JWT_SECRET_KEY: ${JWT_SECRET_KEY}
JWT_EXPIRATION: ${JWT_EXPIRATION}
depends_on:
- migrate
restart: on-failure
migrate:
build:
context: .
args:
NPM_LOG_LEVEL: notice
command: npm run db:init
working_dir: /app/server
environment:
BCRYPT_SALT: ${BCRYPT_SALT}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
depends_on:
db:
condition: service_healthy
db:
image: postgres:12
ports:
- ${DB_PORT}:5432
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- postgres:/var/lib/postgresql/data
healthcheck:
test:
- CMD-SHELL
- pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}
timeout: 45s
interval: 10s
retries: 10
volumes:
postgres: ~
6 changes: 6 additions & 0 deletions apps/notification-service/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sourceRoot": "src",
"compilerOptions": {
"assets": ["swagger"]
}
}
74 changes: 74 additions & 0 deletions apps/notification-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"name": "@notification-service/server",
"private": true,
"scripts": {
"start": "nest start",
"start:watch": "nest start --watch",
"start:debug": "nest start --debug --watch",
"build": "nest build",
"test": "jest",
"seed": "ts-node scripts/seed.ts",
"db:migrate-save": "prisma migrate dev",
"db:migrate-up": "prisma migrate deploy",
"db:clean": "prisma migrate reset",
"db:init": "run-s \"db:migrate-save -- --name 'initial version'\" db:migrate-up seed",
"prisma:generate": "prisma generate",
"docker:dev": "docker-compose -f docker-compose.dev.yml up -d",
"package:container": "docker build .",
"compose:up": "docker-compose up -d",
"compose:down": "docker-compose down --volumes"
},
"dependencies": {
"@apollo/server": "^4.9.4",
"@nestjs/apollo": "12.0.9",
"@nestjs/common": "10.2.7",
"@nestjs/config": "3.1.1",
"@nestjs/core": "10.2.7",
"@nestjs/graphql": "12.0.9",
"@nestjs/jwt": "^10.1.1",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "10.2.7",
"@nestjs/serve-static": "4.0.0",
"@nestjs/swagger": "7.1.13",
"@prisma/client": "^5.4.2",
"@types/bcrypt": "5.0.0",
"bcrypt": "5.1.1",
"class-transformer": "0.5.1",
"class-validator": "0.14.0",
"dotenv": "16.3.1",
"graphql": "^16.8.1",
"graphql-type-json": "0.3.2",
"nest-access-control": "^3.1.0",
"npm-run-all": "4.1.5",
"passport": "0.6.0",
"passport-http": "0.3.0",
"passport-jwt": "4.0.1",
"reflect-metadata": "0.1.13",
"ts-node": "10.9.1",
"type-fest": "2.19.0",
"validator": "13.11.0"
},
"devDependencies": {
"@nestjs/cli": "^10.1.18",
"@nestjs/testing": "^10.2.7",
"@types/express": "^4.17.19",
"@types/graphql-type-json": "0.3.3",
"@types/jest": "^29.5.5",
"@types/normalize-path": "3.0.0",
"@types/passport-http": "0.3.9",
"@types/passport-jwt": "3.0.10",
"@types/supertest": "^2.0.14",
"@types/validator": "^13.11.2",
"jest": "^29.7.0",
"jest-mock-extended": "^3.0.5",
"prisma": "^5.4.2",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"typescript": "~5.3.0"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node",
"modulePathIgnorePatterns": ["<rootDir>/dist/"]
}
}
26 changes: 26 additions & 0 deletions apps/notification-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
datasource db {
provider = "postgresql"
url = env("DB_URL")
}

generator client {
provider = "prisma-client-js"
}

model User {
createdAt DateTime @default(now())
firstName String?
id String @id @default(cuid())
lastName String?
password String
roles Json
updatedAt DateTime @updatedAt
username String @unique
}

model NotificationsHistory {
createdAt DateTime @default(now())
id String @id @default(cuid())
text String?
updatedAt DateTime @updatedAt
}
17 changes: 17 additions & 0 deletions apps/notification-service/scripts/customSeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PrismaClient } from "@prisma/client";

export async function customSeed() {
const client = new PrismaClient();
const username = "admin";

//replace this sample code to populate your database
//with data that is required for your service to start
await client.user.update({
where: { username: username },
data: {
username,
},
});

client.$disconnect();
}
Loading