Skip to content

Refactor coderd to split endpoints (or groups of endpoints) into their own types #872

@deansheather

Description

@deansheather

Most tests, like simple tests for creating a user or something, only have a few dependencies like authz and the db.

To make testing with mocks easier, it would be nice if we didn't have a gigantic type (e.g. *coderd.API) with a million dependencies. We're pretty much forced to use coderdtest with a real database for any tests in the coderd package because the amount of dependencies for an entire *coderd.API instance is mindbogglingly large.

We should split endpoint groups into their own type so they're much more manageable and only have access to the dependencies listed in the type:

type UserRouter struct{
  Database database.Store
  // ...
}

func (r *UserRouter) DeleteUser(rw http.ResponseWriter, r *http.Request) {}

This matches the approach we've taken in the agentapi package, which has been quite effective.

The only major downside is that it's gets very repetitive and boilerplatey to instantiate these types from coderd.go:

func NewAPI(options Options) *API {
  // ...
  api := &API{
    userRouter: &UserRouter{
      Database: options.Database,
    },
    workpsaceRouter: &WorkspaceRouter{
      Database: options.Database,
    },
  }
}

I think this issue could potentially be rectified using generation. The population of all the routers onto the *coderdAPI type could be generated code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions