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

This commit is contained in:
Derrick Hammer 2024-01-16 11:26:27 -05:00
parent 28444ca456
commit 7261b35f94
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 12 additions and 43 deletions

View File

@ -9,14 +9,5 @@ import (
type HTTPService interface { type HTTPService interface {
Service Service
GetHttpRouter() *httprouter.Router GetHttpRouter(inject map[string]jape.Handler) *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)
} }

View File

@ -15,11 +15,6 @@ var _ interfaces.HTTPService = (*HTTPImpl)(nil)
type HTTPImpl struct { type HTTPImpl struct {
node interfaces.Node node interfaces.Node
handler interfaces.HTTPHandler
}
func (h *HTTPImpl) SetHttpHandler(handler interfaces.HTTPHandler) {
h.handler = handler
} }
func NewHTTP(node interfaces.Node) interfaces.HTTPService { func NewHTTP(node interfaces.Node) interfaces.HTTPService {
@ -28,18 +23,17 @@ func NewHTTP(node interfaces.Node) interfaces.HTTPService {
} }
} }
func (h *HTTPImpl) GetHttpRouter() *httprouter.Router { func (h *HTTPImpl) GetHttpRouter(inject map[string]jape.Handler) *httprouter.Router {
mux := jape.Mux(map[string]jape.Handler{ routes := map[string]jape.Handler{
"GET /s5/version": h.versionHandler, "GET /s5/version": h.versionHandler,
"GET /s5/p2p": h.p2pHandler, "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,
})
return mux for k, v := range inject {
routes[k] = v
}
return jape.Mux(routes)
} }
func (h *HTTPImpl) Node() interfaces.Node { func (h *HTTPImpl) Node() interfaces.Node {
@ -92,19 +86,3 @@ func (h *HTTPImpl) p2pHandler(ctx jape.Context) {
h.node.ConnectionTracker().Done() 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)
}