Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
nheek committed Oct 24, 2024
1 parent 062888f commit eccc4e5
Show file tree
Hide file tree
Showing 15 changed files with 15 additions and 67 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ jobs:
echo "DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" >> .env
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env
echo "DOMAIN=${{ secrets.DOMAIN }}" >> .env
curl -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" -O -L https://raw.githubusercontent.com/nheek/uppy/main/Dockerfile
curl -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" -O -L https://raw.githubusercontent.com/nheek/uppy/main/docker-compose.yml
curl -O -L https://raw.githubusercontent.com/nheek/uppy/main/Dockerfile
curl -O -L https://raw.githubusercontent.com/nheek/uppy/main/docker-compose.yml
mkdir .sql_data
cd .sql_data
curl -H "Authorization: token ${{ secrets.PERSONAL_ACCESS_TOKEN }}" -O -L https://raw.githubusercontent.com/nheek/uppy/main/.sql_data/create_database.sql
curl -O -L https://raw.githubusercontent.com/nheek/uppy/main/.sql_data/create_database.sql
cd ..
docker compose pull
docker compose up -d && curl -d "uppy deployed succesfully 🚀" https://ntfy.nheek.com/${{ secrets.NTFY_TOPIC }}
22 changes: 0 additions & 22 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
# Stage 1: Build Stage
FROM node:21-alpine AS build

# Set the working directory inside the container
WORKDIR /usr/src/app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Build the Next.js app
RUN npm run build

# Stage 2: Production Stage
FROM node:21-alpine AS production

# Set the working directory inside the container
WORKDIR /usr/src/app

# Install PostgreSQL client
RUN apk --no-cache add postgresql-client

# Install bash and wait-for-it script
RUN apk --no-cache add bash \
&& wget -O /usr/local/bin/wait-for-it.sh https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh \
&& chmod +x /usr/local/bin/wait-for-it.sh

# Copy built assets from the build stage
COPY --from=build /usr/src/app .

# Start PostgreSQL client and wait for it to be ready, then run the app
CMD ["sh", "-c", "pg_isready -h postgres && npm run start"]
13 changes: 0 additions & 13 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
# Stage 1: Development Stage
FROM node:21-alpine

# Set the working directory inside the container
WORKDIR /usr/src/app

# Install dependencies
COPY package*.json ./
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose the port Next.js runs on
EXPOSE 3000

# Set environment variables to enable hot-reloading
ENV CHOKIDAR_USEPOLLING=true

# Start the Next.js app in development mode
CMD ["npm", "run", "dev"]
4 changes: 0 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ services:
- postgres
env_file:
- .env
environment:
- PLACEHOLDER=YES
image: "nheek/uppy:latest"
volumes:
- uploads:/usr/src/app/public/uploads
Expand All @@ -19,8 +17,6 @@ services:
restart: unless-stopped
env_file:
- .env
environment:
PLACEHOLDER: "YES"
volumes:
- postgres_db_data:/var/lib/postgresql/data
- .sql_data:/docker-entrypoint-initdb.d/
Expand Down
1 change: 0 additions & 1 deletion src/app/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { Pool } = pkg;

dotenv.config();

// Create a PostgreSQL pool using environment variables
export const pool = new Pool({
connectionString: process.env.POSTGRES_URL,
});
1 change: 0 additions & 1 deletion src/app/api/files/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const JWT_SECRET = process.env.JWT_SECRET;

