Skip to content

Add fsnotify support to gin #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ Then verify that `gin` was installed correctly:
gin -h
~~~

## Installing with fsnotify support

If you're in an evironment like Linux, your operating system may support fsnotify events to trigger rebuilds. If you'd like support for this you can use the following command instead of the one above:

~~~ shell
go get -tags fsnotify github.com/codegangsta/gin
~~~

## 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. Web frameworks like [Martini](http://github.com/codegangsta/martini) do this out of the box.
103 changes: 103 additions & 0 deletions fsnotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//+build fsnotify
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@commondream The default for this PR is actually to be without fsnotify. It has to be built with go build -tags fsnotify Though, it seems to have a conflict currently.

// Only build when the fsnotify tag is used

package main

import (
"code.google.com/p/go.exp/fsnotify"
"os"
"path/filepath"
"time"
)

// This is to prevent unneeded rebuilds
var (
watched = make(map[string]bool)
)

func addPaths(watcher *fsnotify.Watcher, path string) error {
if watched[path] {
return nil
}

return filepath.Walk(path, func(wpath string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if wpath == ".git" {
return filepath.SkipDir
}

if watched[wpath] {
return nil
}

if info.IsDir() {
watched[wpath] = true
err = watcher.Watch(wpath)
if err != nil {
return err
}
if path != wpath {
return addPaths(watcher, wpath)
}
} else {
if filepath.Ext(wpath) == ".go" {
watched[wpath] = true
}
}

return nil
})
}

func scanChanges(watchPath string, cb scanCallback) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
logger.Fatal(err)
}

err = addPaths(watcher, watchPath)
if err != nil {
logger.Fatal(err)
}

for {
select {
case ev := <-watcher.Event:
path := ev.Name
if watched[path] {
if ev.IsDelete() {
err = watcher.RemoveWatch(path)
if err != nil {
logger.Fatal(err)
}
watched[path] = false
continue
}

if filepath.Ext(path) == ".go" {
cb(path)
// Drain events that might have been triggered since this build
drain := true
for drain {
select {
case <-watcher.Event:
case <-time.After(250 * time.Millisecond):
drain = false
}
}
}
} else {
if ev.IsCreate() {
// We don't really care /too/ much if this fails...
addPaths(watcher, path)
}
}

case err := <-watcher.Error:
logger.Fatal(err)
}
}
}
25 changes: 2 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"
"github.com/codegangsta/cli"
"github.com/codegangsta/envy/lib"
Expand All @@ -14,11 +13,12 @@ import (
)

var (
startTime = time.Now()
logger = log.New(os.Stdout, "[gin] ", 0)
buildError error
)

type scanCallback func(path string)

func main() {
app := cli.NewApp()
app.Name = "gin"
Expand Down Expand Up @@ -119,24 +119,3 @@ func build(builder gin.Builder, logger *log.Logger) {

time.Sleep(100 * time.Millisecond)
}

type scanCallback func(path string)

func scanChanges(watchPath string, cb scanCallback) {
for {
filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if path == ".git" {
return filepath.SkipDir
}

if filepath.Ext(path) == ".go" && info.ModTime().After(startTime) {
cb(path)
startTime = time.Now()
return errors.New("done")
}

return nil
})
time.Sleep(500 * time.Millisecond)
}
}
35 changes: 35 additions & 0 deletions scan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//+build !fsnotify
// The default is to build this scanner

package main

import (
"errors"
"os"
"path/filepath"
"time"
)

var (
startTime = time.Now()
)

func scanChanges(watchPath string, cb scanCallback) {
for {
filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if path == ".git" {
return filepath.SkipDir
}

if filepath.Ext(path) == ".go" && info.ModTime().After(startTime) {
cb(path)
startTime = time.Now()
return errors.New("done")
}

return nil
})

time.Sleep(500 * time.Millisecond)
}
}