Spaces:
Running
Running
File size: 2,777 Bytes
e7a4412 aac78be e7a4412 b010f46 628121d b010f46 e7a4412 b010f46 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
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`);
});
|