Skip to content

Commit

Permalink
Add dockerfile and fix a few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsingh132 committed Oct 13, 2024
1 parent e8a2127 commit faae840
Show file tree
Hide file tree
Showing 11 changed files with 551 additions and 1,299 deletions.
24 changes: 24 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
FROM node:20-alpine AS base

FROM base AS builder

RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json package-lock.json* ./
RUN npm ci
COPY . .

ENV NEXT_TELEMETRY_DISABLED=1
ENV NEXT_SHARP_PATH=/app/node_modules/sharp
ARG NEXT_PUBLIC_FRONTEND_URL
ARG NEXT_PUBLIC_BACKEND_URL
ARG NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME
ARG NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET

ENV NODE_ENV=production
RUN npm run build

FROM base AS runner
WORKDIR /app

ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV NEXT_SHARP_PATH=/app/node_modules/sharp

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

RUN mkdir .next
RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT=3000

ENV HOSTNAME=0.0.0.0

CMD ["node", "server.js"]
2 changes: 1 addition & 1 deletion app/[username]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async function UserProfile({}) {

async function getWidgetsData() {

if (!backendUrl) {
if (!backendUrl || backendUrl === "undefined") {
return {
trendingPosts: [],
randomUsersResults: [],
Expand Down
17 changes: 6 additions & 11 deletions app/api/widgets/trending/posts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,23 @@ const getTrendingPosts = (posts: PostType[]) => {
// Public route
// Get the 10 most popular posts in the last 24 hours
export async function GET(request: Request) {

if (!process.env.DATABASE_URL) {
return new NextResponse("Database connection not established.", { status: 500 });
}

try {
// Calculate the timestamp for 24 hours ago
const twentyFourHoursAgo = moment().subtract(24, "hours").toDate();

try {
// Query posts created in the last 24 hours
const posts = await db
.select()
.from(Post)
.where(and(gte(Post.createdAt, twentyFourHoursAgo)))
// const posts = await Post.find({
// createdAt: { $gte: twentyFourHoursAgo },
// });

// if no posts or less than 3 posts, return the most recent 10 posts
if (!posts || posts.length < 3) {
// const trendingPosts = await Post.find({
// // where content is not empty and content length is not greater than 50
// content: { $ne: "" },
// $expr: { $lt: [{ $strLenCP: "$content" }, 50] },
// })
// .sort({ createdAt: -1 })
// .limit(10);

const allPosts = await db.select()
.from(Post)
Expand Down
5 changes: 5 additions & 0 deletions app/api/widgets/trending/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import moment from "moment";

// Public route
export async function GET(request: Request) {

if (!process.env.DATABASE_URL) {
return new NextResponse("Database connection not established.", { status: 500 });
}

try {
// Calculate the timestamp for 24 hours ago
const twentyFourHoursAgo = moment().subtract(24, "hours").toDate();
Expand Down
5 changes: 1 addition & 4 deletions db/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { config } from "dotenv";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

config({ path: ".env.local" });

const connectionString = process.env.DATABASE_URL!;

const client = postgres(connectionString, { prepare: false });
export const db = drizzle(client);
export const db = drizzle(client);
3 changes: 0 additions & 3 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { config } from "dotenv";
import { defineConfig } from "drizzle-kit";
import type { Config } from "drizzle-kit";

config({ path: ".env.local" });

export default defineConfig({
schema: "./models/*",
out: "./supabase/migrations",
Expand Down
4 changes: 2 additions & 2 deletions jest.polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Object.defineProperties(globalThis, {
File: { value: File },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
Request: { value: Request, configurable: true},
Response: { value: Response, configurable: true },
});
3 changes: 1 addition & 2 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const { resolve } = require('path')

/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
output: 'standalone',
images: {
remotePatterns: [
{
Expand Down
Loading

0 comments on commit faae840

Please sign in to comment.