vbrowser-modified / server.js
AstraOS's picture
Update server.js
628121d verified
require('dotenv').config();
const express = require('express');
const fetch = require('node-fetch');
const path = require('path');
const nhost = require('./nhostClient');
const app = express();
const PORT = process.env.PORT || 7860;
app.use(express.json());
app.use(express.static(path.join(__dirname)));
async function fetchHyperbeamData() {
try {
const response = await fetch('https://engine.hyperbeam.com/v0/vm', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.HYPERBEAM_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({})
});
if (!response.ok) {
throw new Error(`Failed to create browser: ${response.statusText}`);
}
const data = await response.json();
return data.embed_url;
} catch (error) {
console.error('Error fetching Hyperbeam data:', error);
throw error;
}
}
app.get('/embed-url', async (req, res) => {
try {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ error: 'Unauthorized' });
}
const embedURL = await fetchHyperbeamData();
res.json({ embedURL });
} catch (error) {
res.status(500).json({ error: 'Failed to fetch embed URL' });
}
});
app.post('/signup', async (req, res) => {
const { email, password } = req.body;
try {
const { user, error } = await nhost.auth.signUp({
email,
password,
});
if (error) {
return res.status(400).json({ error: error.message });
}
res.status(201).json({ message: 'Sign up successful', user });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/signin', async (req, res) => {
const { email, password } = req.body;
try {
const { session, error } = await nhost.auth.signIn({
email,
password,
});
if (error || !session) {
return res.status(400).json({ error: error?.message || 'Sign in failed' });
}
res.status(200).json({ token: session.accessToken });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/signout', async (req, res) => {
try {
const { error } = await nhost.auth.signOut();
if (error) {
return res.status(400).json({ error: error.message });
}
res.status(200).json({ message: 'Signed out successfully' });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// app.listen(PORT, () => {
// console.log(`Server running on http://localhost:${PORT}`);
// });
app.listen(PORT, '0.0.0.0', () => {
console.log(`Server running on http://0.0.0.0:7860`);
});