-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added static file monitoring and auto-reload.
Any request that comes in, reload! Signed-off-by: Dave Shanley <[email protected]>
- Loading branch information
1 parent
7cd43cf
commit a223746
Showing
9 changed files
with
212 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley | ||
// SPDX-License-Identifier: MIT | ||
|
||
package daemon | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/google/uuid" | ||
"github.com/pb33f/ranch/bus" | ||
"github.com/pb33f/ranch/model" | ||
"github.com/pb33f/wiretap/shared" | ||
"github.com/pterm/pterm" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func MonitorStatic(wiretapConfig *shared.WiretapConfiguration) { | ||
|
||
b := bus.GetBus() | ||
staticChan, _ := b.GetChannelManager().GetChannel(WiretapStaticChangeChan) | ||
|
||
go func() { | ||
var err error | ||
|
||
watcher, _ := fsnotify.NewWatcher() | ||
defer watcher.Close() | ||
|
||
watchDir := func(path string, fi os.FileInfo, err error) error { | ||
if fi.Mode().IsDir() { | ||
return watcher.Add(path) | ||
} | ||
return nil | ||
} | ||
|
||
if wErr := filepath.Walk(wiretapConfig.StaticDir, watchDir); err != nil { | ||
pterm.Fatal.Println(fmt.Sprintf("Error trying to monitor static directory: %s", wErr)) | ||
} | ||
|
||
for { | ||
select { | ||
case event := <-watcher.Events: | ||
if event.Has(fsnotify.Write) { | ||
pterm.Info.Println(pterm.LightMagenta(fmt.Sprintf("[wiretap] static file changed: %s", event.Name))) | ||
|
||
// broadcast the change to all connected clients | ||
ch := make(map[string]string) | ||
ch["file"] = event.Name | ||
|
||
id, _ := uuid.NewUUID() | ||
staticChan.Send(&model.Message{ | ||
Id: &id, | ||
DestinationId: &id, | ||
Error: err, | ||
Channel: WiretapStaticChangeChan, | ||
Destination: WiretapStaticChangeChan, | ||
Payload: ch, | ||
Direction: model.ResponseDir, | ||
}) | ||
|
||
} | ||
case wErr := <-watcher.Errors: | ||
pterm.Error.Println(fmt.Sprintf("[wiretap] static error: %s", wErr.Error())) | ||
} | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{{ .OriginalContent }} | ||
<!-- auto-injected into the page by wiretap --> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"@stomp/stompjs": "https://ga.jspm.io/npm:@stomp/[email protected]/esm6/index.js" | ||
} | ||
} | ||
</script> | ||
<script type="module"> | ||
import { Client } from '@stomp/stompjs'; | ||
const client = new Client({ | ||
brokerURL: 'ws://localhost:{{ .WebSocketPort }}/ranch', | ||
onConnect: () => { | ||
client.subscribe("/topic/wiretap-static-change", message => { | ||
window.location.reload(); | ||
}); | ||
}, | ||
}); | ||
client.activate(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.