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

View File

@ -14,17 +14,22 @@ import (
var _ interfaces.HTTPService = (*HTTPImpl)(nil)
type HTTPImpl struct {
node interfaces.Node
node interfaces.Node
handler interfaces.HTTPHandler
}
func NewHTTP(node interfaces.Node) *HTTPImpl {
return &HTTPImpl{node: node}
func NewHTTP(node interfaces.Node, handler interfaces.HTTPHandler) interfaces.HTTPService {
return &HTTPImpl{
node: node,
handler: handler,
}
}
func (h *HTTPImpl) GetHandler() *httprouter.Router {
mux := jape.Mux(map[string]jape.Handler{
"GET /s5/version": h.versionHandler,
"GET /s5/p2p": h.p2pHandler,
"POST /s5/upload": h.uploadHandler,
})
return mux
@ -80,3 +85,7 @@ func (h *HTTPImpl) p2pHandler(ctx jape.Context) {
h.node.ConnectionTracker().Done()
}()
}
func (h *HTTPImpl) uploadHandler(context jape.Context) {
h.handler.SmallFileUpload(&context)
}