diff --git a/demo/config.json b/demo/config.json new file mode 100644 index 0000000..c6f716c --- /dev/null +++ b/demo/config.json @@ -0,0 +1,12 @@ +{ + "app_name": "Awesome App", + "version": 1.4, + "debug": true, + "database": { + "adapter": "mysql", + "host": "127.0.0.1", + "port": 3306, + "username": "root", + "password": "" + } +} diff --git a/demo/demo.go b/demo/demo.go new file mode 100644 index 0000000..4374a5e --- /dev/null +++ b/demo/demo.go @@ -0,0 +1,47 @@ +package main + +import ( + "flag" + "log" + + "github.com/localhots/secondly" +) + +// testConf is our app's configuration +type testConf struct { + AppName string `json:"app_name"` + Version float32 `json:"version"` + Debug bool `json:"debug"` + Database testDatabaseConf `json:"database"` +} + +type testDatabaseConf struct { + Adapter string `json:"adapter"` + Host string `json:"host"` + Port int `json:"port"` + Username string `json:"username"` + Password string `json:"password"` +} + +// conf is the variable that holds configuration +var conf = testConf{} + +func main() { + // Setting up flags + secondly.SetupFlags() + flag.Parse() + + // Delegating configuration management to Secondly + secondly.Manage(&conf) + // Handling file system events + secondly.HandleFSEvents() + // Starting a web server + secondly.StartServer("", 5500) + // Defining callbacks + secondly.OnChange("AppName", func(o, n interface{}) { + log.Printf("OMG! AppName changed from %q to %q", o, n) + }) + + // Other application startup logic + select {} +} diff --git a/demo/screenshot.png b/demo/screenshot.png new file mode 100644 index 0000000..5fffd9a Binary files /dev/null and b/demo/screenshot.png differ diff --git a/file_ops.go b/file_ops.go index 8f8b2c1..740f8d7 100644 --- a/file_ops.go +++ b/file_ops.go @@ -23,13 +23,14 @@ func readFile(file string) ([]byte, error) { } func writeFile(file string, body []byte) error { - var fd *os.File var err error if ok := fileExist(file); !ok { if err = mkdirp(file); err != nil { return err } } + + var fd *os.File if fd, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, filePerm); err != nil { return err }