Spaces:
Sleeping
Sleeping
Yogesh commited on
Commit ·
bc3c24f
1
Parent(s): 6157216
Configure Space: add filesystem JSON database catalog endpoints
Browse files
server.js
CHANGED
|
@@ -2,6 +2,8 @@ import express from 'express';
|
|
| 2 |
import cors from 'cors';
|
| 3 |
import dotenv from 'dotenv';
|
| 4 |
import { HfInference } from '@huggingface/inference';
|
|
|
|
|
|
|
| 5 |
|
| 6 |
// Load environment variables
|
| 7 |
dotenv.config();
|
|
@@ -13,14 +15,96 @@ const PORT = process.env.PORT || 5000;
|
|
| 13 |
app.use(cors());
|
| 14 |
app.use(express.json());
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
// Initialize Hugging Face Inference using the free serverless Token
|
| 17 |
const hf = new HfInference(process.env.HF_API_KEY || '');
|
| 18 |
|
| 19 |
// Health Check Endpoint
|
| 20 |
app.get('/api/health', (req, res) => {
|
| 21 |
-
res.json({ status: 'ok', hfConfigured: !!process.env.HF_API_KEY });
|
| 22 |
});
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
// 1. FREE TEXT GENERATION API (Uses Meta Llama 3)
|
| 25 |
app.post('/api/generate-text', async (req, res) => {
|
| 26 |
const { prompt, maxTokens = 800 } = req.body;
|
|
|
|
| 2 |
import cors from 'cors';
|
| 3 |
import dotenv from 'dotenv';
|
| 4 |
import { HfInference } from '@huggingface/inference';
|
| 5 |
+
import fs from 'fs';
|
| 6 |
+
import path from 'path';
|
| 7 |
|
| 8 |
// Load environment variables
|
| 9 |
dotenv.config();
|
|
|
|
| 15 |
app.use(cors());
|
| 16 |
app.use(express.json());
|
| 17 |
|
| 18 |
+
// JSON File Database Configuration
|
| 19 |
+
const DB_FILE = path.join(process.cwd(), 'db.json');
|
| 20 |
+
|
| 21 |
+
const readDb = () => {
|
| 22 |
+
try {
|
| 23 |
+
if (!fs.existsSync(DB_FILE)) {
|
| 24 |
+
const defaultCatalog = [
|
| 25 |
+
{
|
| 26 |
+
id: '1',
|
| 27 |
+
title: 'ADHD Daily Focus Study Planner 2026',
|
| 28 |
+
subtitle: 'The Ultimate Aesthetic Organization Notebook for Neurodivergent College Students',
|
| 29 |
+
type: 'planner',
|
| 30 |
+
price: 9.99,
|
| 31 |
+
date: new Date().toLocaleDateString()
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
id: '2',
|
| 35 |
+
title: 'AWS Certified Solutions Architect Study Guide SAA-C03',
|
| 36 |
+
subtitle: 'The 30-Day Cram Sheet & High-Performance Practice Exam Bank',
|
| 37 |
+
type: 'study_guide',
|
| 38 |
+
price: 14.99,
|
| 39 |
+
date: new Date().toLocaleDateString()
|
| 40 |
+
}
|
| 41 |
+
];
|
| 42 |
+
fs.writeFileSync(DB_FILE, JSON.stringify(defaultCatalog, null, 2));
|
| 43 |
+
return defaultCatalog;
|
| 44 |
+
}
|
| 45 |
+
return JSON.parse(fs.readFileSync(DB_FILE, 'utf8'));
|
| 46 |
+
} catch (err) {
|
| 47 |
+
return [];
|
| 48 |
+
}
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
const writeDb = (data) => {
|
| 52 |
+
fs.writeFileSync(DB_FILE, JSON.stringify(data, null, 2));
|
| 53 |
+
};
|
| 54 |
+
|
| 55 |
// Initialize Hugging Face Inference using the free serverless Token
|
| 56 |
const hf = new HfInference(process.env.HF_API_KEY || '');
|
| 57 |
|
| 58 |
// Health Check Endpoint
|
| 59 |
app.get('/api/health', (req, res) => {
|
| 60 |
+
res.json({ status: 'ok', hfConfigured: !!process.env.HF_API_KEY, dbConnected: true });
|
| 61 |
});
|
| 62 |
|
| 63 |
+
// ----------------------------------------------------
|
| 64 |
+
// DATABASE CATALOG ENDPOINTS ("AI DB")
|
| 65 |
+
// ----------------------------------------------------
|
| 66 |
+
|
| 67 |
+
app.get('/api/catalog', (req, res) => {
|
| 68 |
+
const catalog = readDb();
|
| 69 |
+
res.json(catalog);
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
app.post('/api/catalog', (req, res) => {
|
| 73 |
+
const { title, subtitle, type, price } = req.body;
|
| 74 |
+
if (!title) {
|
| 75 |
+
return res.status(400).json({ error: 'Book title is required' });
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
const catalog = readDb();
|
| 79 |
+
const newBook = {
|
| 80 |
+
id: Date.now().toString(),
|
| 81 |
+
title,
|
| 82 |
+
subtitle: subtitle || '',
|
| 83 |
+
type: type || 'other',
|
| 84 |
+
price: price || 9.99,
|
| 85 |
+
date: new Date().toLocaleDateString()
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
catalog.unshift(newBook);
|
| 89 |
+
writeDb(catalog);
|
| 90 |
+
res.status(201).json(newBook);
|
| 91 |
+
});
|
| 92 |
+
|
| 93 |
+
app.delete('/api/catalog/:id', (req, res) => {
|
| 94 |
+
const { id } = req.params;
|
| 95 |
+
let catalog = readDb();
|
| 96 |
+
const originalLength = catalog.length;
|
| 97 |
+
catalog = catalog.filter(book => book.id !== id);
|
| 98 |
+
|
| 99 |
+
if (catalog.length === originalLength) {
|
| 100 |
+
return res.status(404).json({ error: 'Book not found' });
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
writeDb(catalog);
|
| 104 |
+
res.json({ success: true });
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
|
| 108 |
// 1. FREE TEXT GENERATION API (Uses Meta Llama 3)
|
| 109 |
app.post('/api/generate-text', async (req, res) => {
|
| 110 |
const { prompt, maxTokens = 800 } = req.body;
|