feat: Migrate backend from NGINX to Deno + Hono API framework
This commit is contained in:
parent
450debbc1c
commit
4df0499d14
29
Dockerfile
29
Dockerfile
@ -1,19 +1,20 @@
|
||||
FROM nginx:alpine
|
||||
FROM denoland/deno:alpine-2.0.2
|
||||
|
||||
# Remove default nginx static assets
|
||||
RUN rm -rf /usr/share/nginx/html/*
|
||||
WORKDIR /app
|
||||
|
||||
# Copy Custom NAS 3D CAD Viewer & Static Assets
|
||||
COPY ./viewer /usr/share/nginx/html/viewer
|
||||
COPY ./data /usr/share/nginx/html/data
|
||||
COPY ./mechanical /usr/share/nginx/html/mechanical
|
||||
COPY ./bom /usr/share/nginx/html/bom
|
||||
# Cache dependencies
|
||||
COPY deno.json .
|
||||
RUN deno install || true
|
||||
|
||||
# Copy default landing redirect
|
||||
RUN echo '<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=/viewer/index.html"></head></html>' > /usr/share/nginx/html/index.html
|
||||
|
||||
# Copy optimized nginx configuration
|
||||
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
|
||||
# Copy source code and assets
|
||||
COPY main.ts .
|
||||
COPY viewer ./viewer
|
||||
COPY data ./data
|
||||
COPY mechanical ./mechanical
|
||||
COPY electrical ./electrical
|
||||
|
||||
# Expose port
|
||||
EXPOSE 80
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
|
||||
# Run the Deno app
|
||||
CMD ["task", "start"]
|
||||
|
||||
9
deno.json
Normal file
9
deno.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"tasks": {
|
||||
"start": "deno run --allow-net --allow-read --allow-env main.ts"
|
||||
},
|
||||
"imports": {
|
||||
"hono": "npm:hono@4.0.0",
|
||||
"postgres": "https://deno.land/x/postgresjs@v3.4.4/mod.js"
|
||||
}
|
||||
}
|
||||
46
main.ts
Normal file
46
main.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { Hono } from "hono";
|
||||
import { serveStatic } from "hono/deno";
|
||||
import postgres from "postgres";
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
// Connect to Postgres using env vars (fallback for dev)
|
||||
const sql = postgres({
|
||||
host: Deno.env.get("POSTGRES_HOST") || "db",
|
||||
port: parseInt(Deno.env.get("POSTGRES_PORT") || "5432"),
|
||||
database: Deno.env.get("POSTGRES_DB") || "nas_builder",
|
||||
username: Deno.env.get("POSTGRES_USER") || "nasadmin",
|
||||
password: Deno.env.get("POSTGRES_PASSWORD") || "your_secure_database_password_here",
|
||||
});
|
||||
|
||||
// API Routes
|
||||
app.get("/api/status", async (c) => {
|
||||
try {
|
||||
// Ping the database
|
||||
const result = await sql`SELECT version()`;
|
||||
return c.json({
|
||||
status: "online",
|
||||
database: "connected",
|
||||
db_version: result[0].version,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
} catch (error: any) {
|
||||
return c.json({
|
||||
status: "online",
|
||||
database: "disconnected",
|
||||
error: error.message
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
|
||||
// Serve the 3D Viewer frontend
|
||||
app.use("/*", serveStatic({ root: "./" }));
|
||||
|
||||
// Handle root redirect to viewer
|
||||
app.get("/", (c) => c.redirect("/viewer/index.html"));
|
||||
|
||||
// Start the server
|
||||
const port = parseInt(Deno.env.get("PORT") || "80");
|
||||
console.log(`🚀 NAS-Builder Deno API starting on port ${port}...`);
|
||||
|
||||
Deno.serve({ port }, app.fetch);
|
||||
27
nginx.conf
27
nginx.conf
@ -1,27 +0,0 @@
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html /viewer/index.html;
|
||||
|
||||
# Enable gzip compression for CAD models, netlists & JSON catalogs
|
||||
gzip on;
|
||||
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /viewer/index.html;
|
||||
}
|
||||
|
||||
# Cache static CAD assets and JSON catalogs
|
||||
location ~* \.(json|dxf|scad|csv|stl|obj)$ {
|
||||
expires 1d;
|
||||
add_header Cache-Control "public, no-transform";
|
||||
add_header Access-Control-Allow-Origin "*";
|
||||
}
|
||||
|
||||
error_page 500 502 503 504 /50x.html;
|
||||
location = /50x.html {
|
||||
root /usr/share/nginx/html;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user