Skip to content

Commit

Permalink
Merge pull request #37 from Praashh/app/deployment
Browse files Browse the repository at this point in the history
feat: nginx file, cd files
  • Loading branch information
Praashh authored Oct 8, 2024
2 parents b78fcaa + 454aa8a commit 7b56cdb
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 34 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Deploy Client

on:
push:
paths:
- "apps/client/**"
- "packages/db/**"
- "packages/order-queue/**"
branches:
- master

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: SSH and deploy
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
echo "$SSH_PRIVATE_KEY" > keyfile
chmod 600 keyfile
ssh -o StrictHostKeyChecking=no -t -i keyfile [email protected] "sudo bash /home/ubuntu/offchain-orderbook/apps/client/deploy.sh"
26 changes: 26 additions & 0 deletions .github/workflows/server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Deploy Express Backend

on:
push:
paths:
- "apps/server/**"
- "packages/db/**"
- "packages/order-queue/**"
branches:
- master

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: SSH and deploy
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
run: |
echo "$SSH_PRIVATE_KEY" > keyfile
chmod 600 keyfile
ssh -o StrictHostKeyChecking=no -t -i keyfile [email protected] "sudo bash /home/ubuntu/offchain-orderbook/apps/server/deploy.sh"
55 changes: 27 additions & 28 deletions apps/client/app/(lobby)/portfolio/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"use client";
import React, { useEffect, useState } from "react";
import React, { useCallback, useEffect, useState } from "react";
import { useSession } from "next-auth/react";
import { redirect } from "next/navigation";
import { getTrades } from "@/actions/Trade/getTrades";
import Portfolio from "../../../components/landing/Portfolio";
import { getEventDetails } from "@/actions/Event/getEventDetails";
import axios from "axios";
import {toast, Toaster} from "react-hot-toast"

export interface Trade {
id: string;
createdAt: Date;
Expand All @@ -30,40 +32,36 @@ const Page = () => {
const [loading, setLoading] = useState<boolean>(true);
const [portfolioData, setPortfolioData] = useState<Portfolio | null>(null);
const [tradesWithTitles, setTradesWithTitles] = useState<Trade[]>([]);
const { data } = useSession();
console.log(data?.user);

useEffect(() => {
if (!data?.user) {
redirect("/api/auth/signin");
}
}, [data?.user]);
const userId = "cm1r277l500178uzhh6kiewxa"; //data?.user.id;

useEffect(() => {
if (userId) {
getPortfolioDetails(userId);
}
}, [userId]);
const { status } = useSession();
const userId = "cm1r277l500178uzhh6kiewxa";

async function getPortfolioDetails(userId: string) {
const getPortfolioDetails = useCallback(async (userId: string) => {
setLoading(true);
try {
const portfolio = await getTrades(userId);
console.log(portfolio);

console.log("portfolio", portfolio);
setPortfolioData(portfolio);
if (!portfolio) {
return;
if (portfolio) {
const updatedTrades = await fetchTitles(portfolio.trades);
setTradesWithTitles(updatedTrades);
}
const updatedTrades = await fetchTitles(portfolio.trades);
setTradesWithTitles(updatedTrades);
} catch (e) {
console.log("Error fetching portfolio", e);
} finally {
setLoading(false);
}
}
}, []);

useEffect(() => {
if (status === "unauthenticated") {
redirect("/api/auth/signin");
}
if (status === "authenticated" && userId) {
getPortfolioDetails(userId);
}
}, [status, userId, getPortfolioDetails]);

async function fetchTitles(trades: Trade[]) {
const titles = await Promise.all(
trades.map(async (trade) => {
Expand All @@ -84,7 +82,7 @@ const Page = () => {
);
if (tradeToSell) {
const response = await axios.post(
"http://localhost:3001/v1/order/sell-order",
`${process.env.NEXT_PUBLIC_API_PREFIX_URL}/v1/order/sell-order`,
{
tradeId: tradeToSell.id,
eventId: tradeToSell.eventId,
Expand All @@ -93,13 +91,13 @@ const Page = () => {
side: tradeToSell.side == "YES" ? "yes" : "no",
}
);
window.alert(response.data.message || "Order sold successfully!");
toast.success(response.data.message || "Order sold successfully!");
} else {
window.alert("Trade not found.");
toast.error("Trade not found.");
}
} catch (error) {
console.error("Error selling order", error);
window.alert("Failed to sell order.");
toast.error("Failed to sell order.");
}
}
};
Expand All @@ -126,6 +124,7 @@ const Page = () => {
status :trade.status
}))}
/>
<Toaster position="top-center"/>
</div>
);
};
Expand Down
11 changes: 6 additions & 5 deletions apps/client/components/landing/Orderbook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import LineChart from "../ui/line-chart";
import { getEventDetails } from "@/actions/Event/getEventDetails";
import { ArrowUpDown } from "lucide-react";
import {toast, Toaster} from "react-hot-toast"


interface OrderBookItem {
Expand Down Expand Up @@ -77,12 +78,12 @@ export default function OrderBook({ eventId }: OrderBookProps) {
}
}
fetchInitialData();

}, [eventId]);

useEffect(() => {
const ws = new WebSocket("ws://localhost:3001");
ws.onopen = () => {
console.log("Connected to server");
ws.send(JSON.stringify({ eventId }));
};
ws.onmessage = (event: MessageEvent) => {
Expand All @@ -107,7 +108,7 @@ export default function OrderBook({ eventId }: OrderBookProps) {
};
async function handleTrade() {
const response = await axios.post(
"http://localhost:3001/v1/order/place-order",
`${process.env.NEXT_PUBLIC_API_PREFIX_URL}/v1/order/place-order`,
{
userId: "cm1r277l500178uzhh6kiewxa",
eventId : eventId,
Expand All @@ -116,11 +117,10 @@ export default function OrderBook({ eventId }: OrderBookProps) {
price: tradePrice,
}
);
console.log(response);
if(response.status === 200){
window.alert("Order placed successfully!")
toast.success("Order placed successfully!")
}else{
window.alert("Error placing order!")
toast.error("Error placing order!")
}
}

Expand Down Expand Up @@ -348,6 +348,7 @@ export default function OrderBook({ eventId }: OrderBookProps) {
<p className="text-gray-300">{description}</p>
</CardContent>
</Card>
<Toaster position="top-center"/>
</div>
);
}
8 changes: 8 additions & 0 deletions apps/client/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
export PATH=$PATH:/home/ubuntu/.nvm/versions/node/v18.20.4/bin

cd /home/ubuntu/offchain-orderbook
git pull origin master
npm run build
pm2 stop client
pm2 start npm --name "client" -- run "start:client"
8 changes: 8 additions & 0 deletions apps/server/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
export PATH=$PATH:/home/ubuntu/.nvm/versions/node/v18.20.4/bin

cd /home/ubuntu/offchain-orderbook
git pull origin master
npm run build
pm2 stop server
pm2 start npm --name "server" -- run "start:server"
37 changes: 37 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
events {
worker_connections 1024;
}

http {
# Configuration for the Next.js app
server {
server_name opinix.live;

location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

listen 80;
}

# Configuration for the Express backend
server {
server_name server.opinix.live;

location / {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

listen 80;
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"build": "turbo build",
"dev": "turbo dev",
"lint": "turbo lint",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
"start:client": "cd apps/client && npm run start",
"start:server": "cd apps/server && node dist/index.js"
},
"devDependencies": {
"prettier": "^3.2.5",
Expand Down

0 comments on commit 7b56cdb

Please sign in to comment.