From 9690d27cd30960e29f38ba9d3aa80e5edadd3545 Mon Sep 17 00:00:00 2001 From: Jimmy Li Date: Tue, 12 Nov 2024 14:25:37 -0800 Subject: [PATCH] feat: create new helper which enables passing in of a key manager (#109) --- server/api.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/server/api.go b/server/api.go index 192c250..0a5785d 100644 --- a/server/api.go +++ b/server/api.go @@ -55,16 +55,12 @@ func combine(f, g func(http.HandlerFunc) http.HandlerFunc) func(http.HandlerFunc } } -// GetRouter creates the mux router that serves knox routes. -// All routes are declared in this file. Each handler itself takes in the db and -// auth provider interfaces and returns a handler that the is processed through -// the API Middleware. -func GetRouter( +// GetRouterFromKeyManager creates the mux router that serves knox routes from a key manager +func GetRouterFromKeyManager( cryptor keydb.Cryptor, - db keydb.DB, + keyManager KeyManager, decorators [](func(http.HandlerFunc) http.HandlerFunc), additionalRoutes []Route) (*mux.Router, error) { - existingRouteIds := map[string]Route{} existingRouteMethodAndPaths := map[string]map[string]Route{} allRoutes := append(routes[:], additionalRoutes[:]...) @@ -103,16 +99,28 @@ func GetRouter( decorator = combine(decorators[j], decorator) } - m := NewKeyManager(cryptor, db) - - r.NotFoundHandler = setupRoute("404", m)(decorator(WriteErr(errF(knox.NotFoundCode, "")))) + r.NotFoundHandler = setupRoute("404", keyManager)(decorator(WriteErr(errF(knox.NotFoundCode, "")))) for _, route := range allRoutes { - addRoute(r, route, decorator, m) + addRoute(r, route, decorator, keyManager) } return r, nil } +// GetRouter creates the mux router that serves knox routes. +// All routes are declared in this file. Each handler itself takes in the db and +// auth provider interfaces and returns a handler that the is processed through +// the API Middleware. +func GetRouter( + cryptor keydb.Cryptor, + db keydb.DB, + decorators [](func(http.HandlerFunc) http.HandlerFunc), + additionalRoutes []Route) (*mux.Router, error) { + m := NewKeyManager(cryptor, db) + + return GetRouterFromKeyManager(cryptor, m, decorators, additionalRoutes) +} + func addRoute( router *mux.Router, route Route,