Skip to content

Commit

Permalink
feat: add basic counter setup
Browse files Browse the repository at this point in the history
  • Loading branch information
alex73630 committed Jan 17, 2023
1 parent 6425730 commit 4afbeb1
Show file tree
Hide file tree
Showing 112 changed files with 1,783 additions and 1,619 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NODE_ENV=development
API_DOMAIN=localhost
PORT=3000
CORS_ORIGINS=http://localhost,http://localhost:3000
CORS_ORIGINS=http://localhost,http://localhost:3000,https://piquetde.stream

REDIS_URL=redis://localhost:6379
4 changes: 2 additions & 2 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ processes = []
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
hard_limit = 100
soft_limit = 90
type = "connections"

[[services.ports]]
Expand Down
15 changes: 9 additions & 6 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": [
"@nestjs/swagger"
],
"deleteOutDir": true
}
}
69 changes: 66 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
"@nestjs/config": "^2.2.0",
"@nestjs/core": "^9.2.1",
"@nestjs/platform-express": "^9.2.1",
"@nestjs/swagger": "^6.1.4",
"axios": "^1.2.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dayjs": "^1.11.7",
"helmet": "^6.0.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { AppController } from "./app.controller"
import { AppService } from "./app.service"
import { ExtendedConfigModule } from "./config/config.module"
import { HelloAssoModule } from "./helloasso/helloasso.module"
import { CounterModule } from "./counter/counter.module"

@Module({
imports: [ExtendedConfigModule, HelloAssoModule],
imports: [ExtendedConfigModule, HelloAssoModule, CounterModule],
controllers: [AppController],
providers: [AppService]
})
Expand Down
20 changes: 20 additions & 0 deletions src/counter/counter.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Test, TestingModule } from "@nestjs/testing"
import { CounterController } from "./counter.controller"
import { CounterService } from "./counter.service"

describe("CounterController", () => {
let controller: CounterController

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CounterService],
controllers: [CounterController]
}).compile()

controller = module.get<CounterController>(CounterController)
})

it("should be defined", () => {
expect(controller).toBeDefined()
})
})
24 changes: 24 additions & 0 deletions src/counter/counter.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller, Get, Sse } from "@nestjs/common"
import { ApiTags } from "@nestjs/swagger"
import { from, map, Observable } from "rxjs"
import { CounterService } from "./counter.service"
import { CounterMessage, CounterMessagePayload } from "./interfaces/counter-message.interface"

@ApiTags("Counter")
@Controller("counter")
export class CounterController {
constructor(private readonly counterService: CounterService) {}

@Sse("sse")
counterSse(): Observable<CounterMessage> {
return from(this.counterService.counterSubject).pipe(map((data) => data))
}

@Get("state")
getState(): CounterMessagePayload {
return {
amount: 0,
updatedAt: Date.now()
}
}
}
9 changes: 9 additions & 0 deletions src/counter/counter.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from "@nestjs/common"
import { CounterController } from "./counter.controller"
import { CounterService } from "./counter.service"

@Module({
controllers: [CounterController],
providers: [CounterService]
})
export class CounterModule {}
18 changes: 18 additions & 0 deletions src/counter/counter.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from "@nestjs/testing"
import { CounterService } from "./counter.service"

describe("CounterService", () => {
let service: CounterService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CounterService]
}).compile()

service = module.get<CounterService>(CounterService)
})

it("should be defined", () => {
expect(service).toBeDefined()
})
})
27 changes: 27 additions & 0 deletions src/counter/counter.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Injectable } from "@nestjs/common"
import { Subject } from "rxjs"
import { CounterMessage, CounterMessagePayload } from "./interfaces/counter-message.interface"

@Injectable()
export class CounterService {
public counterSubject: Subject<CounterMessage>

constructor() {
this.counterSubject = new Subject()
}

public updateCounter(amount: number): void {
const payload: CounterMessagePayload = {
amount,
updatedAt: Date.now()
}
this.counterSubject.next({ data: payload, type: "counter-update" })
}

public newDonation(amount: number): void {
const payload: CounterMessagePayload = {
amount
}
this.counterSubject.next({ data: payload, type: "new-donation" })
}
}
29 changes: 29 additions & 0 deletions src/counter/interfaces/counter-message.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export interface NewDonationEvent {
data: NewDonationPayload
id?: string
type: "new-donation"
retry?: number
}

export interface NewDonationPayload {
// Amount of the donation in euros
amount: number
}

export interface CounterUpdateEvent {
data: CounterUpdatePayload
id?: string
type: "counter-update"
retry?: number
}

export interface CounterUpdatePayload {
// Total amount of donations in euros
amount: number
// Timestamp in milliseconds
updatedAt: number
}

export type CounterMessage = NewDonationEvent | CounterUpdateEvent

export type CounterMessagePayload = NewDonationPayload | CounterUpdatePayload
Loading

0 comments on commit 4afbeb1

Please sign in to comment.