Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 7a63972

Browse files
Craig Petersonkylebrandt
authored andcommitted
adding AddRoutes version that accepts middleware functions
1 parent aaf0bbf commit 7a63972

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

web/web.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,30 @@ import (
2020
)
2121

2222
// esc -o static.go -pkg web static
23+
24+
//AddRoutes will add annotate routes to the given router, using the specified prefix
2325
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+
}
2440
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")
3147
if !enableUI {
3248
return nil
3349
}
@@ -41,7 +57,7 @@ func AddRoutes(router *mux.Router, prefix string, b []backend.Backend, enableUI,
4157
return err
4258
}
4359
router.PathPrefix("/static/").Handler(http.FileServer(webFS))
44-
router.PathPrefix("/").HandlerFunc(Index).Methods("GET")
60+
router.PathPrefix("/").Handler(readMiddleware(Index)).Methods("GET")
4561
return nil
4662
}
4763

@@ -272,9 +288,9 @@ func GetAnnotations(w http.ResponseWriter, req *http.Request) {
272288
for param := range values {
273289
sp := strings.Split(param, ":")
274290
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
278294
}
279295
filter := backend.FieldFilter{Field: sp[0], Value: values.Get(param)}
280296
if len(sp) > 1 {

0 commit comments

Comments
 (0)