chore: prettify
This commit is contained in:
parent
585b446ca5
commit
63aa812f5e
|
|
@ -1,26 +1,26 @@
|
||||||
import express, { Application } from 'express';
|
import express, { Application } from "express";
|
||||||
import cors from 'cors';
|
import cors from "cors";
|
||||||
import { config } from 'dotenv';
|
import { config } from "dotenv";
|
||||||
import { Config } from './types/api';
|
import { Config } from "./types/api";
|
||||||
import { generateRouter } from './routes/generate';
|
import { generateRouter } from "./routes/generate";
|
||||||
import { enhanceRouter } from './routes/enhance';
|
import { enhanceRouter } from "./routes/enhance";
|
||||||
import { textToImageRouter } from './routes/textToImage';
|
import { textToImageRouter } from "./routes/textToImage";
|
||||||
import { imagesRouter } from './routes/images';
|
import { imagesRouter } from "./routes/images";
|
||||||
import bootstrapRoutes from './routes/bootstrap';
|
import bootstrapRoutes from "./routes/bootstrap";
|
||||||
import adminKeysRoutes from './routes/admin/keys';
|
import adminKeysRoutes from "./routes/admin/keys";
|
||||||
import { errorHandler, notFoundHandler } from './middleware/errorHandler';
|
import { errorHandler, notFoundHandler } from "./middleware/errorHandler";
|
||||||
|
|
||||||
// Load environment variables
|
// Load environment variables
|
||||||
config();
|
config();
|
||||||
|
|
||||||
// Application configuration
|
// Application configuration
|
||||||
export const appConfig: Config = {
|
export const appConfig: Config = {
|
||||||
port: parseInt(process.env['PORT'] || '3000'),
|
port: parseInt(process.env["PORT"] || "3000"),
|
||||||
geminiApiKey: process.env['GEMINI_API_KEY'] || '',
|
geminiApiKey: process.env["GEMINI_API_KEY"] || "",
|
||||||
resultsDir: './results',
|
resultsDir: "./results",
|
||||||
uploadsDir: './uploads/temp',
|
uploadsDir: "./uploads/temp",
|
||||||
maxFileSize: 5 * 1024 * 1024, // 5MB
|
maxFileSize: 5 * 1024 * 1024, // 5MB
|
||||||
maxFiles: 3
|
maxFiles: 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create Express application
|
// Create Express application
|
||||||
|
|
@ -28,32 +28,34 @@ export const createApp = (): Application => {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// Middleware - CORS configuration (allow all origins)
|
// Middleware - CORS configuration (allow all origins)
|
||||||
app.use(cors({
|
app.use(
|
||||||
origin: true, // Allow all origins
|
cors({
|
||||||
credentials: true,
|
origin: true, // Allow all origins
|
||||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
credentials: true,
|
||||||
allowedHeaders: ['Content-Type', 'Authorization', 'X-API-Key'],
|
methods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||||
exposedHeaders: ['X-Request-ID']
|
allowedHeaders: ["Content-Type", "Authorization", "X-API-Key"],
|
||||||
}));
|
exposedHeaders: ["X-Request-ID"],
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
app.use(express.json({ limit: '10mb' }));
|
app.use(express.json({ limit: "10mb" }));
|
||||||
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
app.use(express.urlencoded({ extended: true, limit: "10mb" }));
|
||||||
|
|
||||||
// Request ID middleware for logging
|
// Request ID middleware for logging
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
req.requestId = Math.random().toString(36).substr(2, 9);
|
req.requestId = Math.random().toString(36).substr(2, 9);
|
||||||
res.setHeader('X-Request-ID', req.requestId);
|
res.setHeader("X-Request-ID", req.requestId);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Health check endpoint
|
// Health check endpoint
|
||||||
app.get('/health', (_req, res) => {
|
app.get("/health", (_req, res) => {
|
||||||
const health = {
|
const health = {
|
||||||
status: 'healthy',
|
status: "healthy",
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
uptime: process.uptime(),
|
uptime: process.uptime(),
|
||||||
environment: process.env['NODE_ENV'] || 'development',
|
environment: process.env["NODE_ENV"] || "development",
|
||||||
version: process.env['npm_package_version'] || '1.0.0'
|
version: process.env["npm_package_version"] || "1.0.0",
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log(`[${health.timestamp}] Health check - ${health.status}`);
|
console.log(`[${health.timestamp}] Health check - ${health.status}`);
|
||||||
|
|
@ -61,30 +63,34 @@ export const createApp = (): Application => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// API info endpoint
|
// API info endpoint
|
||||||
app.get('/api/info', async (req: any, res) => {
|
app.get("/api/info", async (req: any, res) => {
|
||||||
const info: any = {
|
const info: any = {
|
||||||
name: 'Banatie - Nano Banana Image Generation API',
|
name: "Banatie - Nano Banana Image Generation API",
|
||||||
version: '1.0.0',
|
version: "1.0.0",
|
||||||
description: 'REST API service for AI-powered image generation using Gemini Flash Image model',
|
description:
|
||||||
|
"REST API service for AI-powered image generation using Gemini Flash Image model",
|
||||||
endpoints: {
|
endpoints: {
|
||||||
'GET /health': 'Health check',
|
"GET /health": "Health check",
|
||||||
'GET /api/info': 'API information',
|
"GET /api/info": "API information",
|
||||||
'POST /api/generate': 'Generate images from text prompt with optional reference images',
|
"POST /api/generate":
|
||||||
'POST /api/text-to-image': 'Generate images from text prompt only (JSON)',
|
"Generate images from text prompt with optional reference images",
|
||||||
'POST /api/enhance': 'Enhance and optimize prompts for better image generation'
|
"POST /api/text-to-image":
|
||||||
|
"Generate images from text prompt only (JSON)",
|
||||||
|
"POST /api/enhance":
|
||||||
|
"Enhance and optimize prompts for better image generation",
|
||||||
},
|
},
|
||||||
limits: {
|
limits: {
|
||||||
maxFileSize: `${appConfig.maxFileSize / (1024 * 1024)}MB`,
|
maxFileSize: `${appConfig.maxFileSize / (1024 * 1024)}MB`,
|
||||||
maxFiles: appConfig.maxFiles,
|
maxFiles: appConfig.maxFiles,
|
||||||
supportedFormats: ['PNG', 'JPEG', 'JPG', 'WebP']
|
supportedFormats: ["PNG", "JPEG", "JPG", "WebP"],
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// If API key is provided, validate and return key info
|
// If API key is provided, validate and return key info
|
||||||
const providedKey = req.headers['x-api-key'] as string;
|
const providedKey = req.headers["x-api-key"] as string;
|
||||||
if (providedKey) {
|
if (providedKey) {
|
||||||
try {
|
try {
|
||||||
const { ApiKeyService } = await import('./services/ApiKeyService');
|
const { ApiKeyService } = await import("./services/ApiKeyService");
|
||||||
const apiKeyService = new ApiKeyService();
|
const apiKeyService = new ApiKeyService();
|
||||||
const apiKey = await apiKeyService.validateKey(providedKey);
|
const apiKey = await apiKeyService.validateKey(providedKey);
|
||||||
|
|
||||||
|
|
@ -97,7 +103,7 @@ export const createApp = (): Application => {
|
||||||
organizationSlug: apiKey.organizationSlug,
|
organizationSlug: apiKey.organizationSlug,
|
||||||
projectId: apiKey.projectId,
|
projectId: apiKey.projectId,
|
||||||
projectSlug: apiKey.projectSlug,
|
projectSlug: apiKey.projectSlug,
|
||||||
expiresAt: apiKey.expiresAt
|
expiresAt: apiKey.expiresAt,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
@ -111,16 +117,16 @@ export const createApp = (): Application => {
|
||||||
|
|
||||||
// Public routes (no authentication)
|
// Public routes (no authentication)
|
||||||
// Bootstrap route (no auth, but works only once)
|
// Bootstrap route (no auth, but works only once)
|
||||||
app.use('/api/bootstrap', bootstrapRoutes);
|
app.use("/api/bootstrap", bootstrapRoutes);
|
||||||
|
|
||||||
// Admin routes (require master key)
|
// Admin routes (require master key)
|
||||||
app.use('/api/admin/keys', adminKeysRoutes);
|
app.use("/api/admin/keys", adminKeysRoutes);
|
||||||
|
|
||||||
// Protected API routes (require valid API key)
|
// Protected API routes (require valid API key)
|
||||||
app.use('/api', generateRouter);
|
app.use("/api", generateRouter);
|
||||||
app.use('/api', enhanceRouter);
|
app.use("/api", enhanceRouter);
|
||||||
app.use('/api', textToImageRouter);
|
app.use("/api", textToImageRouter);
|
||||||
app.use('/api', imagesRouter);
|
app.use("/api", imagesRouter);
|
||||||
|
|
||||||
// Error handling middleware (must be last)
|
// Error handling middleware (must be last)
|
||||||
app.use(notFoundHandler);
|
app.use(notFoundHandler);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue