examples icon indicating copy to clipboard operation
examples copied to clipboard

Fix chat-tcp panic when current directory is not the expected directory

Open JonathanLindsey opened this issue 1 year ago • 2 comments

Prior to this change if you followed the websockets/chat-tcp README without first making your current directory websockets/chat-tcp, you would get a panic when you opened http://localhost:8080/ .

This uses include_str!() to include the index.html file at compile time referencing it relative to the source code file and avoids the use of the problematic unwrap() all together.

JonathanLindsey avatar Aug 21 '24 15:08 JonathanLindsey

Usage of NamedFile here is preferred. We should update the instructions to include a cd command.

robjtede avatar Aug 21 '24 20:08 robjtede

Ok will stick with NamedFile. But the problem extends to a deployed executable. If you build the executable and move the target/release folder to another location that does not have the source code it won't have index.html. Even if you had a copy of index.html, the current directory you launch the executable from matters...which I think shouldn't matter at least in the deployed state. If you accidentally started the executable from a different directory, you could expose a different index.html file that you didn't mean to besides the application being broken.

Would it make since to copy index.html to the target/debug/static or target/release/static folder at build time using a build.rs file? Then load index.html at runtime with a path relative to the executable file rather than the current directory.

JonathanLindsey avatar Aug 22 '24 17:08 JonathanLindsey

My above idea led me down a few rabbit holes so just adding the instruction to cd to the right directory.

JonathanLindsey avatar Aug 29 '24 04:08 JonathanLindsey