mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 22:13:36 +08:00
[v2] Add -devserverurl flag
This commit is contained in:
parent
f6b83b0933
commit
7572b64bec
@ -27,6 +27,8 @@ import (
|
|||||||
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
"github.com/wailsapp/wails/v2/pkg/commands/build"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultDevServerURL = "http://localhost:34115"
|
||||||
|
|
||||||
func LogGreen(message string, args ...interface{}) {
|
func LogGreen(message string, args ...interface{}) {
|
||||||
text := fmt.Sprintf(message, args...)
|
text := fmt.Sprintf(message, args...)
|
||||||
println(colour.Green(text))
|
println(colour.Green(text))
|
||||||
@ -96,6 +98,9 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
debounceMS := 100
|
debounceMS := 100
|
||||||
command.IntFlag("debounce", "The amount of time to wait to trigger a reload on change", &debounceMS)
|
command.IntFlag("debounce", "The amount of time to wait to trigger a reload on change", &debounceMS)
|
||||||
|
|
||||||
|
devServerURL := defaultDevServerURL
|
||||||
|
command.StringFlag("devserverurl", "The url of the dev server to use", &devServerURL)
|
||||||
|
|
||||||
command.Action(func() error {
|
command.Action(func() error {
|
||||||
|
|
||||||
// Create logger
|
// Create logger
|
||||||
@ -132,6 +137,18 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if devServerURL == defaultDevServerURL && projectConfig.DevServerURL != defaultDevServerURL && projectConfig.DevServerURL != "" {
|
||||||
|
devServerURL = projectConfig.DevServerURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if devServerURL != projectConfig.DevServerURL {
|
||||||
|
projectConfig.DevServerURL = devServerURL
|
||||||
|
err := projectConfig.Save()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if wailsjsdir == "" && projectConfig.WailsJSDir != "" {
|
if wailsjsdir == "" && projectConfig.WailsJSDir != "" {
|
||||||
wailsjsdir = projectConfig.WailsJSDir
|
wailsjsdir = projectConfig.WailsJSDir
|
||||||
}
|
}
|
||||||
@ -198,7 +215,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
|
|
||||||
// Do initial build
|
// Do initial build
|
||||||
logger.Println("Building application for development...")
|
logger.Println("Building application for development...")
|
||||||
newProcess, appBinary, err := restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel)
|
newProcess, appBinary, err := restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel, devServerURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -232,6 +249,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
LogGreen("Watching (sub)/directory: %s", projectDir)
|
LogGreen("Watching (sub)/directory: %s", projectDir)
|
||||||
|
LogGreen("Using Dev Server URL: %s", devServerURL)
|
||||||
|
|
||||||
// Setup a watcher for non-node_modules directories
|
// Setup a watcher for non-node_modules directories
|
||||||
dirs.Each(func(dir string) {
|
dirs.Each(func(dir string) {
|
||||||
@ -325,7 +343,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
rebuild = false
|
rebuild = false
|
||||||
LogGreen("[Rebuild triggered] files updated")
|
LogGreen("[Rebuild triggered] files updated")
|
||||||
// Try and build the app
|
// Try and build the app
|
||||||
newBinaryProcess, _, err = restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel)
|
newBinaryProcess, _, err = restartApp(logger, buildOptions, debugBinaryProcess, loglevel, passthruArgs, assetDir, false, exitCodeChannel, devServerURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogRed("Error during build: %s", err.Error())
|
LogRed("Error during build: %s", err.Error())
|
||||||
continue
|
continue
|
||||||
@ -370,7 +388,7 @@ func AddSubcommand(app *clir.Cli, w io.Writer) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func restartApp(logger *clilogger.CLILogger, buildOptions *build.Options, debugBinaryProcess *process.Process, loglevel string, passthruArgs []string, assetDir string, firstRun bool, exitCodeChannel chan int) (*process.Process, string, error) {
|
func restartApp(logger *clilogger.CLILogger, buildOptions *build.Options, debugBinaryProcess *process.Process, loglevel string, passthruArgs []string, assetDir string, firstRun bool, exitCodeChannel chan int, devServerURL string) (*process.Process, string, error) {
|
||||||
|
|
||||||
appBinary, err := build.Build(buildOptions)
|
appBinary, err := build.Build(buildOptions)
|
||||||
println()
|
println()
|
||||||
@ -400,7 +418,9 @@ func restartApp(logger *clilogger.CLILogger, buildOptions *build.Options, debugB
|
|||||||
if assetDir != "" {
|
if assetDir != "" {
|
||||||
args.Add("-assetdir", assetDir)
|
args.Add("-assetdir", assetDir)
|
||||||
}
|
}
|
||||||
|
if devServerURL != "" {
|
||||||
|
args.Add("-devserverurl", devServerURL)
|
||||||
|
}
|
||||||
if len(passthruArgs) > 0 {
|
if len(passthruArgs) > 0 {
|
||||||
args.AddSlice(passthruArgs)
|
args.AddSlice(passthruArgs)
|
||||||
}
|
}
|
||||||
|
@ -61,11 +61,16 @@ func CreateApp(appoptions *options.App) (*App, error) {
|
|||||||
|
|
||||||
// Check for CLI Flags
|
// Check for CLI Flags
|
||||||
assetdir := flag.String("assetdir", "", "Directory to serve assets")
|
assetdir := flag.String("assetdir", "", "Directory to serve assets")
|
||||||
|
devServerURL := flag.String("devserverurl", "http://localhost:34115", "URL of development server")
|
||||||
loglevel := flag.String("loglevel", "debug", "Loglevel to use - Trace, Debug, Info, Warning, Error")
|
loglevel := flag.String("loglevel", "debug", "Loglevel to use - Trace, Debug, Info, Warning, Error")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
if devServerURL != nil && *devServerURL != "" {
|
||||||
|
ctx = context.WithValue(ctx, "devserverurl", *devServerURL)
|
||||||
|
}
|
||||||
if assetdir != nil && *assetdir != "" {
|
if assetdir != nil && *assetdir != "" {
|
||||||
ctx = context.WithValue(ctx, "assetdir", *assetdir)
|
ctx = context.WithValue(ctx, "assetdir", *assetdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
if loglevel != nil && *loglevel != "" {
|
if loglevel != nil && *loglevel != "" {
|
||||||
level, err := pkglogger.StringToLogLevel(*loglevel)
|
level, err := pkglogger.StringToLogLevel(*loglevel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -35,7 +35,8 @@ type Frontend struct {
|
|||||||
debug bool
|
debug bool
|
||||||
|
|
||||||
// Assets
|
// Assets
|
||||||
assets *assetserver.DesktopAssetServer
|
assets *assetserver.DesktopAssetServer
|
||||||
|
devServerURL string
|
||||||
|
|
||||||
// main window handle
|
// main window handle
|
||||||
mainWindow *Window
|
mainWindow *Window
|
||||||
@ -59,19 +60,32 @@ func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.
|
|||||||
maxWidth: appoptions.MaxWidth,
|
maxWidth: appoptions.MaxWidth,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bindingsJSON, err := appBindings.ToJSON()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_devServerURL := ctx.Value("devserverurl")
|
||||||
|
if _devServerURL != nil {
|
||||||
|
result.devServerURL = _devServerURL.(string)
|
||||||
|
if result.devServerURL == "" {
|
||||||
|
result.devServerURL = "http://localhost:34115"
|
||||||
|
}
|
||||||
|
if result.devServerURL != "http://localhost:34115" {
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if we have been given a directory to serve assets from.
|
// Check if we have been given a directory to serve assets from.
|
||||||
// If so, this means we are in dev mode and are serving assets off disk.
|
// If so, this means we are in dev mode and are serving assets off disk.
|
||||||
// We indicate this through the `servingFromDisk` flag to ensure requests
|
// We indicate this through the `servingFromDisk` flag to ensure requests
|
||||||
// aren't cached by WebView2 in dev mode
|
// aren't cached by WebView2 in dev mode
|
||||||
|
|
||||||
_assetdir := ctx.Value("assetdir")
|
_assetdir := ctx.Value("assetdir")
|
||||||
if _assetdir != nil {
|
if _assetdir != nil {
|
||||||
result.servingFromDisk = true
|
result.servingFromDisk = true
|
||||||
}
|
}
|
||||||
|
|
||||||
bindingsJSON, err := appBindings.ToJSON()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
assets, err := assetserver.NewDesktopAssetServer(ctx, appoptions.Assets, bindingsJSON)
|
assets, err := assetserver.NewDesktopAssetServer(ctx, appoptions.Assets, bindingsJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -306,7 +320,11 @@ func (f *Frontend) setupChromium() {
|
|||||||
f.WindowSetRGBA(f.frontendOptions.RGBA)
|
f.WindowSetRGBA(f.frontendOptions.RGBA)
|
||||||
|
|
||||||
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
|
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
|
||||||
chromium.Navigate("file://wails/")
|
if f.devServerURL == "http://localhost:34115" {
|
||||||
|
chromium.Navigate("file://wails/")
|
||||||
|
} else {
|
||||||
|
chromium.Navigate(f.devServerURL)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventNotify struct {
|
type EventNotify struct {
|
||||||
|
@ -93,47 +93,54 @@ func (d *DevWebServer) Run(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
|
||||||
_assetdir := ctx.Value("assetdir")
|
_devServerURL := ctx.Value("devserverurl")
|
||||||
if _assetdir == nil {
|
if _devServerURL == "http://localhost:34115" {
|
||||||
return fmt.Errorf("no assetdir provided")
|
// Setup internal dev server
|
||||||
|
_assetdir := ctx.Value("assetdir")
|
||||||
|
if _assetdir == nil {
|
||||||
|
return fmt.Errorf("no assetdir provided")
|
||||||
|
}
|
||||||
|
if _assetdir != nil {
|
||||||
|
assetdir := _assetdir.(string)
|
||||||
|
bindingsJSON, err := d.appBindings.ToJSON()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
d.assetServer, err = assetserver.NewBrowserAssetServer(assetdir, bindingsJSON, d.appoptions)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
absdir, err := filepath.Abs(assetdir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
d.LogDebug("Serving assets from: %s", absdir)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.server.Get("*", d.loadAsset)
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
go func(server *fiber.App, log *logger.Logger) {
|
||||||
|
err := server.Listen("localhost:34115")
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err.Error())
|
||||||
|
}
|
||||||
|
d.LogDebug("Shutdown completed")
|
||||||
|
}(d.server, d.logger)
|
||||||
|
|
||||||
|
d.LogDebug("Serving application at http://localhost:34115")
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
err := d.server.Shutdown()
|
||||||
|
if err != nil {
|
||||||
|
d.logger.Error(err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
if _assetdir != nil {
|
|
||||||
assetdir := _assetdir.(string)
|
|
||||||
bindingsJSON, err := d.appBindings.ToJSON()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
d.assetServer, err = assetserver.NewBrowserAssetServer(assetdir, bindingsJSON, d.appoptions)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
absdir, err := filepath.Abs(assetdir)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
d.LogDebug("Serving assets from: %s", absdir)
|
|
||||||
}
|
|
||||||
|
|
||||||
d.server.Get("*", d.loadAsset)
|
|
||||||
|
|
||||||
// Start server
|
|
||||||
go func(server *fiber.App, log *logger.Logger) {
|
|
||||||
err := server.Listen("localhost:34115")
|
|
||||||
if err != nil {
|
|
||||||
log.Error(err.Error())
|
|
||||||
}
|
|
||||||
d.LogDebug("Shutdown completed")
|
|
||||||
}(d.server, d.logger)
|
|
||||||
|
|
||||||
d.LogDebug("Serving application at http://localhost:34115")
|
|
||||||
|
|
||||||
// Launch desktop app
|
// Launch desktop app
|
||||||
err := d.desktopFrontend.Run(ctx)
|
err := d.desktopFrontend.Run(ctx)
|
||||||
d.LogDebug("Starting shutdown")
|
d.LogDebug("Starting shutdown")
|
||||||
err2 := d.server.Shutdown()
|
|
||||||
if err2 != nil {
|
|
||||||
d.logger.Error(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -47,8 +47,11 @@ type Project struct {
|
|||||||
// Fully qualified filename
|
// Fully qualified filename
|
||||||
filename string
|
filename string
|
||||||
|
|
||||||
// The debounce time for hot-reload of the built-in dev server
|
// The debounce time for hot-reload of the built-in dev server. Default 100
|
||||||
DebounceMS int `json:"debounceMS"`
|
DebounceMS int `json:"debounceMS"`
|
||||||
|
|
||||||
|
// The url to use to server assets. Default "https://localhost:34115"
|
||||||
|
DevServerURL string `json:"devserverurl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Project) Save() error {
|
func (p *Project) Save() error {
|
||||||
|
Loading…
Reference in New Issue
Block a user