Compare commits

...

1 Commits

Author SHA1 Message Date
Lea Anthony
ce4fa06828 Initial support for a serve port flag 2020-05-19 07:26:16 -05:00
7 changed files with 58 additions and 35 deletions

File diff suppressed because one or more lines are too long

View File

@@ -510,6 +510,8 @@ func InstallRuntime(caller string, projectDir string, projectOptions *ProjectOpt
// InstallBridge installs the relevant bridge javascript library
func InstallBridge(projectDir string, projectOptions *ProjectOptions) error {
bridgeFileData := mewn.String("../runtime/assets/bridge.js")
// Inject the custom serve port
bridgeFileData = strings.ReplaceAll(bridgeFileData, "$SERVEPORT$", projectOptions.ServePort)
bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, "node_modules", "@wailsapp", "runtime", "init.js")
err := fs.CreateFile(bridgeFileTarget, []byte(bridgeFileData))
return err
@@ -561,6 +563,10 @@ func ldFlags(po *ProjectOptions, buildMode string) string {
ldflags += "-X github.com/wailsapp/wails.BuildMode=" + buildMode
// Inject port if we are using bridge mode
if buildMode == BuildModeBridge {
ldflags += " -X github.com/wailsapp/wails/lib/renderer/bridge.ServePort=" + po.ServePort
}
// If we wish to generate typescript
if po.typescriptDefsFilename != "" {
cwd, err := os.Getwd()

View File

@@ -161,6 +161,7 @@ type ProjectOptions struct {
CrossCompile bool
Platform string
Architecture string
ServePort string
}
// Defaults sets the default project template

View File

@@ -2,6 +2,7 @@ package main
import (
"fmt"
"strconv"
"github.com/leaanthony/spinner"
"github.com/wailsapp/wails/cmd"
@@ -10,6 +11,7 @@ import (
func init() {
var forceRebuild = false
var servePort = "34115"
var verbose = false
buildSpinner := spinner.NewSpinner()
buildSpinner.SetSpinSpeed(50)
@@ -18,7 +20,8 @@ func init() {
initCmd := app.Command("serve", "Run your Wails project in bridge mode").
LongDescription(commandDescription).
BoolFlag("verbose", "Verbose output", &verbose).
BoolFlag("f", "Force rebuild of application components", &forceRebuild)
BoolFlag("f", "Force rebuild of application components", &forceRebuild).
StringFlag("p", "Port to serve on", &servePort)
initCmd.Action(func() error {
@@ -34,7 +37,6 @@ func init() {
// Project options
projectOptions := &cmd.ProjectOptions{}
projectOptions.Verbose = verbose
// Check we are in project directory
// Check project.json loads correctly
@@ -44,6 +46,16 @@ func init() {
return err
}
// Set Verbose flag
projectOptions.Verbose = verbose
// Check port
port, err := strconv.Atoi(servePort)
if err != nil || port <= 0 {
return fmt.Errorf("invalid port value: %s", servePort)
}
projectOptions.ServePort = servePort
// Save project directory
projectDir := fs.Cwd()

View File

@@ -1,4 +1,4 @@
package renderer
package bridge
import (
"encoding/json"
@@ -14,6 +14,10 @@ import (
type messageType int
// ServePort indicates which port to serve the backend on when using
// `wails serve`
var ServePort = "34115"
const (
jsMessage messageType = iota
cssMessage
@@ -93,7 +97,7 @@ func (h *Bridge) startSession(conn *websocket.Conn) {
// Run the app in Bridge mode!
func (h *Bridge) Run() error {
h.server = &http.Server{Addr: ":34115"}
h.server = &http.Server{Addr: ":" + ServePort}
http.HandleFunc("/bridge", h.wsBridgeHandler)
h.log.Info("Bridge mode started.")

View File

@@ -1,4 +1,4 @@
package renderer
package bridge
import (
"time"

View File

@@ -14,7 +14,7 @@ function init() {
window.wailsbridge = {
reconnectOverlay: null,
reconnectTimer: 300,
wsURL: 'ws://' + window.location.hostname + ':34115/bridge',
wsURL: 'ws://' + window.location.hostname + ':$SERVEPORT$/bridge',
connectionState: null,
config: {},
websocket: null,
@@ -149,38 +149,38 @@ function startBridge() {
function handleMessage(message) {
// As a bridge we ignore js and css injections
switch (message.data[0]) {
// Wails library - inject!
case 'w':
addScript(message.data.slice(1));
// Wails library - inject!
case 'w':
addScript(message.data.slice(1));
// Now wails runtime is loaded, wails for the ready event
// and callback to the main app
window.wails.Events.On('wails:loaded', function () {
window.wailsbridge.log('Wails Ready');
if (window.wailsbridge.callback) {
window.wailsbridge.log('Notifying application');
window.wailsbridge.callback(window.wails);
}
});
window.wailsbridge.log('Loaded Wails Runtime');
break;
// Now wails runtime is loaded, wails for the ready event
// and callback to the main app
window.wails.Events.On('wails:loaded', function () {
window.wailsbridge.log('Wails Ready');
if (window.wailsbridge.callback) {
window.wailsbridge.log('Notifying application');
window.wailsbridge.callback(window.wails);
}
});
window.wailsbridge.log('Loaded Wails Runtime');
break;
// Notifications
case 'n':
addScript(message.data.slice(1), true);
break;
case 'n':
addScript(message.data.slice(1), true);
break;
// Binding
case 'b':
var binding = message.data.slice(1);
//log("Binding: " + binding)
window.wails._.NewBinding(binding);
break;
case 'b':
var binding = message.data.slice(1);
//log("Binding: " + binding)
window.wails._.NewBinding(binding);
break;
// Call back
case 'c':
var callbackData = message.data.slice(1);
window.wails._.Callback(callbackData);
break;
default:
window.wails.Log.Error('Unknown message type received: ' + message.data[0]);
case 'c':
var callbackData = message.data.slice(1);
window.wails._.Callback(callbackData);
break;
default:
window.wails.Log.Error('Unknown message type received: ' + message.data[0]);
}
}