我有一个项目与docker和bun,一个网站和一个API,都是用bun编译的。该网站是与天文和API只是小面包。我的问题是,它没有检测到的文件中的变化,无论我有多少尝试了每一种方式,我在互联网上找到的。应该注意的是,如果我独立启动项目,它们确实会检测到热重载。
我将留下我用于此项目的文件:
结构:
.
├── README.md
├── api
│ ├── Dockerfile
│ ├── bun.lockb
│ ├── package.json
│ ├── src
│ │ └── index.ts
│ └── tsconfig.json
├── docker-compose.yml
├── nginx
│ └── nginx.conf
└── web
├── Dockerfile
├── README.md
├── astro.config.mjs
├── bun.lockb
├── index.html
├── package.json
├── public
│ └── favicon.svg
├── src
│ ├── components
│ │ ├── Card.astro
│ │ ├── Post.vue
│ │ ├── Posts.vue
│ │ ├── atoms
│ │ │ └── Button.vue
│ │ ├── molecules
│ │ ├── organisms
│ │ └── templates
│ ├── env.d.ts
│ ├── layouts
│ │ └── Layout.astro
│ ├── pages
│ │ ├── api
│ │ │ └── posts
│ │ │ ├── [id].ts
│ │ │ └── index.ts
│ │ ├── index.astro
│ │ └── posts
│ │ ├── [id].astro
│ │ └── index.astro
│ └── utils
├── tsconfig.json
└── tsconfig.node.json
17 directories, 28 files
# Path: docker-compose.yml
version: '3.8'
networks:
cv_network:
driver: bridge
volumes:
web_node_modules:
api_node_modules:
services:
web:
container_name: cv_web
build: web
image: web
restart: "no"
networks:
- cv_network
volumes:
- ./web/:/app
- web_node_modules:/app/node_modules
ports:
- 3000:3000
env_file:
- project.env
api:
container_name: cv_api
build: api
image: api
restart: "no"
networks:
- cv_network
volumes:
- ./api/:/app
- api_node_modules:/app/node_modules
ports:
- 8000:8000
nginx:
container_name: cv_nginx
image: nginx:1.25.2-alpine-slim
networks:
- cv_network
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- web
- api
ports:
- 7800:7800
# Path: api/Dockerfile
FROM oven/bun:1.0.3-slim
WORKDIR /api
COPY package*.json bun.lockb ./
RUN bun install
COPY . .
CMD [ "bun", "run", "dev" ]
路径:api/package.json
{
"name": "bun_app",
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun --watch run src/index.ts"
},
"dependencies": {
},
"devDependencies": {
"bun-types": "1.0.3"
},
"module": "src/index.js"
}
// Path: api/src/index.ts
import { type Serve } from "bun";
type Middleware = (req: any, res: any, next: () => void) => void;
type RequestHandler = (req: any, res: any) => Response;
const loggerMiddleware: Middleware = (req, res, next) => {
const url = new URL(req.url);
const method = req.method;
const currentTime = new Date().toISOString();
console.log(`[${currentTime}] ${method} - ${url.pathname}`);
next();
};
class Router {
routes: { [method: string]: { [path: string]: RequestHandler } } = {};
get(path: string, handler: RequestHandler) {
this.addRoute("GET", path, handler);
}
post(path: string, handler: RequestHandler) {
this.addRoute("POST", path, handler);
}
put(path: string, handler: RequestHandler) {
this.addRoute("PUT", path, handler);
}
private addRoute(method: string, path: string, handler: RequestHandler) {
if (!this.routes[method]) {
this.routes[method] = {};
}
this.routes[method][path] = handler;
}
}
class Server {
middlewares: Middleware[] = [];
routes: { [method: string]: { [path: string]: RequestHandler } } = {};
use(middleware: Middleware) {
this.middlewares.push(middleware);
}
get(path: string, handler: RequestHandler) {
this.addRoute("GET", path, handler);
}
post(path: string, handler: RequestHandler) {
this.addRoute("POST", path, handler);
}
// Agregar más métodos HTTP según sea necesario
private addRoute(method: string, path: string, handler: RequestHandler) {
if (!this.routes[method]) {
this.routes[method] = {};
}
this.routes[method][path] = handler;
}
useRouter(path: string, router: Router) {
for (const [method, routes] of Object.entries(router.routes)) {
for (const [routePath, handler] of Object.entries(routes)) {
const fullPath = path + routePath;
this.addRoute(method, fullPath, handler);
}
}
}
async fetch(request: Request, server: Server): Promise<Response> {
try {
if (!request || !server) {
console.error("Request o Server no definidos");
return new Response("Internal Server Error", { status: 500 });
}
const url = new URL(request.url);
const pathname = url.pathname;
const method = request.method;
let response: Response;
// Ejecutamos los middlewares en orden
for (const middleware of this.middlewares) {
middleware(request, response, () => {});
}
if (this.routes[method] && this.routes[method][pathname]) {
const handler = this.routes[method][pathname];
response = handler(request, new Response(""));
} else {
throw new Error("Route not found"); // Lanza un error si la ruta no se encuentra
}
return response;
} catch (error) {
console.error(`Error: ${error.message}`);
return new Response(error.message, { status: 500 });
}
}
}
const app = new Server();
app.use(loggerMiddleware);
// Agregar middlewares
app.use((req, res, next) => {
console.log("Middleware 1");
next();
});
// Agregar rutas
app.get("/", (req, res) => {
res = new Response("Hello, world!", { status: 200 });
return res;
});
const router = new Router();
router.get("", (req, res) => {
return new Response(JSON.stringify({ message: "aaaaaaaaaa" }), {
status: 200,
});
});
router.post("", (req, res) => {
return new Response(JSON.stringify({ message: "Router POST succeed" }), {
status: 200,
});
});
router.put("", (req, res) => {
return new Response(JSON.stringify({ message: "Router PUT succeed" }), {
status: 200,
});
});
app.useRouter("/test", router);
// Iniciar el servidor
export default {
port: 8000,
fetch(req) {
return app.fetch(req, app);
},
error(error: Error) {
return new Response(`<pre>${error}\n${error.stack}</pre>`, {
headers: {
"Content-Type": "text/html",
},
});
},
} satisfies Serve;
# Path: nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
# proxy_cache_path /var/cache/nginx keys_zone=astro_ssg_cache:1m inactive=5m max_size=512m;
upstream web {
server web:3000;
}
upstream api {
server api:8000;
}
server {
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
gzip_min_length 1000;
listen 7800;
server_name localhost;
location / {
proxy_pass http://web;
proxy_http_version 1.1;
# proxy_cache astro_ssg_cache;
# proxy_cache_key $uri;
# proxy_cache_valid 5m;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
proxy_redirect off;
}
}
# Para la API
server {
listen 7800;
server_name api.localhost;
location / {
proxy_pass http://api; # Suponiendo que "api" es el nombre del servicio en docker-compose.yml
# ... (otros ajustes de proxy aquí)
}
}
# Para el panel de administración
server {
listen 7800;
server_name admin.localhost;
location / {
proxy_pass http://web; # Asumiendo que "admin" es otro servicio en docker-compose.yml
# ... (otros ajustes de proxy aquí)
}
}
# Para otros subdominios dinámicos
server {
listen 7800;
server_name ~^(?<subdomain>.+)\.localhost$;
location / {
proxy_pass http://web; # Redirige todos los demás subdominios al servicio "web"
# ... (otros ajustes de proxy aquí)
}
}
}
- SO:Windows 11
- WSL2:
- 经销商ID:Ubuntu
- 产品描述:Ubuntu 20.04.6 LTS
- 发布时间:20.04
- 代号:病灶
2条答案
按热度按时间pbpqsu0x1#
你能把手表换成......热的。热将软重新加载代码。
ru9i0ody2#
解决热点问题
在查看了这些文件后,我发现了导致Hot Pandroid问题的差异。以下是使一切按预期工作的必要更改。
docker-compose.yml
首先,在
docker-compose.yml
文件中,我需要更新卷路径以匹配容器中的工作目录。之前:
之后:
web
和api
中的Dockerfileapi
中的package.json最后,在
api
文件夹的package.json
中,将dev
脚本更改为使用--hot
选项。之前:
之后:
总结
1.我更新了
docker-compose.yml
中的卷路径。1.修改
api/package.json
中的dev
脚本以使用--hot
。有了这些变化,Hot Panderad就可以按预期工作了。😎
感谢Revolucion for Monica在SO的评论中给了我这个想法。