https://github.com/samthor/hangar
Local daemon for developing distributed Fly.io servers
https://github.com/samthor/hangar
flyio
Last synced: about 2 months ago
JSON representation
Local daemon for developing distributed Fly.io servers
- Host: GitHub
- URL: https://github.com/samthor/hangar
- Owner: samthor
- License: apache-2.0
- Created: 2023-11-15T05:43:39.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-31T01:11:12.000Z (over 2 years ago)
- Last Synced: 2026-01-05T16:56:10.614Z (5 months ago)
- Topics: flyio
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Hangar provides a local daemon for developing distributed Fly.io servers locally in Go.
## Usage
To start the package at `./demo` in this repository, you can:
```bash
$ go get github.com/samthor/hangar/bin
$ go run github.com/samthor/hangar/bin -p github.com/samthor/hangar/demo
```
This starts a daemon with small number of machines all running in different 'regions'.
It performs basic load-balancing between them (with "the user" assumed to be in the 1st region), or respects [the `fly-prefer-region` header](https://fly.io/docs/reference/dynamic-request-routing/).
You can demonstrate having multiple jobs run with:
```bash
$ curl http://localhost:8080/info -H "fly-prefer-region: ams"
$ curl http://localhost:8080/info -H "fly-prefer-region: syd"
```
The code inside `./lib` helps provide a layer that hides local development vs. the real Fly deployed environment.
(It also works without either, but just provides sensible single-node defaults.)
You can use it in your code like:
```go
import (
hangar "github.com/samthor/hangar/lib"
)
func main() {
// find out about ourselves
self := hangar.Self()
// do something with other instances
others, err := hangar.Discover(context.Background())
// serve on the local $PORT
log.Fatal(http.ListenAndServe(hangar.ListenPort(), nil))
}
```
### Mount
Use `StoragePath()` with a mounted path as a no-op in prod, but to get a local path in dev created under your home directory (in "~/.fly/hangar/").
This doesn't quite match Fly's semantics.
## Extensions/TODOs
This has no knowledge of process groups.
In production, actively only discovers instances in the same process group—has no knowledge of them.
This currently runs Go packages, but really, it could run any command N times—Hangar just sets `$PORT` and other environment variables.
Assumes that the processes under control stop after some time (does not kill it when idle).
Restarts on non-zero exit code (same as Fly).