func Example() { // Create file watcher watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatalln(err) } defer watcher.Close() // Watch dir err = watcher.Watch("/path/to/watched/dir") if err != nil { log.Fatalln(err) } // Start LiveReload server lr, err := lrserver.New(lrserver.DefaultName, lrserver.DefaultPort) if err != nil { log.Fatalln(err) } go lr.ListenAndServe() // Start goroutine that requests reload upon watcher event go func() { for { event := <-watcher.Event lr.Reload(event.Name) } }() // Start serving html http.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) { rw.Write([]byte(html)) }) http.ListenAndServe(":3000", nil) }
func ExampleNewWatcher() { watcher, err := fsnotify.NewWatcher() if err != nil { log.Fatal(err) } go func() { for { select { case ev := <-watcher.Event: log.Println("event:", ev) case err := <-watcher.Error: log.Println("error:", err) } } }() err = watcher.Watch("/tmp/foo") if err != nil { log.Fatal(err) } }
// run executes the main loop of the chatterbox daemon func (d *Daemon) run() error { profile := new(proto.Profile) if err := persistence.UnmarshalFromFile(d.OurChatterboxProfilePath(), profile); err != nil { return err } ourConn, err := d.cc.DialServer(d.Dename, d.ServerAddressTCP, int(d.ServerPortTCP), (*[32]byte)(&d.ServerTransportPK), (*[32]byte)(&profile.UserIDAtServer), (*[32]byte)(&d.TransportSecretKeyForServer)) if err != nil { return err } defer ourConn.Close() notifies := make(chan *util.EnvelopeWithId) replies := make(chan *proto.ServerToClient) connToServer := &util.ConnectionToServer{ InBuf: d.inBuf, Conn: ourConn, ReadReply: replies, ReadEnvelope: notifies, } go connToServer.ReceiveMessages() prekeyPublics, prekeySecrets, err := d.updatePrekeys(connToServer) if err != nil { return err } watcher, err := fsnotify.NewWatcher() if err != nil { return err } defer watcher.Close() initFn := func(path string, f os.FileInfo, err error) error { if f.IsDir() { return d.processOutboxDir(path) } return d.processOutboxDir(filepath.Dir(path)) } err = WatchDir(watcher, d.OutboxDir(), initFn) if err != nil { return err } if err = util.EnablePush(connToServer); err != nil { return err } d.requestAllMessages(connToServer) for { select { case <-d.stop: return nil case ev := <-watcher.Event: // event in the directory structure; watch any new directories if _, err = os.Stat(ev.Name); err == nil { err = WatchDir(watcher, ev.Name, initFn) if err != nil { log.Printf("watch %s: %s", ev.Name, err) // TODO } d.processOutboxDir(ev.Name) } case envelopewithid := <-connToServer.ReadEnvelope: envelope := envelopewithid.Envelope id := envelopewithid.Id msgHash := sha256.Sum256(envelope) // assume it's the first message we're receiving from the person; try to decrypt message, ratch, index, err := d.decryptFirstMessage(envelope, prekeyPublics, prekeySecrets) if err == nil { // assumption was correct, found a prekey that matched if err := StoreRatchet(d, message.Dename, ratch); err != nil { return err } newPrekeyPublics := append(prekeyPublics[:index], prekeyPublics[index+1:]...) newPrekeySecrets := append(prekeySecrets[:index], prekeySecrets[index+1:]...) if err = StorePrekeys(d, newPrekeyPublics, newPrekeySecrets); err != nil { return err } //TODO: Update prekeys by removing index, store // is this done? What does it mean? if err := d.saveMessage(message); err != nil { return err } if err := util.DeleteMessages(connToServer, []*[32]byte{id}); err != nil { return err } } else { // try decrypting with a ratchet ratchets, err := AllRatchets(d, d.fillAuth, d.checkAuth) if err != nil { return err } // TODO: figure out what here should be atomic and comment if message, ratch, err := decryptMessage(envelope, ratchets); err == nil { if err := d.saveMessage(message); err != nil { return err } if err := StoreRatchet(d, message.Dename, ratch); err != nil { return err } } else { log.Printf("failed to decrypt %x: %s", msgHash, err) } if err := util.DeleteMessages(connToServer, []*[32]byte{id}); err != nil { return err } } case err := <-watcher.Error: if err != nil { return err } } } }