Search code examples
rustactix-web

Binding actix to unix socket + running on multiple subdomains


I've run into following problem. I want to set up actix on unix socket, that's my preferred way, but I can change that if really needed.

Let's say /etc/server.sock

My nginx server redirects traffic from few subdomains into this socket Now, I want to set up few domains preferably on same app to avoid mess.

I've tried setting up tokio::net::UnixListener, and passing it into listen method. I've also tried passing everything into one server but then I realized that I can't do that due to fact that routes would collide in few places.


Solution

  • With help of friend I managed to find a solution, I have no idea if it's a good way to do it but:

    1. I've split my files into different routes

    Api folder (module)
     \---files for all subdomains
    main.rs
    

    2. In main I've set up services for every subdomain

    async fn main() -> io::Result<()> {
        let server = HttpServer::new(|| {
            App::new()
                .service(api::a_handler())
                .service(api::b_handler())
                .service(api::c_handler())
                .service(api::d_handler())
        });
    
        server.bind("127.0.0.1:8000")? //I've also switched from unix sockets
            .run()
            .await
    }
    

    3. Then in every api file I've set up handlers and guards respectively

    pub fn a_handler() -> Scope {
        web::scope("").guard(guard::Header("host", "a.example.com"))
    }