-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
552 additions
and
346 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/pietjan/dev-server/pkg/config" | ||
"github.com/pietjan/dev-server/pkg/runner" | ||
"github.com/pietjan/dev-server/pkg/server" | ||
"github.com/pietjan/dev-server/pkg/server/router" | ||
"github.com/pietjan/dev-server/pkg/watcher" | ||
"github.com/pietjan/events" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed assets/ws-live-reload.js | ||
var scriptWS string | ||
|
||
func New(config config.Settings) *App { | ||
app := App{ | ||
event: events.NewProcessor(4, 100), | ||
runner: runner.New( | ||
runner.Build(config.Build.Command), | ||
runner.Target(config.Build.Bin), | ||
runner.Port(config.Proxy.Target), | ||
), | ||
watcher: watcher.New( | ||
watcher.Exclude(config.Watcher.Exclude...), | ||
), | ||
ticker: time.NewTicker(config.Watcher.Interval), | ||
} | ||
|
||
app.server = server.New( | ||
server.Port(config.Proxy.Port), | ||
server.Router( | ||
router.ProxyTarget(config.Proxy.Target), | ||
router.LiveReloadWS(scriptWS), | ||
router.WS(app.event), | ||
), | ||
) | ||
|
||
return &app | ||
} | ||
|
||
type App struct { | ||
event *events.Processor | ||
runner runner.Runner | ||
watcher watcher.Watcher | ||
server *http.Server | ||
ticker *time.Ticker | ||
} | ||
|
||
func (app *App) Start() error { | ||
go func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Println(r) | ||
} | ||
}() | ||
|
||
fmt.Println() | ||
if err := app.runner.Exec(); err != nil { | ||
log.Println(err) | ||
} | ||
fmt.Println() | ||
|
||
for { | ||
<-app.ticker.C | ||
changes, err := app.watcher.Changes() | ||
if err != nil { | ||
log.Println(err) | ||
continue | ||
} | ||
|
||
if len(changes) > 0 { | ||
for _, change := range changes { | ||
slog.Info("changed", "file", change) | ||
} | ||
|
||
fmt.Println() | ||
if err := app.runner.Exec(); err != nil { | ||
slog.Error("runner-exec", "error", err) | ||
} | ||
fmt.Println() | ||
|
||
app.event.Emit("message", "reload") | ||
} | ||
} | ||
}() | ||
|
||
return app.server.ListenAndServe() | ||
} | ||
|
||
func (app *App) Stop() error { | ||
app.ticker.Stop() | ||
app.event.Stop() | ||
app.server.Close() | ||
|
||
return app.runner.Stop() | ||
} | ||
|
||
func (app *App) On(event string, handler func(event *events.Event)) func() { | ||
return app.event.On(event, handler) | ||
} |
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,39 @@ | ||
let debounceTimeout; | ||
|
||
function debounce(func, delay) { | ||
return function (...args) { | ||
clearTimeout(debounceTimeout); | ||
debounceTimeout = setTimeout(() => func.apply(this, args), delay); | ||
}; | ||
} | ||
|
||
function connect() { | ||
if (window.liveReloadConnected) { | ||
console.log("ws-live-reload: already connected"); | ||
return | ||
} | ||
|
||
window.liveReloadConnected = true; | ||
var ws = new WebSocket('/__dev-server/ws'); | ||
|
||
ws.onmessage = debounce(function (event) { | ||
console.log(`ws-live-reload: ${event.data}`); | ||
if (event.data === "reload") { | ||
window.location.reload(true); // Force reload from server | ||
} | ||
}, 500); | ||
|
||
ws.onclose = function (e) { | ||
window.liveReloadConnected = false; | ||
setTimeout(function () { | ||
connect(); | ||
}, 1000); | ||
}; | ||
|
||
ws.onerror = function (err) { | ||
console.log(`ws-live-reload: ${err}`); | ||
ws.close(); | ||
}; | ||
} | ||
|
||
connect(); |
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 |
---|---|---|
@@ -1,12 +1,11 @@ | ||
module github.com/pietjan/dev-server | ||
|
||
go 1.21.0 | ||
go 1.23.0 | ||
|
||
require ( | ||
github.com/antelman107/net-wait-go v0.0.0-20220211074630-12d8a944b87d | ||
github.com/ghodss/yaml v1.0.0 | ||
github.com/julienschmidt/httprouter v1.3.0 | ||
github.com/lmittmann/tint v1.0.0 | ||
github.com/gorilla/websocket v1.5.3 | ||
github.com/lmittmann/tint v1.0.5 | ||
github.com/pietjan/events v0.0.0-20240830213322-3b74dae1474b | ||
gopkg.in/yaml.v2 v2.4.0 | ||
) | ||
|
||
require gopkg.in/yaml.v2 v2.4.0 // indirect |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.