composition exercise: http file server with index page

In which I put together some finger exercises and variations on themes to get an index page for a directory that is being served over http.

I have the two pieces but I will need to combine them somehow. I’m planning on using ports as the message passing mechanism (I don’t really have a choice honestly since isolates can only communicate over ports anyway) so I will need to package up the file registry piece in a way that is more self-contained instead of being spread out over several top-level methods

I will definitely need the accumulator for the registry, the port over which accumulator requests will be received, and the directory which is being watched

The constructor will obviously need to do some work to set everything up and connect the proper listener code that was previously being handled as a top level method

The important parts are the listeners for keeping the accumulator in sync with the file system by watching for the events and for sending responses. The expectation is that whoever sends us a message also passes along the port on which they will be expecting a response. The rest of the class is a transcription of the top-level method versions of the file registry

The main method is pretty similar to how it was before except now we initialize a watcher and pass in the port from the watcher as the initial argument (this kinda reminds me of dependency injection)

I actually don’t know what will happen if something bad happens in the watcher instance. I’m assuming the main isolate will crash but I don’t know if it will bring down the entire process tree or if the server isolate will continue chugging along. This is easy enough to verify though by throwing an exception inside the listeners and then observing what happens so I’m gonna leave that as an exercise for the reader.

This just leaves the code for starting the server and serving requests

Putting it all together I get a file server that serves an “index pages” using the watcher class and the server communicates with the watcher with ports. I’ll need to go back and properly format the accumulator as an index page but that shouldn’t be hard.

Initially I was trying to set up some kind of bi-directional communication channel but that turned out to be a dead end. The proper pattern seems to be to set up a dependency graph for the isolates and then pass down the dependencies as the initial argument to the isolate’s starter method. This way if a given isolate needs something it can send a message and send the port the response should come back on. The isolates that receive messages can then respond properly by extracting the port and sending the response.

There is very little error handling happening here and it is not clear to me how to design robust isolate hierarchies. The way I’ve written things if there is any error in the main isolate then the whole thing crashes. It’s fine for this but I can imagine more complicated hierarchies of isolates and in those instances crashing the entire process is probably not the right thing to do. It is also not clear to me how to build up on top of this pattern. Isolating things is fine and well but I already see how painful it will be to build more complicated isolate hierarchies that need to communicate.