Reputation: 65
i try to bring my preact app to production.
It has a vite.config.js with a proxy configured, where i am routing to the backend.
This proxy is used when running npm run dev
http://localhost:3000/api/abc -> http://localhost:8000/abc
import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';
import os from 'os';
function getLocalIpAddress() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return "http://" + iface.address + ":8000";
}
}
}
return 'http://127.0.0.1:8000';
}
function getLocalIpAddressWs() {
const interfaces = os.networkInterfaces();
for (const name of Object.keys(interfaces)) {
for (const iface of interfaces[name]) {
if (iface.family === 'IPv4' && !iface.internal) {
return "ws://" + iface.address + ":8000";
}
}
}
return 'ws://127.0.0.1:8000';
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [preact()],
build: {
outDir: 'dist',
},
server: {
port: 3000,
host: '0.0.0.0',
proxy: {
'/api': {
target: getLocalIpAddress(), // Replace with your backend server URL
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
'/socket': {
target: getLocalIpAddressWs(), // Replace with your backend server URL
changeOrigin: true,
rewrite: (path) => path.replace(/^\/socket/, ''),
},
},
},
});
I am now using warp to serve my static files, which works fine. But i am just not able to create the proxy for it.
I am trying to spawn by base server and afterwards create the proxy server like in this example, but it just wont compile
use local_ip_address::local_ip;
use std::{error::Error, fs};
use tokio::sync::watch;
use warp::Filter;
use warp::{http::Response, hyper::Body, Rejection, Reply};
use warp_reverse_proxy::reverse_proxy_filter;
async fn log_response(response: Response<Body>) -> Result<impl Reply, Rejection> {
println!("{:?}", response);
Ok(response)
}
pub async fn server_start(mut shutdown_rx: watch::Receiver<()>) -> Result<(), Box<dyn Error>> {
// Print the current PC's IP address
match local_ip() {
Ok(ip) => println!("Current PC IP address: {}", ip),
Err(e) => eprintln!("Failed to get local IP address: {}", e),
}
// Serve static files from the current directory
let mut folder_path = std::env::current_dir()?.join("dist");
println!("Dist dir: {:?}", folder_path);
if !fs::metadata(folder_path.clone()).is_ok() {
folder_path = std::env::current_dir()?.join("preact-app").join("dist");
if !fs::metadata(folder_path.clone()).is_ok() {
println!("Folder not found");
return Ok(());
}
}
let route = warp::fs::dir(folder_path.clone());
let port = 3000;
let (addr, server) =
warp::serve(route).bind_with_graceful_shutdown(([0, 0, 0, 0], port), async move {
shutdown_rx.changed().await.ok();
});
// spawn base server
tokio::spawn(server);
// Forward request to localhost in other port
let app = warp::path!("hello" / ..).and(
reverse_proxy_filter("".to_string(), "http://127.0.0.1:8000/".to_string())
.and_then(log_response),
);
// spawn proxy server
warp::serve(app).run(([0, 0, 0, 0], 3000)).await;
Ok(())
}
For example the "and_then" is not existing. So i am not even able to test it.
let app = warp::path!("hello" / ..).and(
reverse_proxy_filter("".to_string(), "http://127.0.0.1:8000/".to_string())
.and_then(log_response),
);
Upvotes: 0
Views: 43