wetware / pkg

Peer-to-peer cloud environment
https://wetware.run
Other
38 stars 7 forks source link

Remove Fx from server implementation #138

Closed lthibault closed 1 year ago

lthibault commented 1 year ago

Embedding a Wetware server in a third-party application is currently tedious due to extensive boilerplate. This boilerplate comes from three sources:

  1. pkg/server uses Fx for dependency-injection but nonetheless requires us to instantiate, configure, start and stop the Fx application itself.
  2. server.Node comprises multiple sub-services whose startup/shutdown logic logic needs to be carefully orchestrated.
  3. Early iterations of Wetware focused heavily on configurability, so the public API for pkg/server exposes a lot of internals.

The goal of this PR is to provide a dead-simple mechanism for embedding servers. It adds a top level server/ package, within which all three sources of complexity are remedied as follows:

  1. No use of Fx anywhere
  2. Use of blocking Serve / ListenAndServe methods, similar to net/http
  3. Reduced configuration options

A wetware server is now embedded as follows:

// config file is designed to take simple types, which makes it
// easier to populate from CLI parameters.
config := server.Config{
    Logger:   log.New(),
    NS:       "my-namespace",
    Join:  <optional array of bootstrap peers>
    Discover: <optional multiaddr to discovery service>,
    Meta:     <optional map of key/value pairs>,
}

// blocks until the context expires; returns context's error
err := config.ListenAndServe(ctx, c.StringSlice("listen")...)

Contrary to the previous version, server capabilities are not directly accessible since there is no equivalent of the server.Node type. Applications requiring access to the server's capability will have to dial a client connection to the local server. Future work will facilitate this.