Przeglądaj źródła

Introduce priority amongst plugins

+ a bit more logging
Gildas Chabot 8 lat temu
rodzic
commit
4f78290353
2 zmienionych plików z 41 dodań i 8 usunięć
  1. 39 8
      main.go
  2. 2 0
      pluginSrc/grayscale/plugin.go

+ 39 - 8
main.go

@@ -1,6 +1,8 @@
1
 package main
1
 package main
2
 
2
 
3
 import (
3
 import (
4
+	"errors"
5
+	"fmt"
4
 	"image"
6
 	"image"
5
 	"image/jpeg"
7
 	"image/jpeg"
6
 	"log"
8
 	"log"
@@ -10,19 +12,43 @@ import (
10
 	"plugin"
12
 	"plugin"
11
 )
13
 )
12
 
14
 
13
-func loadPlugin(path string) (func(image.Image) image.Image, error) {
15
+type Transformer struct {
16
+	name     string
17
+	t        func(image.Image) image.Image
18
+	priority int // priority can be 0 or 1
19
+}
20
+
21
+func loadPlugin(path string) (*Transformer, error) {
22
+	fmt.Println("Loading", path)
14
 	p, err := plugin.Open(path)
23
 	p, err := plugin.Open(path)
15
 	if err != nil {
24
 	if err != nil {
16
 		return nil, err
25
 		return nil, err
17
 	}
26
 	}
18
-	transform, err := p.Lookup("Transform")
27
+
28
+	transformSymbol, err := p.Lookup("Transform")
19
 	if err != nil {
29
 	if err != nil {
20
 		return nil, err
30
 		return nil, err
21
 	}
31
 	}
22
-	return transform.(func(image.Image) image.Image), nil
32
+	transform, ok := transformSymbol.(func(image.Image) image.Image)
33
+	if !ok {
34
+		return nil, errors.New(fmt.Sprintf("%s: Transform not a func(image.Image) image.Image", path))
35
+	}
36
+
37
+	var priority int
38
+	prioritySymbol, err := p.Lookup("Priority")
39
+	if err != nil {
40
+		fmt.Println("Error looking up Priority:", err)
41
+	} else {
42
+		var ok bool
43
+		priority, ok = prioritySymbol.(int)
44
+		if !ok {
45
+			fmt.Println("Error casting Priority to int")
46
+		}
47
+	}
48
+	return &Transformer{path, transform, priority}, nil
23
 }
49
 }
24
 
50
 
25
-func getPlugins(pluginDir string) ([]func(image.Image) image.Image, error) {
51
+func getPlugins(pluginDir string) (map[int][]*Transformer, error) {
26
 	pDir, err := os.Open(pluginDir)
52
 	pDir, err := os.Open(pluginDir)
27
 	if err != nil {
53
 	if err != nil {
28
 		return nil, err
54
 		return nil, err
@@ -32,7 +58,7 @@ func getPlugins(pluginDir string) ([]func(image.Image) image.Image, error) {
32
 		return nil, err
58
 		return nil, err
33
 	}
59
 	}
34
 
60
 
35
-	var ret []func(image.Image) image.Image
61
+	ret := make(map[int][]*Transformer)
36
 	for _, pFile := range pFiles {
62
 	for _, pFile := range pFiles {
37
 		if pFile.IsDir() {
63
 		if pFile.IsDir() {
38
 			continue
64
 			continue
@@ -42,7 +68,7 @@ func getPlugins(pluginDir string) ([]func(image.Image) image.Image, error) {
42
 		if err != nil {
68
 		if err != nil {
43
 			log.Printf("Failed to load pluging %s", p)
69
 			log.Printf("Failed to load pluging %s", p)
44
 		} else {
70
 		} else {
45
-			ret = append(ret, p)
71
+			ret[p.priority] = append(ret[p.priority], p)
46
 		}
72
 		}
47
 	}
73
 	}
48
 	return ret, nil
74
 	return ret, nil
@@ -57,8 +83,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
57
 		panic(err)
83
 		panic(err)
58
 	}
84
 	}
59
 
85
 
60
-	for _, p := range plugins {
61
-		i = p(i)
86
+	for _, p := range plugins[1] {
87
+		fmt.Println("Apply", p.name)
88
+		i = p.t(i)
89
+	}
90
+	for _, p := range plugins[0] {
91
+		fmt.Println("Apply", p.name)
92
+		i = p.t(i)
62
 	}
93
 	}
63
 
94
 
64
 	jpeg.Encode(w, i, nil)
95
 	jpeg.Encode(w, i, nil)

+ 2 - 0
pluginSrc/grayscale/plugin.go

@@ -2,6 +2,8 @@ package main
2
 
2
 
3
 import "image"
3
 import "image"
4
 
4
 
5
+var Priority int = 1
6
+
5
 // Convert to grayscale image
7
 // Convert to grayscale image
6
 func Transform(src image.Image) image.Image {
8
 func Transform(src image.Image) image.Image {
7
 	bounds := src.Bounds()
9
 	bounds := src.Bounds()