refactor: re-implement s5 routes

This commit is contained in:
Derrick Hammer 2024-01-16 11:42:50 -05:00
parent 69b1938e87
commit c4f0226d1a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 31 additions and 26 deletions

View File

@ -1,8 +1,10 @@
package api
import (
"git.lumeweb.com/LumeWeb/portal/api/s5"
"git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/protocols"
"go.sia.tech/jape"
)
var (
@ -17,9 +19,19 @@ func NewS5() *S5API {
}
func (s S5API) Initialize(portal interfaces.Portal, protocol interfaces.Protocol) error {
s5protocol := protocol.(*protocols.S5Protocol)
registerProtocolSubdomain(portal, s5protocol.Node().Services().HTTP().GetHttpRouter(), "s5")
s5http := s5.NewHttpHandler(portal)
registerProtocolSubdomain(portal, s5protocol.Node().Services().HTTP().GetHttpRouter(getRoutes(s5http)), "s5")
return nil
}
func getRoutes(h *s5.HttpHandler) map[string]jape.Handler {
return map[string]jape.Handler{
"POST /s5/upload": h.SmallFileUpload,
"GET /account/register": h.AccountRegisterChallenge,
"POST /account/register": h.AccountRegister,
"GET /account/login": h.AccountLoginChallenge,
"POST /account/login": h.AccountLogin,
}
}

View File

@ -7,7 +7,6 @@ import (
"encoding/hex"
"errors"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
s5interface "git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/portal/db/models"
"git.lumeweb.com/LumeWeb/portal/interfaces"
@ -19,10 +18,6 @@ import (
"strings"
)
var (
_ s5interface.HTTPHandler = (*HttpHandlerImpl)(nil)
)
const (
errMultiformParse = "Error parsing multipart form"
errRetrievingFile = "Error retrieving the file"
@ -37,15 +32,15 @@ var (
errAccountGenerateChallengeErr = errors.New(errAccountGenerateChallenge)
)
type HttpHandlerImpl struct {
type HttpHandler struct {
portal interfaces.Portal
}
func NewHttpHandler(portal interfaces.Portal) *HttpHandlerImpl {
return &HttpHandlerImpl{portal: portal}
func NewHttpHandler(portal interfaces.Portal) *HttpHandler {
return &HttpHandler{portal: portal}
}
func (h *HttpHandlerImpl) SmallFileUpload(jc *jape.Context) {
func (h *HttpHandler) SmallFileUpload(jc jape.Context) {
var rs io.ReadSeeker
var bufferSize int64
@ -159,7 +154,7 @@ func (h *HttpHandlerImpl) SmallFileUpload(jc *jape.Context) {
jc.Encode(map[string]string{"hash": cidStr})
}
func (h *HttpHandlerImpl) AccountRegisterChallenge(jc *jape.Context) {
func (h *HttpHandler) AccountRegisterChallenge(jc jape.Context) {
var pubkey string
if jc.DecodeForm("pubKey", &pubkey) != nil {
return
@ -201,17 +196,17 @@ func (h *HttpHandlerImpl) AccountRegisterChallenge(jc *jape.Context) {
jc.Encode(map[string]string{"challenge": base64.RawURLEncoding.EncodeToString(challenge)})
}
func (h *HttpHandlerImpl) AccountRegister(context *jape.Context) {
func (h *HttpHandler) AccountRegister(context jape.Context) {
//TODO implement me
panic("implement me")
}
func (h *HttpHandlerImpl) AccountLoginChallenge(context *jape.Context) {
func (h *HttpHandler) AccountLoginChallenge(context jape.Context) {
//TODO implement me
panic("implement me")
}
func (h *HttpHandlerImpl) AccountLogin(context *jape.Context) {
func (h *HttpHandler) AccountLogin(context jape.Context) {
//TODO implement me
panic("implement me")
}

View File

@ -8,7 +8,6 @@ import (
s5interfaces "git.lumeweb.com/LumeWeb/libs5-go/interfaces"
s5node "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/protocols/s5"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
)
@ -83,7 +82,6 @@ func (s *S5Protocol) Initialize(portal interfaces.Portal) error {
cfg.DB = db
s.node = s5node.NewNode(cfg)
s.node.Services().HTTP().SetHttpHandler(s5.NewHttpHandler(s.portal))
return nil
}