Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to postgres #111

Merged
merged 6 commits into from
Aug 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DATABASE_URL="file:./dev.db"
DATABASE_URL="postgres://"
REDIS_AUTH="xxxxxx"
# NEXT_PUBLIC_MIX_PANEL_TOKEN="xxxxxx"
NEXT_PUBLIC_PLATFORM_AUTH="xxxxxx"
Expand Down
Binary file modified .env.production.gpg
Binary file not shown.
3 changes: 3 additions & 0 deletions .env.test.gpg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
� �&J��h������]�Ү�����ঈ�[U-O�ǟ�J�%��F�i�����s�ObZ)s��z.��pU؅r�{(P�T�nQ�L��#b�stt��]��k��\&�6�� V�[�w�)J�>�(�\� �y�'�l:N.�&{�D
Ӷ"'���#��S��@%6��Ԩ�Z,���M5�{�A"�Q�o�.�A���DŽ��]���9Í J�<寡h�,���r~�/>��Xp-��b�gP(E�Z-��!�T�iD|g���#Qڏ��2���D�T�Ŗ�_��V�Q$��! ^w֜NY��'��-���Yˍ_��o H�<u��I̾)���Ƨsj�<
�Q���qt��FH,f�S�k ø�K�����t��.�tm���� (�٢ |i���+�w���6����x`��<Rp����X�� rVhT�b���8[��?�����Tn��3z�Jv<� ǏP�S@��Z�h�9�+V�()�i�����ע�$���t �$�w��$��h,��&��l�,[�Y���Y��~�PGv1�
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cd-short.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Decrypt secret
run: ./scripts/decrypt_secret.sh
run: ./scripts/decrypt_secret.sh .env.production.gpg
env:
SECRET_PASSPHRASE: ${{ secrets.SECRET_PASSPHRASE }}
- name: Login to GitHub Container Registry
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Decrypt secret
run: ./scripts/decrypt_secret.sh
run: ./scripts/decrypt_secret.sh .env.production.gpg
env:
SECRET_PASSPHRASE: ${{ secrets.SECRET_PASSPHRASE }}
- name: Login to GitHub Container Registry
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
branches: [staging]

jobs:
lint:
CI:
runs-on: ubuntu-latest

steps:
Expand All @@ -19,7 +19,10 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 16.x
- name: Decrypt secret
run: ./scripts/decrypt_secret.sh .env.test.gpg
env:
SECRET_PASSPHRASE: ${{ secrets.SECRET_PASSPHRASE }}
- run: npm i
- run: npm run lint
# - run: npm run test
# - run: npm test
- run: npm run test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

encrypt.sh
*.env
*.env.test
*.env.production
prisma/*.db*
/logs
*.log
scripts/migrate/
6 changes: 4 additions & 2 deletions controllers/shorten.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UrlShortenerRecord } from '@prisma/client';
import requestIp from 'request-ip';
import prisma from '../db/prisma';
import { redis } from '../redis/client';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const handler = api<ShortenUrl>(
await redis.expire(hashShortenedLinkKey, LIMIT_SHORTENED_SECOND);
await redis.incr(keyLimit);
const keyHash = getRedisKey(REDIS_KEY.HASH_HISTORY_BY_ID, ip);
let record: UrlShortenerRecord | null = null;
// retrive client id and write to db
let clientRedisId = await redis.hget(keyHash, 'dbId');
if (!clientRedisId) {
Expand All @@ -79,7 +81,7 @@ export const handler = api<ShortenUrl>(
});
if (!clientDb) {
// new client's ip
let record = await prisma.urlShortenerRecord.findFirst({
record = await prisma.urlShortenerRecord.findFirst({
where: { ip },
});
if (!record) {
Expand All @@ -95,7 +97,7 @@ export const handler = api<ShortenUrl>(
}
const dataHashClient = ['lastUrl', url, 'lastHash', targetHash, 'dbId', clientRedisId];
await redis.hset(keyHash, dataHashClient);
let record = await prisma.urlShortenerRecord.findFirst({ where: { id: +clientRedisId } });
record = record ?? (await prisma.urlShortenerRecord.findFirst({ where: { ip } }));
if (!record)
record = await prisma.urlShortenerRecord.create({
data: { ip },
Expand Down
12 changes: 8 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module.exports = {
testEnvironment: 'node',
testPathIgnorePatterns: ['/node_modules', '/.next'],
setupFilesAfterEnv: ['./test/index.ts'],
globals: {
'ts-jest': {
isolatedModules: true,
},
transform: {
'^.+\\.{ts|tsx}?$': [
'ts-jest',
{
isolatedModules: true,
tsConfig: 'tsconfig.json',
},
],
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dev": "nodemon --exec 'next dev -p 5000'",
"test": "jest --logHeapUsage --forceExit",
"migrate:dev": "npx prisma migrate dev --name",
"migrate:status": "npx prisma migrate status",
"build": "next build",
"start": "next start",
"lint": "next lint",
Expand Down
37 changes: 0 additions & 37 deletions prisma/migrations/20230313040231_initial_migration/migration.sql

This file was deleted.

8 changes: 0 additions & 8 deletions prisma/migrations/20230313094039_unique_meta/migration.sql

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions prisma/migrations/20230320084352_delete_cascade_meta/migration.sql

This file was deleted.

44 changes: 44 additions & 0 deletions prisma/migrations/20230819040604_init_migration/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-- CreateTable
CREATE TABLE "UrlShortenerRecord" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"ip" TEXT NOT NULL,
CONSTRAINT "UrlShortenerRecord_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "UrlShortenerHistory" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"url" TEXT NOT NULL,
"hash" TEXT NOT NULL,
"email" TEXT,
"password" TEXT,
"urlShortenerRecordId" INTEGER NOT NULL,
CONSTRAINT "UrlShortenerHistory_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "UrlForwardMeta" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"userAgent" TEXT,
"countryCode" TEXT,
"ip" TEXT,
"fromClientSide" BOOLEAN DEFAULT false,
"urlShortenerHistoryId" INTEGER NOT NULL,
CONSTRAINT "UrlForwardMeta_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "UrlShortenerRecord_ip_key" ON "UrlShortenerRecord"("ip");
-- CreateIndex
CREATE UNIQUE INDEX "UrlShortenerHistory_hash_key" ON "UrlShortenerHistory"("hash");
-- CreateIndex
CREATE UNIQUE INDEX "UrlForwardMeta_userAgent_ip_urlShortenerHistoryId_key" ON "UrlForwardMeta"("userAgent", "ip", "urlShortenerHistoryId");
-- AddForeignKey
ALTER TABLE "UrlShortenerHistory"
ADD CONSTRAINT "UrlShortenerHistory_urlShortenerRecordId_fkey" FOREIGN KEY ("urlShortenerRecordId") REFERENCES "UrlShortenerRecord"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "UrlForwardMeta"
ADD CONSTRAINT "UrlForwardMeta_urlShortenerHistoryId_fkey" FOREIGN KEY ("urlShortenerHistoryId") REFERENCES "UrlShortenerHistory"("id") ON DELETE CASCADE ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
provider = "postgresql"
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ generator client {
}

datasource db {
provider = "sqlite"
provider = "postgresql"
url = env("DATABASE_URL")
}

Expand Down
3 changes: 1 addition & 2 deletions redis/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Redis, RedisOptions } from 'ioredis';
import { isProduction } from '../types/constants';

const redisConfig = {
host: isProduction ? 'cache' : '127.0.0.1',
host: process.env.REDIS_HOST,
password: process.env.REDIS_AUTH,
port: '6379',
};
Expand Down
16 changes: 11 additions & 5 deletions scripts/decrypt_secret.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#!/bin/sh

# --batch to prevent interactive command
# --yes to assume "yes" for questions
gpg --quiet --batch --yes --decrypt --passphrase="$SECRET_PASSPHRASE" \
--output ./.env.production .env.production.gpg
mv ./.env.production ./.env
if [ -z "$1" ]
then
echo "Decript failed. Host is empty!"
else
echo "Decript with host: $1...";
# --batch to prevent interactive command
# --yes to assume "yes" for questions
gpg --quiet --batch --yes --decrypt --passphrase="$SECRET_PASSPHRASE" \
--output ./.env $1
echo "Decript successfully";
fi
2 changes: 2 additions & 0 deletions scripts/encrypt_secret.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gpg --symmetric --cipher-algo AES256 .env.production
gpg --symmetric --cipher-algo AES256 .env.test
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import path from 'path';
require('dotenv').config({ path: path.resolve(__dirname, '../.env.test') });

jest.setTimeout(10000);
jest.setTimeout(20000);
8 changes: 6 additions & 2 deletions test/shorten.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('Test /api/shorten...', () => {

it('Should throw error when reached limit request', async () => {
await redis.expire(key, 0);
const requests = [];

for (let i = 0; i < 5; i++) {
let { req, res } = createMocks({
Expand All @@ -66,8 +67,9 @@ describe('Test /api/shorten...', () => {
},
headers: { 'x-forwarded-for': ip },
});
await controller.shorten.handler(req, res);
requests.push(controller.shorten.handler(req, res));
}
await Promise.all(requests);
const { req, res } = createMocks({
method: 'GET',
query: { url: 'U2FsdGVkX19lPT7tc2v+EAQ+q+S+QmgedQXJPLAhhjZDskrGAPv+kdWEm624npUtHaEGmCTcJHbFaYeZAv+FQw==' },
Expand All @@ -78,10 +80,12 @@ describe('Test /api/shorten...', () => {
});

it('Should shortened URL OK', async () => {
await redis.set(key, 0);
await redis.expire(key, 0);

const { req, res } = createMocks({
method: 'GET',
query: { url: 'U2FsdGVkX19lPT7tc2v+EAQ+q+S+QmgedQXJPLAhhjZDskrGAPv+kdWEm624npUtHaEGmCTcJHbFaYeZAv+FQw==' },
headers: { 'x-forwarded-for': ip },
});
await controller.shorten.handler(req, res);
expect(res._getStatusCode()).toBe(HttpStatusCode.OK);
Expand Down
1 change: 1 addition & 0 deletions types/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const brandUrl = 'https://clickdi.top';
export const brandUrlShort = 'https://clid.top';
export const brandUrlShortDomain = 'clid.top';
export const isProduction = process.env.NODE_ENV === 'production';
export const isTest = process.env.NODE_ENV === 'test';
export const cdnUrl = 'https://cdn.jsdelivr.net/gh/thanhdanh27600/clickdi@production/public';
export const cdn = (file: string) => `${isProduction ? cdnUrl : ''}${file}`;
export const isShortDomain = process.env.NEXT_PUBLIC_SHORT_DOMAIN === 'true';
Expand Down
5 changes: 4 additions & 1 deletion utils/axios.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
import { Response } from 'types/api';
import { z } from 'zod';
import { isProduction } from '../types/constants';
import HttpStatusCode from './statusCode';

export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
Expand All @@ -17,10 +18,12 @@ export function errorHandler<T extends Response>(
}

export const catchErrorHandler = (res: NextApiResponse, error?: any) => {
require('./loggerServer').error(error);
if (!isProduction) console.error('[Error]', error);
if (error instanceof z.ZodError) {
require('./loggerServer').warn(error);
return res.status(HttpStatusCode.BAD_REQUEST).json(error.issues);
}
require('./loggerServer').error(error);
return res
.status(HttpStatusCode.INTERNAL_SERVER_ERROR)
.json({ errorMessage: error.message || 'Something when wrong.' });
Expand Down
42 changes: 26 additions & 16 deletions utils/loggerServer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import pino from 'pino';
import { isProduction } from '../types/constants';
import { isProduction, isTest } from '../types/constants';

const fileTransport = pino.transport({
target: 'pino/file',
options: { destination: isProduction ? `pino.log` : `./logs/pino.log` },
});

module.exports = pino(
{
formatters: {
level: (label: string) => {
return { level: label.toUpperCase() };
module.exports = isTest
? {
info(message: any) {
console.log('[INFO]: ', message);
},
warn(message: any) {
console.warn('[WARN]: ', message);
},
error(message: any) {
console.error('[WARN]: ', message);
},
}
: pino(
{
formatters: {
level: (label: string) => {
return { level: label.toUpperCase() };
},
},
timestamp: pino.stdTimeFunctions.isoTime,
},
},
timestamp: pino.stdTimeFunctions.isoTime,
},
fileTransport,
);
pino.transport({
target: 'pino/file',
options: { destination: isProduction ? `pino.log` : `./logs/pino.log` },
}),
);