Skip to content

Commit

Permalink
feat: move the geo convert to memphis functions (#602)
Browse files Browse the repository at this point in the history
Co-authored-by: orig <[email protected]>
  • Loading branch information
origranot and orig authored Dec 15, 2023
1 parent 030cb67 commit 0102ba3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 155 deletions.
6 changes: 2 additions & 4 deletions apps/backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
node_modules
dist
npm-debug.log
CONTRIBUTE.MD
README.md
.gitignore
LICENSE
*.md
.git
.DS_Store
7 changes: 2 additions & 5 deletions apps/frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
node_modules
dist
npm-debug.log
CONTRIBUTE.MD
README.md
.gitignore
LICENSE
*.md
.git
.DS_Store
server
6 changes: 2 additions & 4 deletions apps/tracker/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
node_modules
dist
npm-debug.log
CONTRIBUTE.MD
README.md
.gitignore
LICENSE
*.md
.git
.DS_Store
36 changes: 9 additions & 27 deletions apps/tracker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
# --------------------------------------------
# Dependencies Stage
# --------------------------------------------
FROM node:19.2-alpine3.15 as dependencies
FROM node:lts-alpine as dependencies
WORKDIR /app

COPY package*.json ./

RUN apk add --update python3 make g++\
&& rm -rf /var/cache/apk/*
# Install Python for node-gyp
RUN apk add --no-cache python3 make g++

COPY package*.json ./
RUN npm ci

# --------------------------------------------
# Build Stage
# --------------------------------------------
# Intermediate docker image to build the bundle in and install dependencies
FROM node:19.2-alpine3.15 as build
FROM node:lts-alpine 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
RUN npx nx build tracker --prod --skip-nx-cache \
&& npm prune --production

# --------------------------------------------
# Production Stage
# --------------------------------------------
FROM node:19.2-alpine3.15 as production
FROM node:lts-alpine 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 "node tracker/main.js"







CMD ["node", "tracker/main.js"]
21 changes: 16 additions & 5 deletions apps/tracker/src/stats/stats.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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/*';

Expand All @@ -14,8 +13,13 @@ export class StatsConsumer extends ConsumerService {
}

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 { ip, userAgent, key, geo, url } = message.getDataAsJson() as {
ip: string;
userAgent: string;
key: string;
url: string;
geo: string;
};

const hashedIp = createHash('sha256').update(ip).digest('hex');
const isUniqueVisit = await this.statsService.isUniqueVisit(key, hashedIp);
Expand All @@ -24,13 +28,20 @@ export class StatsConsumer extends ConsumerService {
return;
}

const geo = geoip.lookup(ip);
let geoLocation = null;
try {
geoLocation = JSON.parse(geo);
} catch (err) {
this.loggerService.error(`Failed to parse geo location for ${key} with error: ${err.message}`);
}

await this.statsService.addVisit(key, {
hashedIp,
ua: userAgent,
geo,
...(geoLocation?.status === 'success' && geoLocation),
});

message.ack();
this.loggerService.log(`Added unique visit for ${key}`);
}
}
6 changes: 3 additions & 3 deletions apps/tracker/src/stats/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Prisma, PrismaService } from '@reduced.to/prisma';
export class StatsService {
constructor(private readonly prismaService: PrismaService) {}

async addVisit(key: string, opts: { hashedIp: string; ua: string; geo: object }) {
const { hashedIp, ua, geo } = opts;
async addVisit(key: string, opts: { hashedIp: string; ua: string; geoLocation?: object }) {
const { hashedIp, ua, geoLocation } = opts;

try {
await this.prismaService.visit.create({
data: {
ip: hashedIp,
userAgent: ua,
...(geo && { geo }),
...(geoLocation && { geo: geoLocation }),
link: {
connect: {
key,
Expand Down
105 changes: 0 additions & 105 deletions package-lock.json

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

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@types/canvas-confetti": "^1.6.1",
"@types/cookie-parser": "^1.4.4",
"@types/express": "^4.17.17",
"@types/geoip-lite": "^1.4.4",
"@types/jest": "^29.4.0",
"@types/node": "^18.17.14",
"@types/passport-google-oauth20": "^2.0.14",
Expand Down Expand Up @@ -87,7 +86,6 @@
"class-validator": "^0.14.0",
"clsx": "^2.0.0",
"cookie-parser": "^1.4.6",
"geoip-lite": "^1.4.9",
"jwt-decode": "^3.1.2",
"memphis-dev": "^1.2.2",
"passport-google-oauth20": "^2.0.0",
Expand Down

0 comments on commit 0102ba3

Please sign in to comment.