浏览代码

#237 further tweaks for https and timing issues with docker deployment

amueller 8 月之前
父节点
当前提交
e4bd6f5729
共有 2 个文件被更改,包括 14 次插入8 次删除
  1. 2 1
      internal/assets/templates/document.html
  2. 12 7
      internal/glance/main.go

+ 2 - 1
internal/assets/templates/document.html

@@ -25,7 +25,8 @@
         function createWebSocket() {
         function createWebSocket() {
             let host = window.location.hostname;
             let host = window.location.hostname;
             let port = window.location.protocol === "https:" ? (window.location.port || 443) : (window.location.port || 80);
             let port = window.location.protocol === "https:" ? (window.location.port || 443) : (window.location.port || 80);
-            let ws = new WebSocket("ws://" + host + ":" + port + "/ws");
+            let protocol = window.location.protocol === "https:" ? "wss" : "ws";
+            let ws = new WebSocket(protocol + "://" + host + ":" + port + "/ws");
             console.log("WebSocket connection established");
             console.log("WebSocket connection established");
             ws.onmessage = function(event) {
             ws.onmessage = function(event) {
                 if (event.data === "reload") {
                 if (event.data === "reload") {

+ 12 - 7
internal/glance/main.go

@@ -3,6 +3,7 @@ package glance
 import (
 import (
 	"fmt"
 	"fmt"
 	"os"
 	"os"
+	"time"
 
 
 	"github.com/fsnotify/fsnotify"
 	"github.com/fsnotify/fsnotify"
 )
 )
@@ -21,7 +22,7 @@ func Main() int {
 	}
 	}
 
 
 	if options.Intent == CliIntentServe {
 	if options.Intent == CliIntentServe {
-		err := startWatcherAndApp(options.ConfigPath)
+		err := startWatcherAndApp(options.ConfigPath, false)
 		if err != nil {
 		if err != nil {
 			fmt.Println(err)
 			fmt.Println(err)
 			return 1
 			return 1
@@ -31,7 +32,7 @@ func Main() int {
 	return 0
 	return 0
 }
 }
 
 
-func startWatcherAndApp(configPath string) error {
+func startWatcherAndApp(configPath string, sendReload bool) error {
 	done = make(chan bool)
 	done = make(chan bool)
 	watcher, err := fsnotify.NewWatcher()
 	watcher, err := fsnotify.NewWatcher()
 	if err != nil {
 	if err != nil {
@@ -49,12 +50,12 @@ func startWatcherAndApp(configPath string) error {
 				if event.Op&fsnotify.Write == fsnotify.Write {
 				if event.Op&fsnotify.Write == fsnotify.Write {
 					fmt.Println("config file modified, restarting application...")
 					fmt.Println("config file modified, restarting application...")
 					if currentApp != nil {
 					if currentApp != nil {
-						wsBroadcast <- []byte("reload")
 						if err := currentApp.Stop(); err != nil {
 						if err := currentApp.Stop(); err != nil {
 							fmt.Printf("failed to shutdown application: %v\n", err)
 							fmt.Printf("failed to shutdown application: %v\n", err)
 						}
 						}
+						time.Sleep(1 * time.Second) // give it enough time to shutdown
 					}
 					}
-					startWatcherAndApp(configPath)
+					startWatcherAndApp(configPath, true)
 				}
 				}
 			case err, ok := <-watcher.Errors:
 			case err, ok := <-watcher.Errors:
 				if !ok {
 				if !ok {
@@ -70,14 +71,13 @@ func startWatcherAndApp(configPath string) error {
 		return fmt.Errorf("failed to watch config file: %v", err)
 		return fmt.Errorf("failed to watch config file: %v", err)
 	}
 	}
 
 
-	restartApplication(configPath)
+	restartApplication(configPath, sendReload)
 	<-done
 	<-done
 
 
 	return nil
 	return nil
 }
 }
 
 
-func restartApplication(configPath string) {
-
+func restartApplication(configPath string, sendReload bool) {
 	configFile, err := os.Open(configPath)
 	configFile, err := os.Open(configPath)
 	if err != nil {
 	if err != nil {
 		fmt.Printf("failed opening config file: %v\n", err)
 		fmt.Printf("failed opening config file: %v\n", err)
@@ -99,7 +99,12 @@ func restartApplication(configPath string) {
 
 
 	currentApp = app
 	currentApp = app
 
 
+	if sendReload {
+		wsBroadcast <- []byte("reload")
+	}
+
 	if err := app.Serve(); err != nil {
 	if err := app.Serve(); err != nil {
 		fmt.Printf("http server error: %v\n", err)
 		fmt.Printf("http server error: %v\n", err)
 	}
 	}
+
 }
 }