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 // InstallBridge installs the relevant bridge javascript library
func InstallBridge(projectDir string, projectOptions *ProjectOptions) error { func InstallBridge(projectDir string, projectOptions *ProjectOptions) error {
bridgeFileData := mewn.String("../runtime/assets/bridge.js") 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") bridgeFileTarget := filepath.Join(projectDir, projectOptions.FrontEnd.Dir, "node_modules", "@wailsapp", "runtime", "init.js")
err := fs.CreateFile(bridgeFileTarget, []byte(bridgeFileData)) err := fs.CreateFile(bridgeFileTarget, []byte(bridgeFileData))
return err return err
@@ -561,6 +563,10 @@ func ldFlags(po *ProjectOptions, buildMode string) string {
ldflags += "-X github.com/wailsapp/wails.BuildMode=" + buildMode 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 we wish to generate typescript
if po.typescriptDefsFilename != "" { if po.typescriptDefsFilename != "" {
cwd, err := os.Getwd() cwd, err := os.Getwd()

View File

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

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"strconv"
"github.com/leaanthony/spinner" "github.com/leaanthony/spinner"
"github.com/wailsapp/wails/cmd" "github.com/wailsapp/wails/cmd"
@@ -10,6 +11,7 @@ import (
func init() { func init() {
var forceRebuild = false var forceRebuild = false
var servePort = "34115"
var verbose = false var verbose = false
buildSpinner := spinner.NewSpinner() buildSpinner := spinner.NewSpinner()
buildSpinner.SetSpinSpeed(50) buildSpinner.SetSpinSpeed(50)
@@ -18,7 +20,8 @@ func init() {
initCmd := app.Command("serve", "Run your Wails project in bridge mode"). initCmd := app.Command("serve", "Run your Wails project in bridge mode").
LongDescription(commandDescription). LongDescription(commandDescription).
BoolFlag("verbose", "Verbose output", &verbose). 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 { initCmd.Action(func() error {
@@ -34,7 +37,6 @@ func init() {
// Project options // Project options
projectOptions := &cmd.ProjectOptions{} projectOptions := &cmd.ProjectOptions{}
projectOptions.Verbose = verbose
// Check we are in project directory // Check we are in project directory
// Check project.json loads correctly // Check project.json loads correctly
@@ -44,6 +46,16 @@ func init() {
return err 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 // Save project directory
projectDir := fs.Cwd() projectDir := fs.Cwd()

View File

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

View File

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

View File

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