Skip to content

Commit cb584df

Browse files
VikasVikas
authored andcommitted
server
1 parent f4f2704 commit cb584df

7 files changed

Lines changed: 761 additions & 0 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ examples/test_*.txt
6666
# Google Test build artifacts
6767
_deps/
6868
lib/
69+
70+
# Server dependencies
71+
server/node_modules/
72+
server/package-lock.json

ByteCode_key.pem

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIG4gIBAAKCAYEA1LMyHF2Gpb3LOdK1hZB0UB7BE2tolp6vL7lBX+g3WqcEkgoH
3+
xC17G6W2TH7OM4qcx2XBSopiO0SGMCSIiySMMvFqAGY8MLzOUJqiDSnmslSKj23Y
4+
6qZK1C/Vcf8ElUSrrF/VWyASfmBwLMTfNjdNavka2DJ4zt2E0NsSufs9SNXpc19n
5+
ZPxmz4oFmWj1H8ISxWO/8E1gEZQyEDs1fOxuGItQ7FbM64z4uNb7mHgaZ5w8OgC6
6+
4wLJWVfdArXz1muWCkWsn/fGYWz8DDtJ76GaHeOu3jT3VdNUkqrFAbnyddBB+LZT
7+
cPPrmRB0HC4JYSydtPGPmjYA06f9Cy7mwERnXShkSmv3USDomsiYj3JcvNDa/0Sh
8+
qNMKRbRjO+ii1TsLHaq2GXEI15n4Wlc9N74MPSXeicE6sySporsIsarD0Uqs+4kw
9+
MtUlmVw3YBMx9aMMdAmal4wXm1Qw71DA+i5fG70FpwMsCDFMkUE6JuIzeJ+eJQDf
10+
nOG/A4wVQCKk79yNAgMBAAECggGASgRJzeEl+AtB609N57rLHw50teNMtA1ofT0E
11+
aPGQQbWZO5DN/xCspIRYGeqOqxTzVdgtGVlqsaE46QvtkC4Zs6ia8KSjEXrCeP42
12+
okoQg8IzrZHWSGGx7IDw0AikeaU58HQw9NMGXIAGyS2BV4aLJEyncNCvhZ1EmMGD
13+
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SR9GP7T
29+
/Ygwu2eoUfJ0nmbXzC12j0DzwFoBQPRORge4ZMng8JtJttkyngdINEVU0Rjw4+RH
30+
ZIgWjKvxylv0S6vu581NsKxVhu5ywIOuPF0VBwX/s9UCgcBQx1uMxVT/Xz3PGzpf
31+
EUgpulOwpN4hZODF6oGiaWHSAsMI2iykJNyNBQZB7nsL6GOmzdUopj8JEQYlCA0F
32+
Tr5TwQUBhL+aeKVijlRejWgcW4aCN+404FBGnVKlVteVAl8WAY0BWwkk8yJQoH7v
33+
UjI7lM4Ld/HrE4YhM0ARJYSCSztkKQp/JLTZGnWCqHLf6eE6LZnDF1iatiLItMSL
34+
g5ZCuE/H6XtkdE7wCecd9zCWIS0F2wLQl7VD8JRvhJ0JHKMCgcATN6Q1WV9RhlHp
35+
w3ruveaneeJ/ko86gOy264XIWLRf6RgdXN/ouQZOEU+rfmj2MXtIzmXVA6XNjnyw
36+
GnAFAc1oETM+OruPvMsrXaLYscp/Qh0eC+gYbFJsH73/FDcTUdGLsvtDW9RPm+A6
37+
Sp8D3uv+iIl3sEYUcEo29OUts59O1d7i0gcJTeXLHbdWSSYMA8mXU0ReY/BlK/Ou
38+
8zJgCx6QacnREyOcJn9ITriiJuJ6294tmMLkwTbPCpdKZ4THR10=
39+
-----END RSA PRIVATE KEY-----

