Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/sst/mosaic.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func CmdMosaic(c *cli.Cli) error {

if mode == "basic" {
wg.Go(func() error {
<-server.Ready
return CmdUI(c)
})
}
Expand Down
28 changes: 21 additions & 7 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
)

type Server struct {
Port int
Mux *http.ServeMux
Rpc *rpc.Server
Port int
Mux *http.ServeMux
Rpc *rpc.Server
Ready chan struct{}
}

func New() (*Server, error) {
Expand All @@ -35,9 +36,10 @@ func New() (*Server, error) {
return nil, err
}
result := &Server{
Port: port,
Mux: http.NewServeMux(),
Rpc: rpc.NewServer(),
Port: port,
Mux: http.NewServeMux(),
Rpc: rpc.NewServer(),
Ready: make(chan struct{}),
}
result.Mux.HandleFunc("/rpc", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
Expand Down Expand Up @@ -69,7 +71,19 @@ func (s *Server) Start(ctx context.Context, p *project.Project) error {
u, _ := url.Parse("http://" + server.Addr)
os.WriteFile(serverPath, []byte(u.String()), 0644)
defer os.Remove(serverPath)
go server.ListenAndServe()

listener, err := net.Listen("tcp", server.Addr)
if err != nil {
close(s.Ready)
return fmt.Errorf("failed to listen on %s: %w", server.Addr, err)
}

go func() {
server.Serve(listener)
}()

close(s.Ready)
log.Info("server ready to accept connections")

keyPath := filepath.Join(global.CertPath(), "key.pem")
certPath := filepath.Join(global.CertPath(), "cert.pem")
Expand Down