Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/blakewilliams/go-reloader
A simple live-reloader package for Go.
https://github.com/blakewilliams/go-reloader
Last synced: 16 days ago
JSON representation
A simple live-reloader package for Go.
- Host: GitHub
- URL: https://github.com/blakewilliams/go-reloader
- Owner: BlakeWilliams
- License: mit
- Created: 2023-10-08T14:36:23.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-19T18:57:18.000Z (about 1 year ago)
- Last Synced: 2024-12-15T14:57:10.964Z (19 days ago)
- Language: Go
- Size: 23.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-reloader
go-reloader is a simple implementation of Go live-reloading for development. It works by running a process that watches for changes in specified files and directories, and rebuilds + restarts the process when a change is detected.
Unlike other live-reload implementations, go-reloader is configured and run in your application so that custom loggers, build implementations, file watchers, and other aspects of the live-reload process can be customized.
go-reloader is still a WIP but is usable in its current state. Improvements and contributions are welcome!
## Usage
The example below shows how to use go-reloader to watch for changes in the `internal`, `pkg`, and `assets` directories, and restart the application when a change is detected. It uses a simple HTTP health check to determine when the application is ready to receive requests to prevent the proxy from sending requests to the application before it's ready.
```go
l := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))watcher, err := reloader.NewWatcher(
"internal",
"pkg",
"assets",
)if err != nil {
panic(err)
}appReloader := reloader.New(
reloader.NewBuilder(
[]string{"go", "build", "-o", "buzzword", "./cmd/app"},
[]string{"./buzzword", "serve"},
l,
func() bool {
res, err := http.Get("http://localhost:8080/_status")
if err != nil {
return false
}if res.StatusCode != 200 {
return false
}return true
},
),
watcher,
l,
)go func() {
err := appReloader.ListenAndProxy(ctx, ":9000", ":8080")
if err != nil && err != context.Canceled {
panic(err)
}
}()
```