From 4b4d6a3ad34e540dcf95f1f30ff674b0edcca284 Mon Sep 17 00:00:00 2001 From: Arnav <158455815+rarnavneo@users.noreply.github.com> Date: Sat, 16 Mar 2024 14:10:46 +0530 Subject: [PATCH 1/2] update/docker-integration --- url-shortener/servers/express-js/Dockerfile | 5 +++-- url-shortener/servers/express-js/index.js | 10 +++++++++- .../servers/express-js/rdClient/redisClient.js | 4 ++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/url-shortener/servers/express-js/Dockerfile b/url-shortener/servers/express-js/Dockerfile index 528bc01..4f04f2a 100644 --- a/url-shortener/servers/express-js/Dockerfile +++ b/url-shortener/servers/express-js/Dockerfile @@ -1,5 +1,4 @@ -# TODO: error building; possible: node version -FROM node:21-alpine +FROM node:18 LABEL authors="arnavrneo" WORKDIR /app @@ -8,6 +7,8 @@ COPY package*.json ./ RUN npm install +COPY . . + EXPOSE 5678 CMD [ "node", "index.js" ] \ No newline at end of file diff --git a/url-shortener/servers/express-js/index.js b/url-shortener/servers/express-js/index.js index 71adf31..81739fe 100644 --- a/url-shortener/servers/express-js/index.js +++ b/url-shortener/servers/express-js/index.js @@ -16,9 +16,17 @@ const PORT = process.env.PORT; const app = express(); const allowedOrigins = ["http://localhost:3000", "http://localhost:3000/signup", "http://localhost:3000/main"] +//const allowedOrigins = ["*"] app.use(express.json()); -app.use(cors({allowedOrigins, credentials: true, origin: true})); +app.use(cors({origin: allowedOrigins, credentials: true})); //This one kills the server after cors error +// app.use(function(req, res, next) { +// res.setHeader('Access-Control-Allow-Origin', '*'); +// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); +// res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); +// res.setHeader('Access-Control-Allow-Credentials', true); +// next(); +// }); app.use(cookieParser()); mongoose.connect(process.env.MONGODB_URI) diff --git a/url-shortener/servers/express-js/rdClient/redisClient.js b/url-shortener/servers/express-js/rdClient/redisClient.js index 6e0e542..129a1de 100644 --- a/url-shortener/servers/express-js/rdClient/redisClient.js +++ b/url-shortener/servers/express-js/rdClient/redisClient.js @@ -3,8 +3,8 @@ import {createClient} from "redis"; const client = createClient({ password: process.env.REDIS_PASS, socket: { - host: 'redis-15391.c326.us-east-1-3.ec2.cloud.redislabs.com', - port: 15391, + host: process.env.REDIS_URI, + port: process.env.REDIS_PORT, tls: {rejectUnauthorized: false}, } }); From 7fb12784bf90efb4f4cb1e82717c1ae056e486b5 Mon Sep 17 00:00:00 2001 From: Arnav <158455815+rarnavneo@users.noreply.github.com> Date: Sat, 16 Mar 2024 14:49:09 +0530 Subject: [PATCH 2/2] update/frontend --- url-shortener/frontend/app/main/page.jsx | 224 +++++++++++------------ url-shortener/servers/go/Dockerfile | 13 -- 2 files changed, 112 insertions(+), 125 deletions(-) delete mode 100644 url-shortener/servers/go/Dockerfile diff --git a/url-shortener/frontend/app/main/page.jsx b/url-shortener/frontend/app/main/page.jsx index 0beb46c..a124b31 100644 --- a/url-shortener/frontend/app/main/page.jsx +++ b/url-shortener/frontend/app/main/page.jsx @@ -5,128 +5,128 @@ import FourOOne from "@/pages/Unauthorized"; import {useRouter} from "next/navigation"; function Main() { - const [userName, setUserName] = useState(''); - const [logged, setLogged] = useState(false); - const [url, setUrl] = useState(''); - const [visible, setVisible] = useState(false); - const [shortLink, setShortLink] = useState(''); - - const router = useRouter(); - - useEffect(() => { - ( - async () => { - const response = await fetch(process.env.NEXT_PUBLIC_ENDPOINT + '/user', { - credentials: 'include', - headers: {"Content-Type": "application/json"}, - }); + const [userName, setUserName] = useState(''); + const [logged, setLogged] = useState(false); + const [url, setUrl] = useState(''); + const [visible, setVisible] = useState(false); + const [shortLink, setShortLink] = useState(''); + + const router = useRouter(); + + useEffect(() => { + ( + async () => { + const response = await fetch(process.env.NEXT_PUBLIC_ENDPOINT + '/user', { + credentials: 'include', + headers: {"Content-Type": "application/json"}, + }); + + if (response.ok) { + const content = await response.json(); + setUserName(content.username); + setLogged(true) + } else { + setLogged(false) + } + } + )(); + }, []); + + + const handleShorten = async (e) => { + e.preventDefault(); + + const formData = new FormData(); + formData.append("url", url); + + try { + const response = await fetch(process.env.NEXT_PUBLIC_ENDPOINT + "/shorten", { + method: "POST", + credentials: "include", + // headers: {"Content-Type": "application/json"}, + body: formData, + }) if (response.ok) { - const content = await response.json(); - setUserName(content.username); - setLogged(true) + const data = await response.json() + console.log(data) + setShortLink(data.shorten_link) + setVisible(true) } else { - setLogged(false) + alert("url cannot be sent") } + } catch (error) { + console.log(error); } - )(); - }, []); - - - const handleShorten = async (e) => { - e.preventDefault(); - - const formData = new FormData(); - formData.append("url", url); - - try { - const response = await fetch(process.env.NEXT_PUBLIC_ENDPOINT + "/shorten", { - method: "POST", - credentials: "include", - // headers: {"Content-Type": "application/json"}, - body: formData, - }) - - if (response.ok) { - const data = await response.json() - console.log(data) - setShortLink(data.shorten_link) - setVisible(true) - } else { - alert("url cannot be sent") - } - } catch (error) { - console.log(error); - } - } - - const handleLogout = async () => { - try { - const response = await fetch(process.env.NEXT_PUBLIC_ENDPOINT + "/logout", { - credentials: "include", - method: "POST", - }) - - if (response.ok) { - console.log("logged out successfully") - router.push("/") - } - } catch (error) { - console.log(error) - } - } - - return ( -
frontend for url-shortener (in nextjs)
+frontend for url-shortener (in nextjs)
+