Skip to content

Commit

Permalink
feat: server started message (#211)
Browse files Browse the repository at this point in the history
Show the user how to connect to the server remotely when the server is started.

Signed-off-by: Toma Puljak <[email protected]>
  • Loading branch information
Tpuljak authored Mar 14, 2024
1 parent e4be3bc commit c52f5a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
23 changes: 19 additions & 4 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
package server

import (
"fmt"
"time"

"github.com/daytonaio/daytona/pkg/server"
"github.com/daytonaio/daytona/pkg/server/config"
"github.com/daytonaio/daytona/pkg/server/frpc"
"github.com/daytonaio/daytona/pkg/views/util"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -20,17 +25,27 @@ var ServerCmd = &cobra.Command{
// for now, force the log level to info when running the server
log.SetLevel(log.InfoLevel)
}
errCh := make(chan error)

err := server.Start(errCh)
if err != nil {
log.Fatal(err)
}

config, err := config.GetConfig()
c, err := config.GetConfig()
if err != nil {
log.Fatal(err)
}

if config == nil {
log.Fatal("Server configuration is not set. Please run `daytona configure`.")
select {
case err := <-errCh:
log.Fatal(err)
// TODO: This is an optimistic check. We should check if the server is actually running
case <-time.After(5 * time.Second):
util.RenderBorderedMessage(fmt.Sprintf("Daytona Server running on port: %d.\nTo connect to the server remotely, use the following command on the client machine:\n\ndaytona profile add -a %s", c.ApiPort, frpc.GetApiUrl(c)))
}

err = server.Start()
err = <-errCh
if err != nil {
log.Fatal(err)
}
Expand Down
21 changes: 14 additions & 7 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Self struct {
DNSName string `json:"DNSName"`
}

func Start() error {
func Start(errCh chan error) error {
err := logs.Init()
if err != nil {
return err
Expand Down Expand Up @@ -66,13 +66,13 @@ func Start() error {

go func() {
if err := frpc.ConnectServer(); err != nil {
log.Fatal(err)
errCh <- err
}
}()

go func() {
if err := frpc.ConnectApi(); err != nil {
log.Fatal(err)
errCh <- err
}
}()

Expand All @@ -95,18 +95,25 @@ func Start() error {

select {
case err := <-errChan:
log.Fatal(err)
errCh <- err
case <-time.After(1 * time.Second):
go func() {
errChan <- headscale.Connect()
}()
}

if err := <-errChan; err != nil {
log.Fatal(err)
errCh <- err
}
}()

log.Infof("Starting api server on port %d", c.ApiPort)
return apiServer.Serve(apiListener)
go func() {
log.Infof("Starting api server on port %d", c.ApiPort)
err := apiServer.Serve(apiListener)
if err != nil {
errCh <- err
}
}()

return nil
}
11 changes: 11 additions & 0 deletions pkg/views/util/globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ func RenderListLine(message string) {
func RenderInfoMessageBold(message string) {
fmt.Println(lipgloss.NewStyle().Bold(true).Padding(1, 0, 1, 1).Render(message))
}

func RenderBorderedMessage(message string) {
fmt.Println(lipgloss.
NewStyle().
Margin(1, 0).
Padding(1, 1, 1, 1).
BorderForeground(views.Green).
Border(lipgloss.RoundedBorder()).
Render(message),
)
}

0 comments on commit c52f5a9

Please sign in to comment.