5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 23:23:48 +08:00

Move startURL calculation to assetserver

This commit is contained in:
Lea Anthony 2024-01-20 11:50:20 +11:00
parent bcb390f611
commit c82facd6ff
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
3 changed files with 41 additions and 30 deletions

View File

@ -6,6 +6,8 @@ import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"path"
"strings"
"time"
)
@ -165,3 +167,36 @@ func (a *AssetServer) AddPluginScript(pluginName string, script string) {
pluginScriptName := fmt.Sprintf("/wails/plugin/%s.js", pluginName)
a.pluginScripts[pluginScriptName] = script
}
func GetStartURL(userURL string) (string, error) {
devServerURL := GetDevServerURL()
if devServerURL != "" {
// Parse the port
parsedURL, err := url.Parse(devServerURL)
if err != nil {
return "", fmt.Errorf("Error parsing environment variable 'FRONTEND_DEVSERVER_URL`: " + err.Error() + ". Please check your `Taskfile.yml` file")
}
port := parsedURL.Port()
if port != "" {
startURL += ":" + port
}
} else {
if userURL != "" {
// parse the url
parsedURL, err := url.Parse(userURL)
if err != nil {
return "", fmt.Errorf("Error parsing URL: " + err.Error())
}
if parsedURL.Scheme == "" {
startURL = path.Join(startURL, userURL)
// if the original URL had a trailing slash, add it back
if strings.HasSuffix(userURL, "/") {
startURL = startURL + "/"
}
} else {
startURL = userURL
}
}
}
return startURL, nil
}

View File

@ -0,0 +1,3 @@
package assetserver
var startURL = "http://wails.localhost"

View File

@ -7,7 +7,6 @@ import (
"fmt"
"github.com/wailsapp/wails/v3/internal/assetserver"
"net/url"
"path"
"strconv"
"strings"
"sync"
@ -1457,35 +1456,9 @@ func (w *windowsWebviewWindow) setupChromium() {
chromium.Init(script)
chromium.NavigateToString(w.parent.options.HTML)
} else {
var startURL = "http://wails.localhost"
devServerURL := assetserver.GetDevServerURL()
if devServerURL != "" {
// Parse the port
parsedURL, err := url.Parse(devServerURL)
startURL, err := assetserver.GetStartURL(w.parent.options.URL)
if err != nil {
globalApplication.fatal("Error parsing environment variable 'FRONTEND_DEVSERVER_URL`: " + err.Error() + ". Please check your `Taskfile.yml` file")
}
port := parsedURL.Port()
if port != "" {
startURL += ":" + port
}
} else {
if w.parent.options.URL != "" {
// parse the url
parsedURL, err := url.Parse(w.parent.options.URL)
if err != nil {
globalApplication.fatal("Error parsing URL: " + err.Error())
}
if parsedURL.Scheme == "" {
startURL = path.Join(startURL, w.parent.options.URL)
// if the original URL had a trailing slash, add it back
if strings.HasSuffix(w.parent.options.URL, "/") {
startURL = startURL + "/"
}
} else {
startURL = w.parent.options.URL
}
}
globalApplication.fatal(err.Error())
}
chromium.Navigate(startURL)
}