-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
378 additions
and
275 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cardinal | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/rotisserie/eris" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"pkg.world.dev/world-cli/common/teacmd" | ||
) | ||
|
||
const ceReadTimeout = 5 * time.Second | ||
|
||
// startCardinalEditor runs the Cardinal Editor | ||
func startCardinalEditor(ctx context.Context, rootDir string, gameDir string, port int) error { | ||
if err := teacmd.SetupCardinalEditor(rootDir, gameDir); err != nil { | ||
return err | ||
} | ||
|
||
// Create a new HTTP server | ||
fs := http.FileServer(http.Dir(filepath.Join(rootDir, teacmd.TargetEditorDir))) | ||
http.Handle("/", fs) | ||
server := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
ReadTimeout: ceReadTimeout, | ||
} | ||
|
||
group, ctx := errgroup.WithContext(ctx) | ||
group.Go(func() error { | ||
if err := server.ListenAndServe(); err != nil && !eris.Is(err, http.ErrServerClosed) { | ||
return eris.Wrap(err, "Cardinal Editor server encountered an error") | ||
} | ||
return nil | ||
}) | ||
group.Go(func() error { | ||
<-ctx.Done() | ||
if err := server.Shutdown(ctx); err != nil { | ||
return eris.Wrap(err, "Failed to gracefully shutdown server") | ||
} | ||
return nil | ||
}) | ||
|
||
if err := group.Wait(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package common | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/rotisserie/eris" | ||
) | ||
|
||
// WithEnv sets the environment variables from the given map. | ||
func WithEnv(env map[string]string) error { | ||
for key, value := range env { | ||
if err := os.Setenv(key, value); err != nil { | ||
return eris.Wrap(err, "Failed to set env var") | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
// FindUnusedPort finds an unused port in the range [start, end] for Cardinal Editor | ||
func FindUnusedPort(start int, end int) (int, error) { | ||
for port := start; port <= end; port++ { | ||
address := fmt.Sprintf(":%d", port) | ||
listener, err := net.Listen("tcp", address) | ||
if err == nil { | ||
if err := listener.Close(); err != nil { | ||
return 0, err | ||
} | ||
return port, nil | ||
} | ||
} | ||
return 0, fmt.Errorf("no available port in the range %d-%d", start, end) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters