feat: add an interface for handling http methods to be handled abstractly and implement the basic upload endpoint

This commit is contained in:
Derrick Hammer 2024-01-14 20:53:44 -05:00
parent 3d41119f74
commit 38e330e02b
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 17 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package interfaces
import ( import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"go.sia.tech/jape"
) )
//go:generate mockgen -source=http.go -destination=../mocks/interfaces/http.go -package=interfaces //go:generate mockgen -source=http.go -destination=../mocks/interfaces/http.go -package=interfaces
@ -10,3 +11,7 @@ type HTTPService interface {
Service Service
GetHandler() *httprouter.Router GetHandler() *httprouter.Router
} }
type HTTPHandler interface {
SmallFileUpload(context *jape.Context)
}

View File

@ -15,16 +15,21 @@ var _ interfaces.HTTPService = (*HTTPImpl)(nil)
type HTTPImpl struct { type HTTPImpl struct {
node interfaces.Node node interfaces.Node
handler interfaces.HTTPHandler
} }
func NewHTTP(node interfaces.Node) *HTTPImpl { func NewHTTP(node interfaces.Node, handler interfaces.HTTPHandler) interfaces.HTTPService {
return &HTTPImpl{node: node} return &HTTPImpl{
node: node,
handler: handler,
}
} }
func (h *HTTPImpl) GetHandler() *httprouter.Router { func (h *HTTPImpl) GetHandler() *httprouter.Router {
mux := jape.Mux(map[string]jape.Handler{ mux := jape.Mux(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,
}) })
return mux return mux
@ -80,3 +85,7 @@ 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)
}