coder / coder/internal

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

Open
#872 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
3
Forks
0
PR merge metrics
No merged PRs in 30d

Description

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:

```go
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`:
```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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.