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
11 changes: 6 additions & 5 deletions packages/core/.env
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
BCRYPT_SALT=10
COMPOSE_PROJECT_NAME=amp_cl97vl69r38903601gw5nozm8t7
JWT_SECRET_KEY=Change_ME!!!
JWT_EXPIRATION=2d
PORT=3000
DB_USER=postgres
DB_PASSWORD=postgres
DB_URL=postgres://admin:admin@localhost:5432/my-db
DB_USER=admin
DB_PASSWORD=admin
DB_PORT=5432
DB_URL=postgres://postgres:postgres@localhost:5432/postgres
DB_NAME=my-db
JWT_SECRET_KEY=Change_ME!!!
JWT_EXPIRATION=2d
KAFKA_BROKERS=localhost:9092
KAFKA_ENABLE_SSL=false
KAFKA_CLIENT_ID=core
Expand Down
5 changes: 2 additions & 3 deletions packages/core/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.



/node_modules
/dist
.DS_Store
.DS_Store
4 changes: 2 additions & 2 deletions packages/core/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:19.1 AS base
FROM node:18.12.1 AS base

WORKDIR /app

Expand All @@ -14,7 +14,7 @@ COPY . .

RUN npm run build

FROM node:19.1 AS prod
FROM node:18.12.1 AS prod

WORKDIR /app

Expand Down
66 changes: 66 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<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
```
```sh
# generate the prisma client
$ npm run prisma:generate
```
```sh
# start the database where the server component will connect to
$ npm run docker:db
```
```sh
# initialize the database
$ npm run db:init
```

## Step 2.2: Scripts - local development

```shell
# start the server component
$ npm run install
```

## Step 2.2: Scripts - container based development

```shell
# start the server component as a docker container
$ npm run compose:up
```
14 changes: 5 additions & 9 deletions packages/core/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ services:
BCRYPT_SALT: ${BCRYPT_SALT}
JWT_SECRET_KEY: ${JWT_SECRET_KEY}
JWT_EXPIRATION: ${JWT_EXPIRATION}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5433
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
depends_on:
- migrate
migrate:
Expand All @@ -23,7 +23,7 @@ services:
working_dir: /app/server
environment:
BCRYPT_SALT: ${BCRYPT_SALT}
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432
DB_URL: postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}
depends_on:
db:
condition: service_healthy
Expand All @@ -34,17 +34,13 @@ services:
environment:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- postgres:/var/lib/postgresql/data
healthcheck:
test:
- CMD
- pg_isready
- -q
- -d
- ${DB_DB_NAME}
- -U
- ${DB_USER}
- CMD-SHELL
- pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}
timeout: 45s
interval: 10s
retries: 10
Expand Down
20 changes: 11 additions & 9 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
"@nestjs/config": "1.1.5",
"@nestjs/core": "8.4.7",
"@nestjs/graphql": "9.1.2",
"@nestjs/jwt": "8.0.0",
"@nestjs/passport": "8.2.2",
"@nestjs/platform-express": "8.4.7",
"@nestjs/serve-static": "2.2.2",
"@nestjs/swagger": "5.1.5",
Expand All @@ -39,12 +37,15 @@
"nest-morgan": "1.0.1",
"nestjs-prisma": "0.19.0",
"npm-run-all": "4.1.5",
"passport": "0.6.0",
"passport-http": "0.3.0",
"passport-jwt": "4.0.0",
"reflect-metadata": "0.1.13",
"swagger-ui-express": "4.3.0",
"ts-node": "9.1.1",
"ts-node": "10.9.1",
"validator": "^13.9.0",
"@nestjs/jwt": "^10.0.2",
"@nestjs/passport": "^9.0.0",
"passport": "0.6.0",
"passport-http": "0.3.0",
"passport-jwt": "4.0.1",
"@nestjs/microservices": "8.2.3",
"kafkajs": "2.2.0"
},
Expand All @@ -56,16 +57,17 @@
"@types/graphql-type-json": "0.3.2",
"@types/jest": "26.0.19",
"@types/normalize-path": "3.0.0",
"@types/passport-http": "0.3.9",
"@types/passport-jwt": "3.0.6",
"@types/supertest": "2.0.11",
"@types/validator": "^13.7.15",
"jest": "27.0.6",
"jest-mock-extended": "^2.0.4",
"prisma": "4.6.1",
"supertest": "4.0.2",
"ts-jest": "27.0.3",
"type-fest": "0.11.0",
"typescript": "4.2.3"
"typescript": "4.2.3",
"@types/passport-http": "0.3.9",
"@types/passport-jwt": "3.0.8"
},
"jest": {
"preset": "ts-jest",
Expand Down
45 changes: 10 additions & 35 deletions packages/core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ generator client {
}

model User {
createdAt DateTime @default(now())
firstName String?
friendRelationships FriendRelationship[] @relation(name: "acceptor")
id String @id @default(cuid())
invites FriendRelationship[] @relation(name: "inviter")
lastName String?
password String
privateMessages PrivateMessage[] @relation(name: "privateMessages")
receivedMessges PrivateMessage[] @relation(name: "receivedMessges")
roles Json
roomMessages RoomMessage[]
status EnumUserStatus?
updatedAt DateTime @updatedAt
username String @unique
createdAt DateTime @default(now())
firstName String?
id String @id @default(cuid())
lastName String?
password String
roles Json
roomMessages RoomMessage[]
status EnumUserStatus?
updatedAt DateTime @updatedAt
username String @unique
}

model Room {
Expand All @@ -40,27 +36,6 @@ model RoomMessage {
updatedAt DateTime @updatedAt
}

model PrivateMessage {
content String?
createdAt DateTime @default(now())
id String @id @default(cuid())
receiver User? @relation(name: "receivedMessges", fields: [receiverId], references: [id])
receiverId String?
sender User? @relation(name: "privateMessages", fields: [senderId], references: [id])
senderId String?
updatedAt DateTime @updatedAt
}

model FriendRelationship {
acceptor User? @relation(name: "acceptor", fields: [acceptorId], references: [id])
acceptorId String?
createdAt DateTime @default(now())
id String @id @default(cuid())
inviter User? @relation(name: "inviter", fields: [inviterId], references: [id])
inviterId String?
updatedAt DateTime @updatedAt
}

model ArcadeGame {
commands Int?
createdAt DateTime @default(now())
Expand Down
11 changes: 8 additions & 3 deletions packages/core/scripts/seed.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as dotenv from "dotenv";
import { PrismaClient } from "@prisma/client";
import { customSeed } from "./customSeed";
import { Salt, parseSalt } from "../src/auth/password.service";
import { hash } from "bcrypt";
import { customSeed } from "./customSeed";

if (require.main === module) {
dotenv.config();
Expand All @@ -12,7 +12,6 @@ if (require.main === module) {
if (!BCRYPT_SALT) {
throw new Error("BCRYPT_SALT environment variable must be defined");
}

const salt = parseSalt(BCRYPT_SALT);

seed(salt).catch((error) => {
Expand All @@ -25,16 +24,22 @@ async function seed(bcryptSalt: Salt) {
console.info("Seeding database...");

const client = new PrismaClient();

const data = {
username: "admin",
password: await hash("admin", bcryptSalt),
roles: ["user"],
};

await client.user.upsert({
where: { username: data.username },
where: {
username: data.username,
},

update: {},
create: data,
});

void client.$disconnect();

console.info("Seeding database with custom seed...");
Expand Down
16 changes: 7 additions & 9 deletions packages/core/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@ import { MorganInterceptor, MorganModule } from "nest-morgan";
import { UserModule } from "./user/user.module";
import { RoomModule } from "./room/room.module";
import { RoomMessageModule } from "./roomMessage/roomMessage.module";
import { PrivateMessageModule } from "./privateMessage/privateMessage.module";
import { FriendRelationshipModule } from "./friendRelationship/friendRelationship.module";
import { ArcadeGameModule } from "./arcadeGame/arcadeGame.module";
import { ACLModule } from "./auth/acl.module";
import { AuthModule } from "./auth/auth.module";
import { HealthModule } from "./health/health.module";
import { PrismaModule } from "./prisma/prisma.module";
import { SecretsManagerModule } from "./providers/secrets/secretsManager.module";
import { KafkaModule } from "./kafka/kafka.module";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { ServeStaticModule } from "@nestjs/serve-static";
import { ServeStaticOptionsService } from "./serveStaticOptions.service";
import { GraphQLModule } from "@nestjs/graphql";

import { ACLModule } from "./auth/acl.module";
import { AuthModule } from "./auth/auth.module";

@Module({
controllers: [],
imports: [
ACLModule,
AuthModule,
UserModule,
RoomModule,
RoomMessageModule,
PrivateMessageModule,
FriendRelationshipModule,
ArcadeGameModule,
ACLModule,
AuthModule,
HealthModule,
PrismaModule,
SecretsManagerModule,
KafkaModule,
MorganModule,
ConfigModule.forRoot({ isGlobal: true }),
ServeStaticModule.forRootAsync({
Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/arcadeGame/base/ArcadeGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ https://docs.amplication.com/how-to/custom-code
*/
import { ObjectType, Field } from "@nestjs/graphql";
import { ApiProperty } from "@nestjs/swagger";
import { IsInt, IsOptional, IsDate, IsString, IsJSON } from "class-validator";
import { IsInt, IsOptional, IsDate, IsString } from "class-validator";
import { Type } from "class-transformer";
import { IsJSONValue } from "@app/custom-validators";
import { GraphQLJSON } from "graphql-type-json";
import { JsonValue } from "type-fest";

@ObjectType()
class ArcadeGame {
@ApiProperty({
Expand Down Expand Up @@ -50,7 +52,7 @@ class ArcadeGame {
@ApiProperty({
required: false,
})
@IsJSON()
@IsJSONValue()
@IsOptional()
@Field(() => GraphQLJSON, {
nullable: true,
Expand Down Expand Up @@ -112,7 +114,7 @@ class ArcadeGame {
@ApiProperty({
required: false,
})
@IsJSON()
@IsJSONValue()
@IsOptional()
@Field(() => GraphQLJSON, {
nullable: true,
Expand All @@ -122,7 +124,7 @@ class ArcadeGame {
@ApiProperty({
required: false,
})
@IsJSON()
@IsJSONValue()
@IsOptional()
@Field(() => GraphQLJSON, {
nullable: true,
Expand All @@ -148,4 +150,5 @@ class ArcadeGame {
@Field(() => Date)
updatedAt!: Date;
}
export { ArcadeGame };

export { ArcadeGame as ArcadeGame };
Loading