From 7261b35f946c88f10bd43fdc7b590c6d79d028e1 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 16 Jan 2024 11:26:27 -0500 Subject: [PATCH] refactor: switch to composing routes vs using a handler so we can control the api better outside the library, and only define what the library absolutely needs --- interfaces/http.go | 11 +---------- service/http.go | 44 +++++++++++--------------------------------- 2 files changed, 12 insertions(+), 43 deletions(-) diff --git a/interfaces/http.go b/interfaces/http.go index 8aeb78e..d13ffef 100644 --- a/interfaces/http.go +++ b/interfaces/http.go @@ -9,14 +9,5 @@ import ( type HTTPService interface { Service - GetHttpRouter() *httprouter.Router - SetHttpHandler(handler HTTPHandler) -} - -type HTTPHandler interface { - SmallFileUpload(context *jape.Context) - AccountRegisterChallenge(context *jape.Context) - AccountRegister(context *jape.Context) - AccountLoginChallenge(context *jape.Context) - AccountLogin(context *jape.Context) + GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router } diff --git a/service/http.go b/service/http.go index adc6856..445c75d 100644 --- a/service/http.go +++ b/service/http.go @@ -14,12 +14,7 @@ import ( var _ interfaces.HTTPService = (*HTTPImpl)(nil) type HTTPImpl struct { - node interfaces.Node - handler interfaces.HTTPHandler -} - -func (h *HTTPImpl) SetHttpHandler(handler interfaces.HTTPHandler) { - h.handler = handler + node interfaces.Node } func NewHTTP(node interfaces.Node) interfaces.HTTPService { @@ -28,18 +23,17 @@ func NewHTTP(node interfaces.Node) interfaces.HTTPService { } } -func (h *HTTPImpl) GetHttpRouter() *httprouter.Router { - mux := jape.Mux(map[string]jape.Handler{ - "GET /s5/version": h.versionHandler, - "GET /s5/p2p": h.p2pHandler, - "POST /s5/upload": h.uploadHandler, - "GET /account/register": h.accountRegisterChallengeHandler, - "POST /account/register": h.accountRegisterHandler, - "GET /account/login": h.accountLoginChallengeHandler, - "POST /account/login": h.accountLoginHandler, - }) +func (h *HTTPImpl) GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router { + routes := map[string]jape.Handler{ + "GET /s5/version": h.versionHandler, + "GET /s5/p2p": h.p2pHandler, + } - return mux + for k, v := range inject { + routes[k] = v + } + + return jape.Mux(routes) } func (h *HTTPImpl) Node() interfaces.Node { @@ -92,19 +86,3 @@ func (h *HTTPImpl) p2pHandler(ctx jape.Context) { h.node.ConnectionTracker().Done() }() } - -func (h *HTTPImpl) uploadHandler(context jape.Context) { - h.handler.SmallFileUpload(&context) -} -func (h *HTTPImpl) accountRegisterChallengeHandler(context jape.Context) { - h.handler.AccountRegisterChallenge(&context) -} -func (h *HTTPImpl) accountRegisterHandler(context jape.Context) { - h.handler.AccountRegisterChallenge(&context) -} -func (h *HTTPImpl) accountLoginChallengeHandler(context jape.Context) { - h.handler.AccountLoginChallenge(&context) -} -func (h *HTTPImpl) accountLoginHandler(context jape.Context) { - h.handler.AccountLoginChallenge(&context) -}