2024-01-10 14:19:21 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/build"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"go.sia.tech/jape"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"net/url"
|
|
|
|
"nhooyr.io/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ interfaces.HTTPService = (*HTTPImpl)(nil)
|
|
|
|
|
|
|
|
type HTTPImpl struct {
|
2024-01-15 01:53:44 +00:00
|
|
|
node interfaces.Node
|
|
|
|
handler interfaces.HTTPHandler
|
2024-01-10 14:19:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 03:06:18 +00:00
|
|
|
func (h *HTTPImpl) SetHttpHandler(handler interfaces.HTTPHandler) {
|
|
|
|
h.handler = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTP(node interfaces.Node) interfaces.HTTPService {
|
2024-01-15 01:53:44 +00:00
|
|
|
return &HTTPImpl{
|
2024-01-15 03:06:18 +00:00
|
|
|
node: node,
|
2024-01-15 01:53:44 +00:00
|
|
|
}
|
2024-01-10 14:19:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 03:06:57 +00:00
|
|
|
func (h *HTTPImpl) GetHttpRouter() *httprouter.Router {
|
2024-01-10 14:19:21 +00:00
|
|
|
mux := jape.Mux(map[string]jape.Handler{
|
|
|
|
"GET /s5/version": h.versionHandler,
|
|
|
|
"GET /s5/p2p": h.p2pHandler,
|
2024-01-15 01:53:44 +00:00
|
|
|
"POST /s5/upload": h.uploadHandler,
|
2024-01-10 14:19:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPImpl) Node() interfaces.Node {
|
|
|
|
return h.node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPImpl) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPImpl) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPImpl) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTTPImpl) versionHandler(ctx jape.Context) {
|
|
|
|
_, _ = ctx.ResponseWriter.Write([]byte(build.Version))
|
|
|
|
}
|
|
|
|
func (h *HTTPImpl) p2pHandler(ctx jape.Context) {
|
|
|
|
c, err := websocket.Accept(ctx.ResponseWriter, ctx.Request, nil)
|
|
|
|
if err != nil {
|
|
|
|
h.node.Logger().Error("error accepting websocket connection", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peer, err := net.CreateTransportPeer("wss", &net.TransportPeerConfig{
|
|
|
|
Socket: c,
|
|
|
|
Uris: []*url.URL{},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.node.Logger().Error("error creating transport peer", zap.Error(err))
|
|
|
|
err := c.Close(websocket.StatusInternalError, "the sky is falling")
|
|
|
|
if err != nil {
|
|
|
|
h.node.Logger().Error("error closing websocket connection", zap.Error(err))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-01-10 16:21:07 +00:00
|
|
|
|
|
|
|
h.Node().ConnectionTracker().Add(1)
|
|
|
|
|
2024-01-10 14:19:21 +00:00
|
|
|
go func() {
|
2024-01-10 16:00:01 +00:00
|
|
|
err := h.node.Services().P2P().OnNewPeer(peer, false)
|
2024-01-10 14:19:21 +00:00
|
|
|
if err != nil {
|
|
|
|
h.node.Logger().Error("error handling new peer", zap.Error(err))
|
|
|
|
}
|
|
|
|
h.node.ConnectionTracker().Done()
|
|
|
|
}()
|
|
|
|
}
|
2024-01-15 01:53:44 +00:00
|
|
|
|
|
|
|
func (h *HTTPImpl) uploadHandler(context jape.Context) {
|
|
|
|
h.handler.SmallFileUpload(&context)
|
|
|
|
}
|