@@ -20,14 +20,30 @@ import (
20
20
)
21
21
22
22
// esc -o static.go -pkg web static
23
+
24
+ //AddRoutes will add annotate routes to the given router, using the specified prefix
23
25
func AddRoutes (router * mux.Router , prefix string , b []backend.Backend , enableUI , useLocalAssets bool ) error {
26
+ return AddRoutesWithMiddleware (router , prefix , b , enableUI , useLocalAssets , nil , nil )
27
+ }
28
+
29
+ func noopMiddleware (h http.HandlerFunc ) http.Handler { return h }
30
+
31
+ //AddRoutesWithMiddleware will add annotate routes to the given router, using the specified prefix. It accepts two middleware functions that will be applied to each route,
32
+ //depending on whether they are a "read" operation, or a "write" operation
33
+ func AddRoutesWithMiddleware (router * mux.Router , prefix string , b []backend.Backend , enableUI , useLocalAssets bool , readMiddleware , modifyMiddleware func (http.HandlerFunc ) http.Handler ) error {
34
+ if readMiddleware == nil {
35
+ readMiddleware = noopMiddleware
36
+ }
37
+ if modifyMiddleware == nil {
38
+ modifyMiddleware = noopMiddleware
39
+ }
24
40
backends = b
25
- router .HandleFunc (prefix + "/annotation" , InsertAnnotation ).Methods ("POST" , "PUT" )
26
- router .HandleFunc (prefix + "/annotation/query" , GetAnnotations ).Methods ("GET" )
27
- router .HandleFunc (prefix + "/annotation/{id}" , GetAnnotation ).Methods ("GET" )
28
- router .HandleFunc (prefix + "/annotation/{id}" , InsertAnnotation ).Methods ("PUT" )
29
- router .HandleFunc (prefix + "/annotation/{id}" , DeleteAnnotation ).Methods ("DELETE" )
30
- router .HandleFunc (prefix + "/annotation/values/{field}" , GetFieldValues ).Methods ("GET" )
41
+ router .Handle (prefix + "/annotation" , modifyMiddleware ( InsertAnnotation ) ).Methods ("POST" , "PUT" )
42
+ router .Handle (prefix + "/annotation/query" , readMiddleware ( GetAnnotations ) ).Methods ("GET" )
43
+ router .Handle (prefix + "/annotation/{id}" , readMiddleware ( GetAnnotation ) ).Methods ("GET" )
44
+ router .Handle (prefix + "/annotation/{id}" , modifyMiddleware ( InsertAnnotation ) ).Methods ("PUT" )
45
+ router .Handle (prefix + "/annotation/{id}" , modifyMiddleware ( DeleteAnnotation ) ).Methods ("DELETE" )
46
+ router .Handle (prefix + "/annotation/values/{field}" , readMiddleware ( GetFieldValues ) ).Methods ("GET" )
31
47
if ! enableUI {
32
48
return nil
33
49
}
@@ -41,7 +57,7 @@ func AddRoutes(router *mux.Router, prefix string, b []backend.Backend, enableUI,
41
57
return err
42
58
}
43
59
router .PathPrefix ("/static/" ).Handler (http .FileServer (webFS ))
44
- router .PathPrefix ("/" ).HandlerFunc ( Index ).Methods ("GET" )
60
+ router .PathPrefix ("/" ).Handler ( readMiddleware ( Index ) ).Methods ("GET" )
45
61
return nil
46
62
}
47
63
@@ -272,9 +288,9 @@ func GetAnnotations(w http.ResponseWriter, req *http.Request) {
272
288
for param := range values {
273
289
sp := strings .Split (param , ":" )
274
290
switch sp [0 ] {
275
- case annotate .Source , annotate .Host , annotate .CreationUser , annotate .Owner , annotate .Category , annotate .Message :
276
- default :
277
- continue
291
+ case annotate .Source , annotate .Host , annotate .CreationUser , annotate .Owner , annotate .Category , annotate .Message :
292
+ default :
293
+ continue
278
294
}
279
295
filter := backend.FieldFilter {Field : sp [0 ], Value : values .Get (param )}
280
296
if len (sp ) > 1 {
0 commit comments