Skip to content

Commit

Permalink
glanceapp#237 further tweaks for https and timing issues with docker …
Browse files Browse the repository at this point in the history
…deployment
  • Loading branch information
arminus committed Oct 17, 2024
1 parent 46ad425 commit e4bd6f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion internal/assets/templates/document.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
function createWebSocket() {
let host = window.location.hostname;
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");
ws.onmessage = function(event) {
if (event.data === "reload") {
Expand Down
19 changes: 12 additions & 7 deletions internal/glance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package glance
import (
"fmt"
"os"
"time"

"github.com/fsnotify/fsnotify"
)
Expand All @@ -21,7 +22,7 @@ func Main() int {
}

if options.Intent == CliIntentServe {
err := startWatcherAndApp(options.ConfigPath)
err := startWatcherAndApp(options.ConfigPath, false)
if err != nil {
fmt.Println(err)
return 1
Expand All @@ -31,7 +32,7 @@ func Main() int {
return 0
}

func startWatcherAndApp(configPath string) error {
func startWatcherAndApp(configPath string, sendReload bool) error {
done = make(chan bool)
watcher, err := fsnotify.NewWatcher()
if err != nil {
Expand All @@ -49,12 +50,12 @@ func startWatcherAndApp(configPath string) error {
if event.Op&fsnotify.Write == fsnotify.Write {
fmt.Println("config file modified, restarting application...")
if currentApp != nil {
wsBroadcast <- []byte("reload")
if err := currentApp.Stop(); err != nil {
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:
if !ok {
Expand All @@ -70,14 +71,13 @@ func startWatcherAndApp(configPath string) error {
return fmt.Errorf("failed to watch config file: %v", err)
}

restartApplication(configPath)
restartApplication(configPath, sendReload)
<-done

return nil
}

func restartApplication(configPath string) {

func restartApplication(configPath string, sendReload bool) {
configFile, err := os.Open(configPath)
if err != nil {
fmt.Printf("failed opening config file: %v\n", err)
Expand All @@ -99,7 +99,12 @@ func restartApplication(configPath string) {

currentApp = app

if sendReload {
wsBroadcast <- []byte("reload")
}

if err := app.Serve(); err != nil {
fmt.Printf("http server error: %v\n", err)
}

}

0 comments on commit e4bd6f5

Please sign in to comment.