Skip to content

Commit bf33b64

Browse files
committed
Separate the server with embed
1 parent 627fbe5 commit bf33b64

File tree

6 files changed

+1662
-1583
lines changed

6 files changed

+1662
-1583
lines changed

cli/root.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
"golang.org/x/xerrors"
2020

21+
"cdr.dev/slog"
22+
2123
"github.com/charmbracelet/lipgloss"
2224
"github.com/kirsle/configdir"
2325
"github.com/mattn/go-isatty"
@@ -28,7 +30,6 @@ import (
2830
"github.com/coder/coder/cli/cliui"
2931
"github.com/coder/coder/cli/config"
3032
"github.com/coder/coder/cli/deployment"
31-
"github.com/coder/coder/coderd"
3233
"github.com/coder/coder/coderd/gitauth"
3334
"github.com/coder/coder/codersdk"
3435
)
@@ -105,11 +106,10 @@ func Core() []*cobra.Command {
105106
}
106107
}
107108

109+
type ServerFunc func(ctx context.Context, options interface{}) (interface{}, io.Closer, error)
110+
108111
func AGPL() []*cobra.Command {
109-
all := append(Core(), Server(deployment.NewViper(), func(_ context.Context, o *coderd.Options) (*coderd.API, io.Closer, error) {
110-
api := coderd.New(o)
111-
return api, api, nil
112-
}))
112+
all := append(Core(), Server(deployment.NewViper(), serverFunc()))
113113
return all
114114
}
115115

@@ -724,3 +724,26 @@ func dumpHandler(ctx context.Context) {
724724
}
725725
}
726726
}
727+
728+
func serveHandler(ctx context.Context, logger slog.Logger, handler http.Handler, addr, name string) (closeFunc func()) {
729+
logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name))
730+
731+
// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
732+
// websockets over the dev tunnel.
733+
// See: https://github.com/coder/coder/pull/3730
734+
//nolint:gosec
735+
srv := &http.Server{
736+
Addr: addr,
737+
Handler: handler,
738+
}
739+
go func() {
740+
err := srv.ListenAndServe()
741+
if err != nil && !xerrors.Is(err, http.ErrServerClosed) {
742+
logger.Error(ctx, "http server listen", slog.F("name", name), slog.Error(err))
743+
}
744+
}()
745+
746+
return func() {
747+
_ = srv.Close()
748+
}
749+
}

0 commit comments

Comments
 (0)