DEPLOYMENT.md

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
# Azure Deployment Guide
2+
3+
This guide shows how to deploy the ByteCode Compiler on Azure VM with a REST API backend.
4+
5+
## Architecture
6+
7+
```
8+
┌──────────────────┐ ┌──────────────────────┐
9+
│ Frontend │ │ Azure VM │
10+
│ (Vercel) │ ─HTTP──>│ ┌────────────┐ │
11+
│ - Code Editor │ │ │ Node.js │ │
12+
│ - Submit Code │ │ │ Express │ │
13+
│ - Show Output │ │ │ API │ │
14+
└──────────────────┘ │ └─────┬──────┘ │
15+
│ │ │
16+
│ ┌─────▼──────┐ │
17+
│ │ Compiler │ │
18+
│ │ Binary │ │
19+
│ └────────────┘ │
20+
└──────────────────────┘
21+
```
22+
23+
## Step 1: Set Up Azure VM
24+
25+
### Create VM
26+
1. Go to Azure Portal
27+
2. Create a new Virtual Machine:
28+
- **OS**: Ubuntu 22.04 LTS
29+
- **Size**: Standard B2s (2 vCPUs, 4 GB RAM) or higher
30+
- **Authentication**: SSH public key
31+
3. Add inbound port rules:
32+
- Port 22 (SSH)
33+
- Port 80 (HTTP)
34+
- Port 443 (HTTPS)
35+
- Port 3000 (API - temporary, will use NGINX)
36+
37+
### Connect to VM
38+
```bash
39+
ssh azureuser@<your-vm-ip>
40+
```
41+
42+
## Step 2: Install Dependencies on Azure VM
43+
44+
```bash
45+
# Update system
46+
sudo apt update && sudo apt upgrade -y
47+
48+
# Install build tools
49+
sudo apt install -y build-essential cmake git
50+
51+
# Install Node.js
52+
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
53+
sudo apt install -y nodejs
54+
55+
# Install NGINX
56+
sudo apt install -y nginx
57+
58+
# Install certbot for SSL
59+
sudo apt install -y certbot python3-certbot-nginx
60+
```
61+
62+
## Step 3: Deploy Compiler on Azure VM
63+
64+
### Upload and build compiler
65+
```bash
66+
# Clone your repository
67+
cd ~
68+
git clone https://github.com/IsVohi/ByteCode-Compiler.git
69+
cd ByteCode-Compiler
70+
71+
# Build the compiler
72+
mkdir build && cd build
73+
cmake ..
74+
make
75+
76+
# Verify it works
77+
./compiler
78+
```
79+
80+
## Step 4: Set Up Backend API
81+
82+
### Install dependencies
83+
```bash
84+
cd ~/ByteCode-Compiler
85+
npm init -y
86+
npm install express cors body-parser
87+
```
88+
89+
### Create API server (see `server/api.js`)
90+
91+
The backend API is in the `server/` folder. It provides:
92+
- `POST /api/compile` - Compile and run code
93+
- `POST /api/repl` - Interactive REPL session
94+
- `GET /api/health` - Health check
95+
96+
## Step 5: Run Backend as a Service
97+
98+
### Create systemd service
99+
```bash
100+
sudo nano /etc/systemd/system/compiler-api.service
101+
```
102+
103+
Add:
104+
```ini
105+
[Unit]
106+
Description=ByteCode Compiler API
107+
After=network.target
108+
109+
[Service]
110+
Type=simple
111+
User=azureuser
112+
WorkingDirectory=/home/azureuser/ByteCode-Compiler/server
113+
ExecStart=/usr/bin/node api.js
114+
Restart=on-failure
115+
RestartSec=10
116+
117+
[Install]
118+
WantedBy=multi-user.target
119+
```
120+
121+
Enable and start:
122+
```bash
123+
sudo systemctl daemon-reload
124+
sudo systemctl enable compiler-api
125+
sudo systemctl start compiler-api
126+
sudo systemctl status compiler-api
127+
```
128+
129+
## Step 6: Configure NGINX
130+
131+
```bash
132+
sudo nano /etc/nginx/sites-available/compiler
133+
```
134+
135+
Add:
136+
```nginx
137+
server {
138+
listen 80;
139+
server_name your-domain.com; # Replace with your domain or IP
140+
141+
location /api {
142+
proxy_pass http://localhost:3000;
143+
proxy_http_version 1.1;
144+
proxy_set_header Upgrade $http_upgrade;
145+
proxy_set_header Connection 'upgrade';
146+
proxy_set_header Host $host;
147+
proxy_cache_bypass $http_upgrade;
148+
proxy_set_header X-Real-IP $remote_addr;
149+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
150+
}
151+
}
152+
```
153+
154+
Enable site:
155+
```bash
156+
sudo ln -s /etc/nginx/sites-available/compiler /etc/nginx/sites-enabled/
157+
sudo nginx -t
158+
sudo systemctl restart nginx
159+
```
160+
161+
## Step 7: Add SSL (Optional but Recommended)
162+
163+
```bash
164+
sudo certbot --nginx -d your-domain.com
165+
```
166+
167+
## Step 8: Test the API
168+
169+
```bash
170+
# Test health endpoint
171+
curl http://your-vm-ip/api/health
172+
173+
# Test compilation
174+
curl -X POST http://your-vm-ip/api/compile \
175+
-H "Content-Type: application/json" \
176+
-d '{"code": "let x = 10; print(x * 2);"}'
177+
```
178+
179+
## Frontend Integration
180+
181+
From your Vercel frontend, call the API:
182+
183+
```javascript
184+
const API_URL = 'https://your-domain.com/api';
185+
186+
async function compileCode(code) {
187+
const response = await fetch(`${API_URL}/compile`, {
188+
method: 'POST',
189+
headers: { 'Content-Type': 'application/json' },
190+
body: JSON.stringify({ code })
191+
});
192+
return await response.json();
193+
}
194+
```
195+
196+
## Security Considerations
197+
198+
1. **Rate Limiting**: Add rate limiting to prevent abuse
199+
2. **Timeout**: Set execution timeouts (default: 5 seconds)
200+
3. **Resource Limits**: Limit memory and CPU usage
201+
4. **Input Validation**: Sanitize input code
202+
5. **Firewall**: Configure Azure NSG properly
203+
6. **SSL**: Always use HTTPS in production
204+
205+
## Monitoring
206+
207+
```bash
208+
# View API logs
209+
sudo journalctl -u compiler-api -f
210+
211+
# View NGINX logs
212+
sudo tail -f /var/log/nginx/access.log
213+
sudo tail -f /var/log/nginx/error.log
214+
215+
# Monitor system resources
216+
htop
217+
```
218+
219+
## Updating the Compiler
220+
221+
```bash
222+
cd ~/ByteCode-Compiler
223+
git pull origin main
224+
cd build
225+
cmake ..
226+
make
227+
sudo systemctl restart compiler-api
228+
```
229+
230+
## Troubleshooting
231+
232+
### API not responding
233+
```bash
234+
sudo systemctl status compiler-api
235+
sudo journalctl -u compiler-api -n 50
236+
```
237+
238+
### Build errors
239+
```bash
240+
cd ~/ByteCode-Compiler/build
241+
cmake ..
242+
make clean && make
243+
```
244+
245+
### Port already in use
246+
```bash
247+
sudo lsof -i :3000
248+
sudo kill -9 <PID>
249+
```
250+
251+
## Cost Estimation
252+
253+
- **Azure VM B2s**: ~$30-40/month
254+
- **Domain**: ~$10-15/year
255+
- **SSL**: Free (Let's Encrypt)
256+
257+
**Total**: ~$30-40/month
258+
259+
---
260+
261+
**Next Steps:**
262+
1. Follow this guide to deploy on Azure
263+
2. Create frontend (see `frontend/` folder)
264+
3. Deploy frontend on Vercel
265+
4. Connect frontend to Azure backend

0 commit comments

Comments
 (0)