-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: created dockerfile for tracker ms
- Loading branch information
orig
committed
Dec 11, 2023
1 parent
9dbd855
commit d92def4
Showing
16 changed files
with
363 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ProducerService } from '@reduced.to/queue-manager'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
|
||
const SHORTENER_PRODUCER_NAME = 'shortener'; | ||
const SHORTENER_QUEUE_NAME = 'stats'; | ||
|
||
@Injectable() | ||
export class ShortenerProducer extends ProducerService { | ||
constructor() { | ||
super(SHORTENER_PRODUCER_NAME, SHORTENER_QUEUE_NAME); | ||
constructor(config: AppConfigService) { | ||
super(SHORTENER_PRODUCER_NAME, config.getConfig().tracker.stats.queueName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
node_modules | ||
dist | ||
npm-debug.log | ||
CONTRIBUTE.MD | ||
README.md | ||
.gitignore | ||
LICENSE | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# -------------------------------------------- | ||
# Dependencies Stage | ||
# -------------------------------------------- | ||
FROM node:19.2-alpine3.15 as dependencies | ||
WORKDIR /app | ||
|
||
COPY package*.json ./ | ||
|
||
RUN apk add --update python3 make g++\ | ||
&& rm -rf /var/cache/apk/* | ||
|
||
RUN npm ci | ||
|
||
# -------------------------------------------- | ||
# Build Stage | ||
# -------------------------------------------- | ||
# Intermediate docker image to build the bundle in and install dependencies | ||
FROM node:19.2-alpine3.15 as build | ||
WORKDIR /app | ||
|
||
COPY . . | ||
COPY --from=dependencies /app/node_modules ./node_modules | ||
|
||
# Run prisma generate & build the bundle in production mode | ||
RUN npx nx build tracker --prod --skip-nx-cache | ||
|
||
# -------------------------------------------- | ||
# Production Stage | ||
# -------------------------------------------- | ||
FROM node:19.2-alpine3.15 as production | ||
WORKDIR /app | ||
|
||
COPY --from=build /app/dist/apps/tracker ./tracker | ||
COPY --from=build /app/node_modules ./node_modules | ||
COPY --from=build /app/libs/ ./libs | ||
|
||
EXPOSE 3001 | ||
|
||
# Start the application | ||
CMD sh -c "npx nx migrate-deploy prisma && node backend/main.js" | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConsumerService } from '@reduced.to/queue-manager'; | ||
import { AppConfigService } from '@reduced.to/config'; | ||
import { AppLoggerSerivce } from '@reduced.to/logger'; | ||
import { StatsService } from './stats.service'; | ||
import * as geoip from 'geoip-lite'; | ||
import { createHash } from 'node:crypto'; | ||
import { Message } from 'memphis-dev/*'; | ||
|
||
@Injectable() | ||
export class StatsConsumer extends ConsumerService { | ||
constructor(config: AppConfigService) { | ||
constructor(config: AppConfigService, private readonly loggerService: AppLoggerSerivce, private readonly statsService: StatsService) { | ||
super('tracker', config.getConfig().tracker.stats.queueName); | ||
} | ||
async onMessage(message: any): Promise<void> { | ||
console.log('StatsConsumer', message); | ||
|
||
async onMessage(message: Message): Promise<void> { | ||
const { ip, userAgent, key, url } = message.getDataAsJson() as { ip: string; userAgent: string; key: string; url: string }; | ||
message.ack(); | ||
|
||
const hashedIp = createHash('sha256').update(ip).digest('hex'); | ||
const isUniqueVisit = await this.statsService.isUniqueVisit(key, hashedIp); | ||
|
||
if (!isUniqueVisit) { | ||
return; | ||
} | ||
|
||
const geo = geoip.lookup(ip); | ||
await this.statsService.addVisit(key, { | ||
hashedIp, | ||
ua: userAgent, | ||
geo, | ||
}); | ||
|
||
this.loggerService.log(`Added unique visit for ${key}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,37 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '@reduced.to/prisma'; | ||
|
||
@Injectable() | ||
export class StatsService {} | ||
export class StatsService { | ||
constructor(private readonly prismaService: PrismaService) {} | ||
|
||
async addVisit(key: string, opts: { hashedIp: string; ua: string; geo: object }) { | ||
const { hashedIp, ua, geo } = opts; | ||
|
||
return this.prismaService.visit.create({ | ||
data: { | ||
ip: hashedIp, | ||
userAgent: ua, | ||
...(geo && { geo }), | ||
link: { | ||
connect: { | ||
key, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
async isUniqueVisit(key: string, hashedIp: string) { | ||
const visit = await this.prismaService.visit.findFirst({ | ||
where: { | ||
ip: hashedIp, | ||
link: { | ||
key: key, | ||
}, | ||
}, | ||
}); | ||
|
||
return visit === null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: tracker-deployment | ||
annotations: | ||
# force policy will ensure that deployment is updated | ||
# even when tag is unchanged (latest remains) | ||
keel.sh/policy: force | ||
keel.sh/trigger: poll # <-- actively query registry, otherwise defaults to webhooks | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: tracker | ||
template: | ||
metadata: | ||
labels: | ||
app: tracker | ||
spec: | ||
containers: | ||
- name: tracker | ||
image: ghcr.io/origranot/reduced.to/tracker:master | ||
imagePullPolicy: Always | ||
envFrom: | ||
- configMapRef: | ||
name: reduced-configmap | ||
ports: | ||
- containerPort: 3000 | ||
resources: | ||
requests: | ||
memory: '256Mi' | ||
cpu: '100m' | ||
limits: | ||
memory: '1024Mi' | ||
cpu: '500m' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: tracker-service | ||
spec: | ||
selector: | ||
app: tracker | ||
ports: | ||
- protocol: TCP | ||
port: 3001 | ||
targetPort: 3001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
libs/prisma/src/migrations/20231211122529_add_visits_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- AlterTable | ||
ALTER TABLE "Link" ADD COLUMN "clicks" INTEGER NOT NULL DEFAULT 0; | ||
|
||
-- CreateTable | ||
CREATE TABLE "Visit" ( | ||
"id" TEXT NOT NULL, | ||
"ip" TEXT NOT NULL, | ||
"userAgent" TEXT, | ||
"geo" JSONB, | ||
"linkId" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "Visit_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Visit_linkId_idx" ON "Visit"("linkId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "Link_url_clicks_idx" ON "Link"("url", "clicks"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Visit" ADD CONSTRAINT "Visit_linkId_fkey" FOREIGN KEY ("linkId") REFERENCES "Link"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.