export async function GET(request: Request) {
try {
// Verify the JWT token
const authHeader = request.headers.get("authorization");
const token = authHeader?.split(" ")[1];

Expand Down
1 change: 0 additions & 1 deletion src/app/api/register/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { NextRequest, NextResponse } from "next/server";

const saltRounds = 10;

// POST method handler
export async function POST(req: NextRequest) {
const { username, password, confirmPassword } = await req.json(); // Use .json() to parse the request body

Expand Down
6 changes: 1 addition & 5 deletions src/app/api/upload-cropped/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import path from "path";
export const POST = async (request: Request) => {
try {
const data = await request.json();
const { croppedImage, savedName } = data; // assuming these are sent from the frontend

// Decode the base64 image string
const { croppedImage, savedName } = data;
const base64Data = croppedImage.replace(/^data:image\/\w+;base64,/, "");
const buffer = Buffer.from(base64Data, "base64");

// Define the upload directory
const uploadDir = path.join(process.cwd(), "public/uploads");
const filePath = path.join(uploadDir, savedName);

Expand Down
5 changes: 1 addition & 4 deletions src/app/api/upload/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const JWT_SECRET = process.env.JWT_SECRET;

export async function POST(request: Request) {
try {
// Verify the JWT token
const authHeader = request.headers.get("authorization");
const token = authHeader?.split(" ")[1];

Expand All @@ -28,8 +27,6 @@ export async function POST(request: Request) {
}

const decoded = jwt.verify(token, JWT_SECRET) as { id: number };

// Parse formData from the request
const formData = await request.formData();
const file = formData.get("file") as File | null;

Expand Down Expand Up @@ -57,7 +54,7 @@ export async function POST(request: Request) {
}

// Validate file size (10 MB limit)
const MAX_SIZE = 10 * 1024 * 1024; // 10 MB
const MAX_SIZE = 10 * 1024 * 1024;
if (file.size > MAX_SIZE) {
return NextResponse.json(
{ message: "File size exceeds the 10 MB limit" },
Expand Down
2 changes: 0 additions & 2 deletions src/app/components/FileUpload/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ const FileUpload = () => {
setIsModalVisible(true);
};

// Check if user is logged in
useEffect(() => {
const token = Cookies.get("token");
setIsLoggedIn(!!token);
setLoading(false);
}, []);

// Render a loading spinner while checking login status
if (loading) {
return (
<div className="flex flex-col items-center justify-center min-h-screen p-6">
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/Index/Index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use client"; // Ensure this is included
"use client";

import { useEffect, useState } from "react";
import Link from "next/link";
import Cookies from "js-cookie";
import { useRouter } from "next/navigation"; // Use next/navigation for client components
import { useRouter } from "next/navigation";

const Index = () => {
const [isAuthenticated, setIsAuthenticated] = useState(false);
Expand All @@ -17,7 +17,7 @@ const Index = () => {
const handleLogout = () => {
Cookies.remove("token");
setIsAuthenticated(false);
router.push("/"); // Redirect to homepage
router.push("/");
};

return (
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ interface ModalProps {
title: string;
message: string;
onClose: () => void;
children?: ReactNode; // Optional children prop
closeBtn?: boolean; // Optional closeBtn prop
children?: ReactNode;
closeBtn?: boolean;
}

const Modal: React.FC<ModalProps> = ({
Expand Down
4 changes: 1 addition & 3 deletions src/app/components/MyFiles/MyFiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ const MyFiles = () => {
setIsLoggedIn(true);
fetchFiles();
}
setLoading(false); // Once token check is done, stop loading
setLoading(false);
}, []);

// Render a loading spinner or nothing while checking login status
if (loading) {
return <div className="flex items-center justify-center min-h-screen text-xl font-bold text-blue-600">Loading...</div>;
}

// Render a message if the user is not logged in
if (!isLoggedIn) {
return (
<div className="flex flex-col items-center justify-center min-h-screen p-6">
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/Register/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useRouter } from "next/navigation";
const Register = () => {
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState(""); // New state for confirm password
const [confirmPassword, setConfirmPassword] = useState("");
const router = useRouter();

const handleRegister = async (e: React.FormEvent) => {
Expand All @@ -21,7 +21,7 @@ const Register = () => {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ username, password, confirmPassword }), // Include confirmPassword in the request
body: JSON.stringify({ username, password, confirmPassword }),
});

if (response.ok) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function RootLayout({
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="File hosting/sharing platform" />
<title>Uppy</title>
<title>Uppy - File hosting/sharing platform</title>
</head>
<body>{children}</body>
</html>
Expand Down

0 comments on commit eccc4e5

Please sign in to comment.