-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
96 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare namespace Express { | ||
export interface Request { | ||
tracking : string; | ||
redis : ReturnType<typeof import("redis").createClient> | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ RUN npm ci | |
|
||
COPY . . | ||
|
||
RUN npm run build | ||
|
||
EXPOSE 8080 | ||
|
||
ENTRYPOINT ["npm", "start"] |
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
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 +1,2 @@ | ||
export { router as default } from "./analytics"; | ||
export { router as default } from "./analytics"; | ||
export { AnalyticsMiddleware } from "./middleware"; |
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,32 @@ | ||
import { randomBytes } from "crypto"; | ||
import { NextFunction, Request, Response } from "express"; | ||
import { CreateDatabase } from "../../modules/database"; | ||
import { TrackingInstance } from "./types"; | ||
|
||
export async function AnalyticsMiddleware(req : Request, res : Response, next : NextFunction) { | ||
req.tracking = await getCookie(req, res); | ||
next(); | ||
} | ||
|
||
async function createTrackingInstance(req : Request, cookie : string) { | ||
const data : TrackingInstance = { | ||
id: cookie, | ||
visits: [] | ||
} | ||
|
||
await req.redis.json.set(`tracking:${cookie}`, ".", data as any); | ||
} | ||
|
||
async function getCookie(req : Request, res : Response) { | ||
let cookie = req.signedCookies["tracker"]; | ||
if(!cookie) { | ||
cookie = randomBytes(20).toString("hex"); | ||
res.cookie("tracker", cookie, { | ||
signed: true | ||
}); | ||
|
||
await createTrackingInstance(req, cookie); | ||
} | ||
|
||
return cookie as string; | ||
} |
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,10 @@ | ||
export interface TrackingInstance { | ||
id : string; | ||
visits : TrackingVisit[]; | ||
} | ||
|
||
export interface TrackingVisit { | ||
ts : number; | ||
route : string; | ||
data : any; | ||
} |
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,28 +1,15 @@ | ||
import { randomBytes } from "crypto"; | ||
import { Request, Response } from "express"; | ||
import { CreateDatabase } from "../../modules/database"; | ||
import { TrackingInstance, TrackingVisit } from "./types"; | ||
|
||
export async function getTrackingData(req : Request, res : Response) { | ||
const db = await CreateDatabase(); | ||
|
||
const cookie = getCookie(req, res); | ||
|
||
|
||
|
||
const data = await db.json.get(`tracker:${cookie}`); | ||
|
||
const data = await req.redis.json.get(`tracking:${req.tracking}`); | ||
|
||
return data; | ||
} | ||
|
||
function getCookie(req : Request, res : Response) { | ||
let cookie = req.signedCookies["tracker"]; | ||
if(!cookie) { | ||
cookie = randomBytes(20).toString("hex"); | ||
res.cookie("tracker", cookie, { | ||
signed: true | ||
}) | ||
} | ||
export async function logRequest(req : Request, res : Response, visit : TrackingVisit) { | ||
visit.ts = Date.now(); | ||
await req.redis.json.arrAppend(`tracking:${req.tracking}`, ".visits", visit as any); | ||
} | ||
|
||
return cookie as string; | ||
} |
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