Deploy chat server to Hugging Face Space
Browse files- .gitignore +5 -0
- Dockerfile +18 -0
- README.md +35 -6
- package.json +14 -0
- server.js +63 -0
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
node_modules/
|
| 2 |
+
.env
|
| 3 |
+
test_ai.js
|
| 4 |
+
test_message.json
|
| 5 |
+
*.log
|
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM node:18-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy package files
|
| 6 |
+
COPY package*.json ./
|
| 7 |
+
|
| 8 |
+
# Install dependencies
|
| 9 |
+
RUN npm install --production
|
| 10 |
+
|
| 11 |
+
# Copy server code
|
| 12 |
+
COPY server.js ./
|
| 13 |
+
|
| 14 |
+
# Expose port
|
| 15 |
+
EXPOSE 7860
|
| 16 |
+
|
| 17 |
+
# Start server
|
| 18 |
+
CMD ["node", "server.js"]
|
README.md
CHANGED
|
@@ -1,11 +1,40 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
-
license: mit
|
| 9 |
---
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Chat App Server
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# Chat App Server
|
| 11 |
+
|
| 12 |
+
Backend server for Chat App Premium - Real-time messaging with MongoDB.
|
| 13 |
+
|
| 14 |
+
## Features
|
| 15 |
+
- REST API for messages
|
| 16 |
+
- MongoDB Atlas integration
|
| 17 |
+
- CORS enabled
|
| 18 |
+
- Real-time message storage
|
| 19 |
+
|
| 20 |
+
## API Endpoints
|
| 21 |
+
|
| 22 |
+
### GET /messages
|
| 23 |
+
Returns last 50 messages
|
| 24 |
+
|
| 25 |
+
### POST /messages
|
| 26 |
+
Create new message
|
| 27 |
+
|
| 28 |
+
**Body:**
|
| 29 |
+
```json
|
| 30 |
+
{
|
| 31 |
+
"text": "Hello",
|
| 32 |
+
"senderId": "user123",
|
| 33 |
+
"senderName": "John",
|
| 34 |
+
"isAi": false
|
| 35 |
+
}
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Environment Variables
|
| 39 |
+
- `MONGO_URI`: MongoDB connection string
|
| 40 |
+
- `PORT`: Server port (default: 7860)
|
package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "chat-app-server",
|
| 3 |
+
"version": "1.0.0",
|
| 4 |
+
"description": "Chat App Backend Server",
|
| 5 |
+
"main": "server.js",
|
| 6 |
+
"scripts": {
|
| 7 |
+
"start": "node server.js"
|
| 8 |
+
},
|
| 9 |
+
"dependencies": {
|
| 10 |
+
"express": "^4.21.2",
|
| 11 |
+
"mongoose": "^8.9.4",
|
| 12 |
+
"cors": "^2.8.5"
|
| 13 |
+
}
|
| 14 |
+
}
|
server.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const mongoose = require('mongoose');
|
| 3 |
+
const cors = require('cors');
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
app.use(cors());
|
| 7 |
+
app.use(express.json());
|
| 8 |
+
|
| 9 |
+
// MongoDB Connection
|
| 10 |
+
const MONGO_URI = process.env.MONGO_URI || "mongodb+srv://ibrohm135:mansur5754@cluster0.intw8qq.mongodb.net/?appName=Cluster0";
|
| 11 |
+
|
| 12 |
+
mongoose.connect(MONGO_URI)
|
| 13 |
+
.then(() => console.log("✅ Connected to MongoDB Atlas"))
|
| 14 |
+
.catch(err => console.error("❌ MongoDB connection error:", err));
|
| 15 |
+
|
| 16 |
+
// Message Schema
|
| 17 |
+
const MessageSchema = new mongoose.Schema({
|
| 18 |
+
text: String,
|
| 19 |
+
senderId: String,
|
| 20 |
+
senderName: String,
|
| 21 |
+
isAi: Boolean,
|
| 22 |
+
timestamp: { type: Date, default: Date.now }
|
| 23 |
+
});
|
| 24 |
+
|
| 25 |
+
const Message = mongoose.model('Message', MessageSchema);
|
| 26 |
+
|
| 27 |
+
// Routes
|
| 28 |
+
app.get('/', (req, res) => {
|
| 29 |
+
res.json({
|
| 30 |
+
status: 'running',
|
| 31 |
+
message: 'Chat App Server - Hugging Face Space',
|
| 32 |
+
endpoints: {
|
| 33 |
+
messages: 'GET/POST /messages'
|
| 34 |
+
}
|
| 35 |
+
});
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
app.get('/messages', async (req, res) => {
|
| 39 |
+
try {
|
| 40 |
+
const messages = await Message.find().sort({ timestamp: -1 }).limit(50);
|
| 41 |
+
res.json(messages);
|
| 42 |
+
} catch (e) {
|
| 43 |
+
res.status(500).json({ error: e.message });
|
| 44 |
+
}
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
app.post('/messages', async (req, res) => {
|
| 48 |
+
try {
|
| 49 |
+
const { text, senderId, senderName, isAi } = req.body;
|
| 50 |
+
|
| 51 |
+
const newMessage = new Message({ text, senderId, senderName, isAi });
|
| 52 |
+
await newMessage.save();
|
| 53 |
+
|
| 54 |
+
res.json(newMessage);
|
| 55 |
+
} catch (e) {
|
| 56 |
+
res.status(500).json({ error: e.message });
|
| 57 |
+
}
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
const PORT = process.env.PORT || 7860;
|
| 61 |
+
app.listen(PORT, '0.0.0.0', () => {
|
| 62 |
+
console.log(`🚀 Server running on port ${PORT}`);
|
| 63 |
+
});
|