-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
109 lines (90 loc) · 3.4 KB
/
Copy pathserver.ts
File metadata and controls
109 lines (90 loc) · 3.4 KB
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
108
109
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import cors from "cors";
const ATLAS_API_KEY = process.env.ATLAS_API_KEY || "";
const ATLAS_API_URL = "https://api.atlascloud.ai/api/v1/model/generateImage";
// In-memory store for IP rate limiting
const ipUsage = new Map<string, number>();
const TRIAL_LIMIT = 3;
async function startServer() {
const app = express();
const PORT = 3000;
app.set('trust proxy', true);
app.use(express.json({ limit: '50mb' }));
app.use(cors());
// API routes
app.post("/api/generate", async (req, res) => {
const customApiKey = req.headers['x-atlas-api-key'] as string;
const clientIp = req.ip || req.socket.remoteAddress || 'unknown';
// Rate limiting logic (only if no custom API key is provided)
if (!customApiKey) {
const currentUsage = ipUsage.get(clientIp) || 0;
if (currentUsage >= TRIAL_LIMIT) {
return res.status(429).json({
error: "Trial limit reached",
message: "You have reached the 3-time free trial limit. Please provide your own Atlas Cloud API key to continue."
});
}
ipUsage.set(clientIp, currentUsage + 1);
}
const apiKeyToUse = customApiKey || ATLAS_API_KEY;
if (!apiKeyToUse) {
return res.status(401).json({
error: "Configuration Error",
message: "The server is not configured with an Atlas Cloud API key. Please provide your own API key to continue."
});
}
try {
console.log(`[Backend] Generating image for IP: ${clientIp}, Model: ${req.body.model}`);
const response = await fetch(ATLAS_API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKeyToUse}`,
},
body: JSON.stringify(req.body),
});
console.log(`[Backend] Atlas API Response Status: ${response.status}`);
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
console.error(`[Backend] Atlas API Error:`, errorData);
// If it's a 429, it's likely a quota issue
if (response.status === 429) {
return res.status(429).json({
error: "Resource exhausted",
message: "The Atlas Cloud API key has reached its quota. Please try again later or provide your own API key."
});
}
return res.status(response.status).json(errorData);
}
const result = await response.json();
console.log(`[Backend] Generation successful`);
res.json(result);
} catch (error: any) {
console.error("[Backend] Atlas Cloud API Error:", error);
res.status(500).json({
error: "Internal Server Error",
message: error.message || "An unknown error occurred on the server."
});
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), 'dist');
app.use(express.static(distPath));
app.get('*all', (req, res) => {
res.sendFile(path.join(distPath, 'index.html'));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();