Skip to content

chore: Refactor common server cli components for reusability #7103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Apr 12, 2023

Workspace-proxy cmd needs to redo all of this, better to be shared.

See the workspace-proxy cmd (WIP):

Handler: func(inv *clibase.Invocation) error {
scd, err := cli.SetupServerCmd(inv, cfg)
if err != nil {
return err
}
defer scd.Close()
ctx := scd.Ctx
pu, _ := url.Parse("http://localhost:3000")
proxy, err := wsproxy.New(&wsproxy.Options{
Logger: scd.Logger,
// TODO: PrimaryAccessURL
PrimaryAccessURL: pu,
AccessURL: cfg.AccessURL.Value(),
AppHostname: scd.AppHostname,
AppHostnameRegex: scd.AppHostnameRegex,
RealIPConfig: scd.RealIPConfig,
// TODO: AppSecurityKey
AppSecurityKey: workspaceapps.SecurityKey{},
Tracing: scd.Tracer,
PrometheusRegistry: scd.PrometheusRegistry,
APIRateLimit: int(cfg.RateLimit.API.Value()),
SecureAuthCookie: cfg.SecureAuthCookie.Value(),
// TODO: DisablePathApps
DisablePathApps: false,
// TODO: ProxySessionToken
ProxySessionToken: "",
})
if err != nil {
return xerrors.Errorf("create workspace proxy: %w", err)
}
shutdownConnsCtx, shutdownConns := context.WithCancel(ctx)
defer shutdownConns()

Workspace-proxy cmd needs to redo all of this, better to be shared
@Emyrk Emyrk requested a review from kylecarbs April 12, 2023 15:13
@Emyrk Emyrk requested a review from deansheather April 12, 2023 19:07

// SetupServerCmd sets up the common elements of starting a server daemon.
// This is used by both coderd and workspace proxies.
func SetupServerCmd(inv *clibase.Invocation, cfg *codersdk.DeploymentValues) (_ *CommonServerCmd, err error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too broad of a function name. Is there a reason this needs to be abstracted together and can't just be duplicated? I'm having a difficult time understanding why I'd put something here, and whether it would break workspace proxies or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can duplicate it, and maybe that's the way to go. I was just trying not to duplicate all this logic across the Coderd and workspace proxy.

I'm just afraid of regressions when things are not properly done in both places.


My other idea was to refactor this into as many functions as I can so at least the shared code is a bit less LOC

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't share the shared code we will start seeing devs do something in server.go and not do it in proxy.go just like we did in V1. Code duplication is off the table IMO because of this.

Just using functions and not a big "CommonServerCmd" struct would also work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can whip up the making common functions option. It's not as nice since I have to export a lot of things from this cli package, and new things still have be copied into 2 locations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kylecarbs I need this PR where I reuse the code, or I need this PR #7118.

The second PR just exports all the functions that I need to make the workspace proxy command act the same.

@Emyrk Emyrk closed this Apr 13, 2023
@Emyrk
Copy link
Member Author

Emyrk commented Apr 13, 2023

closed for #7118

@Emyrk Emyrk deleted the stevenmasley/server_cli_shared branch April 17, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants