Skip to content

Commit

Permalink
rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietjan committed Aug 31, 2024
1 parent 14b2dee commit 3c1bd9c
Show file tree
Hide file tree
Showing 16 changed files with 552 additions and 346 deletions.
108 changes: 108 additions & 0 deletions app/app.go
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)
}
39 changes: 39 additions & 0 deletions app/assets/ws-live-reload.js
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();
89 changes: 0 additions & 89 deletions config.go

This file was deleted.

11 changes: 5 additions & 6 deletions go.mod
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
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/antelman107/net-wait-go v0.0.0-20220211074630-12d8a944b87d h1:W/DlgbaUy+TYYjR2wWYkLhBbM1siBlkebM8hszeGHzI=
github.com/antelman107/net-wait-go v0.0.0-20220211074630-12d8a944b87d/go.mod h1:+tQQjzrp2501Nd6JXrb9s/XsNvFK3ZbxOnCdQl/vDRo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/lmittmann/tint v1.0.0 h1:fzEj70K1L58uyoePQxKe+ezDZJ5pybiWGdA0JeFvvyw=
github.com/lmittmann/tint v1.0.0/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/pietjan/events v0.0.0-20240830213322-3b74dae1474b h1:2UZecRjjyMj83quOv4jnyoCpm377Xy2Ua4Cjofy6RG4=
github.com/pietjan/events v0.0.0-20240830213322-3b74dae1474b/go.mod h1:a07vtXLJWeiIRfjvwuWDknuFGe6998UnQM141vgDpCE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
7 changes: 0 additions & 7 deletions live-reload.js

This file was deleted.

Loading

0 comments on commit 3c1bd9c

Please sign in to comment.