瀏覽代碼

Introduce priority amongst plugins

+ a bit more logging
Gildas Chabot 8 年之前
父節點
當前提交
4f78290353
共有 2 個文件被更改,包括 41 次插入8 次删除
  1. 39 8
      main.go
  2. 2 0
      pluginSrc/grayscale/plugin.go

+ 39 - 8
main.go

@@ -1,6 +1,8 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"errors"
5
+	"fmt"
4 6
 	"image"
5 7
 	"image/jpeg"
6 8
 	"log"
@@ -10,19 +12,43 @@ import (
10 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 23
 	p, err := plugin.Open(path)
15 24
 	if err != nil {
16 25
 		return nil, err
17 26
 	}
18
-	transform, err := p.Lookup("Transform")
27
+
28
+	transformSymbol, err := p.Lookup("Transform")
19 29
 	if err != nil {
20 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 52
 	pDir, err := os.Open(pluginDir)
27 53
 	if err != nil {
28 54
 		return nil, err
@@ -32,7 +58,7 @@ func getPlugins(pluginDir string) ([]func(image.Image) image.Image, error) {
32 58
 		return nil, err
33 59
 	}
34 60
 
35
-	var ret []func(image.Image) image.Image
61
+	ret := make(map[int][]*Transformer)
36 62
 	for _, pFile := range pFiles {
37 63
 		if pFile.IsDir() {
38 64
 			continue
@@ -42,7 +68,7 @@ func getPlugins(pluginDir string) ([]func(image.Image) image.Image, error) {
42 68
 		if err != nil {
43 69
 			log.Printf("Failed to load pluging %s", p)
44 70
 		} else {
45
-			ret = append(ret, p)
71
+			ret[p.priority] = append(ret[p.priority], p)
46 72
 		}
47 73
 	}
48 74
 	return ret, nil
@@ -57,8 +83,13 @@ func handler(w http.ResponseWriter, r *http.Request) {
57 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 95
 	jpeg.Encode(w, i, nil)

+ 2 - 0
pluginSrc/grayscale/plugin.go

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