gin
Forked from https://github.com/codegangsta/gin
gin is a simple command line utility for live-reloading Go web applications.
Just run gin in your app directory and your web app will be served with
gin as a proxy. gin will automatically recompile your code when it
detects a change, and hand traffic over to the new build without dropping
requests. The proxy supports standard HTTP traffic, WebSockets, server-sent
events, and gRPC.
gin adheres to the "silence is golden" principle, so it will only complain
if there was a compiler error or if you succesfully compile after an error.
Installation
Assuming you have a working Go environment and GOPATH/bin is in your
PATH, gin is a breeze to install:
go install github.com/cwarden/gin@latest
Then verify that gin was installed correctly:
gin help
Basic usage
gin run main.go
Options
--laddr value, -l value listening address for the proxy server
--port value, -p value port for the proxy server (default: 3000)
--appPort value, -a value port for the Go web server (default: 3001)
--bin value, -b value name of generated binary file (default: "gin-bin")
--path value, -t value Path to watch files from (default: ".")
--pre-build value, -m value Command to run before `go build`
--build value, -d value Path to build files from (defaults to same value as --path)
--excludeDir value, -x value Relative directories to exclude
--immediate, -i run the server immediately after it's built
--all reloads whenever any file changes, as opposed to reloading only on .go file change
--godep, -g use godep when building
--buildArgs value Additional go build arguments
--certFile value TLS Certificate
--keyFile value TLS Certificate Key
--logPrefix value Setup custom log prefix
--notifications enable desktop notifications
--drainTimeout value How long to let a replaced app finish serving its open connections (default: 30s)
--help, -h show help
--version, -v print the version
Reloading Without Dropped Requests
When gin detects a change, the running application keeps serving requests for
the whole build. If the build fails, nothing changes: the current process stays
up and the build errors are shown in the browser.
When the build succeeds, gin starts the new binary as a second process on its
own port and waits for it to accept connections. Only then does the proxy send
new requests to it. Requests already in progress against the previous process --
including WebSocket and server-sent event streams -- are left alone; that
process is shut down once its last connection closes, or after --drainTimeout
if some connection never does.
There is no deadline on the new process: an application that takes its time
starting is not a failing one, and the process that is serving keeps serving for
however long the wait is, so waiting costs nothing. What ends the wait is the
new process exiting, in which case gin reports it and leaves the previous
process serving, or another change arriving, in which case the process that is
still starting is dropped in favour of the newer build.
Because two processes overlap during a reload, --appPort is a starting point
rather than a fixed port: gin picks the first free port at or above it for
each process, so the two never collide. Your application reads its port from
PORT, which gin sets per process.
Without --immediate, gin still waits for the first HTTP request before
starting your application. Once it is running, later rebuilds hand over as
described above.
While There Is Nothing To Serve
The proxy accepts connections as soon as it starts, which is before the first
build has produced a binary, and an application can take a while to start after
that. A browser that asks for a page in the meantime is shown what the build and
the application are printing to the console, updated as it is written, and the
page loads the application itself once it is accepting connections.
Anything that is not a browser -- an XHR call, an asset request, a gRPC call --
waits for the application instead, and is answered by it once it is up. If the
build fails, the compiler output is the response, as it is at any other time,
and a request waiting on an application that exits before it serves gets a 503
saying so.
The progress page reads /__gin__/status, which is the one path the proxy
answers itself rather than forwarding to your application.
Once an application is running, requests are never held: they go to the running
process for the whole of the next build.
Where the Binary Goes
By default gin builds into a temporary directory and removes it on exit, so
the binary does not sit in the directory you are working in -- which is also the
directory gin watches. Give --bin a path rather than a bare name to choose
the location yourself:
gin --bin build/myapp run main.go # written to ./build/myapp
gin --bin /tmp/myapp run main.go # written to /tmp/myapp
gin --bin myapp run main.go # written to a temporary directory
Wherever it ends up, the binary gin builds is never treated as a change, even
under --all, so writing it into a watched directory does not set off another
build.
Pre-build Commands
The --pre-build flag allows you to run commands before each build. This is useful for
generating assets, running code generators, or building dependencies.
Example using Make to build WebAssembly before the main Go application:
gin --pre-build "make web/static/app.wasm" run main.go
You can also use it for asset generation:
gin --pre-build "npm run build" run main.go
gRPC Proxying
gin can proxy gRPC traffic in addition to regular HTTP requests. For local
development this includes cleartext HTTP/2 (h2c), which is commonly used by
non-TLS gRPC servers.
If your application serves gRPC on the same port as HTTP handlers, gin will
forward both through the same proxy listener.
Supporting Gin in Your Web app
gin assumes that your web app binds itself to the PORT environment
variable so it can properly proxy requests to your app. This matters more with
overlapping reloads: each process is given a different PORT, so an app that
hardcodes its port will fail to start its replacement. Web frameworks
like Martini do this out of
the box.
Using flags?
When you normally start your server with flags
if you want to override any of them when running gin we suggest you
instead use github.com/namsral/flag
as explained in this post
If you want to still use the standard flag package but support environment variables, you might want to try github.com/peak6/envflag.