-
Notifications
You must be signed in to change notification settings - Fork 124
APP-8173: Make local testing for viam apps easier #5036
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7a8dec7
APP-8173: Make local testing for viam apps easier
jr22 c09bfac
some cleanup and allow optional port arg to specify where server is
jr22 33ac6df
remove some ai comments
jr22 24b36e2
rename and get weird of some ai gen stuff
jr22 e0bbcc8
use printf
jr22 5927b19
update cookies
jr22 7b267e5
make work with path
jr22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,106 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// localAppTestingArgs contains the arguments for the local-app-testing command. | ||
type localAppTestingArgs struct { | ||
Port int `json:"port"` | ||
} | ||
|
||
// LocalAppTestingAction is the action for the local-app-testing command. | ||
func LocalAppTestingAction(ctx *cli.Context, args localAppTestingArgs) error { | ||
htmlPath, err := getHTMLFilePath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
server := setupHTTPServer(htmlPath, args.Port) | ||
serverURL := fmt.Sprintf("http://localhost:%d", args.Port) | ||
|
||
printf(ctx.App.Writer, "Starting server to locally test viam apps on %s", serverURL) | ||
printf(ctx.App.Writer, "Press Ctrl+C to stop the server") | ||
|
||
if err := startServerInBackground(server, ctx.App.Writer); err != nil { | ||
return fmt.Errorf("failed to start server: %w", err) | ||
} | ||
|
||
if err := openbrowser(serverURL); err != nil { | ||
printf(ctx.App.Writer, "Warning: Could not open browser: %v", err) | ||
} | ||
|
||
<-ctx.Context.Done() | ||
|
||
if err := server.Shutdown(context.Background()); err != nil { | ||
return fmt.Errorf("error shutting down server: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// getHTMLFilePath returns the absolute path to module_local_viam_apps_test.html. | ||
func getHTMLFilePath() (string, error) { | ||
_, currentFile, _, ok := runtime.Caller(0) | ||
if !ok { | ||
return "", errors.New("error getting current file path") | ||
} | ||
sourceDir := filepath.Dir(currentFile) | ||
|
||
htmlPath := filepath.Join(sourceDir, "module_local_viam_apps_test.html") | ||
absPath, err := filepath.Abs(htmlPath) | ||
if err != nil { | ||
return "", fmt.Errorf("error getting absolute path: %w", err) | ||
} | ||
|
||
if _, err := os.Stat(absPath); os.IsNotExist(err) { | ||
return "", fmt.Errorf("module_local_viam_apps_test.html not found at: %s", absPath) | ||
} | ||
|
||
return absPath, nil | ||
} | ||
|
||
// setupHTTPServer creates and configures an HTTP server with the given HTML file. | ||
func setupHTTPServer(htmlPath string, port int) *http.Server { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0") | ||
w.Header().Set("Pragma", "no-cache") | ||
w.Header().Set("Expires", "0") | ||
|
||
http.ServeFile(w, r, htmlPath) | ||
}) | ||
|
||
return &http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
ReadHeaderTimeout: time.Minute * 5, | ||
} | ||
} | ||
|
||
// startServerInBackground starts the HTTP server in a goroutine and returns any startup errors. | ||
func startServerInBackground(server *http.Server, writer io.Writer) error { | ||
errChan := make(chan error, 1) | ||
go func() { | ||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
printf(writer, "Error starting server: %v", err) | ||
errChan <- err | ||
} | ||
close(errChan) | ||
}() | ||
|
||
select { | ||
case err := <-errChan: | ||
return err | ||
case <-time.After(100 * time.Millisecond): | ||
return nil // Server started successfully | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] subcommand seems longer than necessary? e.g.
local-test