mirror of
https://github.com/InazumaV/Ratte.git
synced 2026-02-04 04:30:09 +00:00
Initial commit
This commit is contained in:
75
common/watcher/http.go
Normal file
75
common/watcher/http.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package watcher
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type HTTPWatcher struct {
|
||||
hash [32]byte
|
||||
url string
|
||||
interval uint
|
||||
handler EventHandler
|
||||
errorHandler ErrorHandler
|
||||
close chan struct{}
|
||||
}
|
||||
|
||||
func NewHTTPWatcher(url string, interval uint) *HTTPWatcher {
|
||||
return &HTTPWatcher{
|
||||
url: url,
|
||||
interval: interval,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *HTTPWatcher) handle() error {
|
||||
rsp, err := http.Get(w.url)
|
||||
if err != nil {
|
||||
return fmt.Errorf("request error: %w", err)
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
b, err := io.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read body error: %w", err)
|
||||
}
|
||||
h := sha256.Sum256(b)
|
||||
if bytes.Equal(w.hash[:], h[:]) {
|
||||
return nil
|
||||
}
|
||||
w.hash = h
|
||||
err = w.handler(w.url)
|
||||
if err != nil {
|
||||
return fmt.Errorf("handle error: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *HTTPWatcher) SetEventHandler(handler EventHandler) {
|
||||
w.handler = handler
|
||||
}
|
||||
|
||||
func (w *HTTPWatcher) SetErrorHandler(handler ErrorHandler) {
|
||||
w.errorHandler = handler
|
||||
}
|
||||
|
||||
func (w *HTTPWatcher) Watch() error {
|
||||
go func() {
|
||||
for range time.Tick(time.Duration(w.interval) * time.Second) {
|
||||
select {
|
||||
case <-w.close:
|
||||
return
|
||||
default:
|
||||
}
|
||||
w.errorHandler(w.handle())
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *HTTPWatcher) Close() error {
|
||||
close(w.close)
|
||||
return nil
|
||||
}
|
||||
85
common/watcher/local.go
Normal file
85
common/watcher/local.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package watcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
)
|
||||
|
||||
type LocalWatcher struct {
|
||||
dir string
|
||||
filenames []string
|
||||
handler EventHandler
|
||||
errorHandler ErrorHandler
|
||||
watcher *fsnotify.Watcher
|
||||
close chan struct{}
|
||||
}
|
||||
|
||||
func NewLocalWatcher(dir string, filenames []string) *LocalWatcher {
|
||||
return &LocalWatcher{
|
||||
dir: dir,
|
||||
filenames: filenames,
|
||||
close: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *LocalWatcher) SetEventHandler(handler EventHandler) {
|
||||
w.handler = handler
|
||||
}
|
||||
func (w *LocalWatcher) SetErrorHandler(handler ErrorHandler) {
|
||||
w.errorHandler = handler
|
||||
}
|
||||
|
||||
func (w *LocalWatcher) handle(e fsnotify.Event) error {
|
||||
if (!e.Has(fsnotify.Write)) && (!e.Has(fsnotify.Create)) {
|
||||
return nil
|
||||
}
|
||||
name := path.Base(e.Name)
|
||||
file := ""
|
||||
for _, filename := range w.filenames {
|
||||
ok, _ := path.Match(filename, name)
|
||||
if ok {
|
||||
file = filename
|
||||
}
|
||||
}
|
||||
if len(file) == 0 {
|
||||
return nil
|
||||
}
|
||||
err := w.handler(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *LocalWatcher) Watch() error {
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return fmt.Errorf("new watcher error: %s", err)
|
||||
}
|
||||
go func() {
|
||||
defer watcher.Close()
|
||||
for {
|
||||
select {
|
||||
case e := <-watcher.Events:
|
||||
err := w.handle(e)
|
||||
if err != nil {
|
||||
w.errorHandler(err)
|
||||
}
|
||||
case err := <-watcher.Errors:
|
||||
if err != nil {
|
||||
w.errorHandler(err)
|
||||
}
|
||||
case <-w.close:
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
return watcher.Add(w.dir)
|
||||
}
|
||||
|
||||
func (w *LocalWatcher) Close() error {
|
||||
close(w.close)
|
||||
return w.watcher.Close()
|
||||
}
|
||||
11
common/watcher/watcher.go
Normal file
11
common/watcher/watcher.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package watcher
|
||||
|
||||
type EventHandler func(filename string) error
|
||||
type ErrorHandler func(err error)
|
||||
|
||||
type Watcher interface {
|
||||
SetEventHandler(handler EventHandler)
|
||||
SetErrorHandler(handler ErrorHandler)
|
||||
Watch() error
|
||||
Close() error
|
||||
}
|
||||
Reference in New Issue
Block a user