|
@@ -12,6 +12,7 @@ import (
|
12
|
12
|
"plugin"
|
13
|
13
|
"time"
|
14
|
14
|
|
|
15
|
+ "github.com/fsnotify/fsnotify"
|
15
|
16
|
"golang.org/x/net/websocket"
|
16
|
17
|
)
|
17
|
18
|
|
|
@@ -77,25 +78,35 @@ func getPlugins(pluginDir string) (map[int][]*Transformer, error) {
|
77
|
78
|
return ret, nil
|
78
|
79
|
}
|
79
|
80
|
|
80
|
|
-func handler(w http.ResponseWriter, r *http.Request) {
|
81
|
|
- fi, _ := os.Open("lca.jpg")
|
82
|
|
- i, _ := jpeg.Decode(fi)
|
|
81
|
+var processed image.Image
|
83
|
82
|
|
84
|
|
- plugins, err := getPlugins(pluginDir)
|
85
|
|
- if err != nil {
|
86
|
|
- panic(err)
|
87
|
|
- }
|
|
83
|
+func processImage(events <-chan fsnotify.Event) {
|
|
84
|
+ for {
|
|
85
|
+ fi, _ := os.Open("lca.jpg")
|
|
86
|
+ i, _ := jpeg.Decode(fi)
|
88
|
87
|
|
89
|
|
- for _, p := range plugins[1] {
|
90
|
|
- fmt.Println("Apply", p.name)
|
91
|
|
- i = p.t(i)
|
92
|
|
- }
|
93
|
|
- for _, p := range plugins[0] {
|
94
|
|
- fmt.Println("Apply", p.name)
|
95
|
|
- i = p.t(i)
|
|
88
|
+ plugins, err := getPlugins(pluginDir)
|
|
89
|
+ if err != nil {
|
|
90
|
+ panic(err)
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ for _, p := range plugins[1] {
|
|
94
|
+ fmt.Println("Apply", p.name)
|
|
95
|
+ i = p.t(i)
|
|
96
|
+ }
|
|
97
|
+ for _, p := range plugins[0] {
|
|
98
|
+ fmt.Println("Apply", p.name)
|
|
99
|
+ i = p.t(i)
|
|
100
|
+ }
|
|
101
|
+ processed = i
|
|
102
|
+
|
|
103
|
+ // Wait for a change in folder
|
|
104
|
+ <-events
|
96
|
105
|
}
|
|
106
|
+}
|
97
|
107
|
|
98
|
|
- jpeg.Encode(w, i, nil)
|
|
108
|
+func handler(w http.ResponseWriter, r *http.Request) {
|
|
109
|
+ jpeg.Encode(w, processed, nil)
|
99
|
110
|
}
|
100
|
111
|
|
101
|
112
|
func wsHandler(ws *websocket.Conn) {
|
|
@@ -110,6 +121,17 @@ var pluginDir string
|
110
|
121
|
func main() {
|
111
|
122
|
pluginDir = os.Args[1]
|
112
|
123
|
|
|
124
|
+ watcher, err := fsnotify.NewWatcher()
|
|
125
|
+ if err != nil {
|
|
126
|
+ log.Fatal(err)
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ go processImage(watcher.Events)
|
|
130
|
+ err = watcher.Add(pluginDir)
|
|
131
|
+ if err != nil {
|
|
132
|
+ log.Fatal(err)
|
|
133
|
+ }
|
|
134
|
+
|
113
|
135
|
http.HandleFunc("/a.jpg", handler)
|
114
|
136
|
http.Handle("/ws", websocket.Handler(wsHandler))
|
115
|
137
|
http.ListenAndServe(":8080", nil)
|