Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boxo wip #50

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions api/debug_profiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package api

import (
httpprof "net/http/pprof"
"runtime/pprof"
"time"

"github.com/application-research/edge-ur/core"
"github.com/labstack/echo/v4"
)

// ConfigureDebugRouter It configures the router to handle requests for node profiling
func ConfigureDebugProfileRouter(profilerGroup *echo.Group, node *core.LightNode) {

profilerGroup.GET("/pprof/:prof", func(c echo.Context) error {
httpprof.Handler(c.Param("prof")).ServeHTTP(c.Response().Writer, c.Request())
return nil
})

profilerGroup.GET("/cpuprofile", func(c echo.Context) error {
if err := pprof.StartCPUProfile(c.Response()); err != nil {
return err
}

defer pprof.StopCPUProfile()

select {
case <-c.Request().Context().Done():
return c.Request().Context().Err()
case <-time.After(time.Second * 30):
}
return nil
})
}
22 changes: 13 additions & 9 deletions api/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"context"
"errors"
"fmt"
"github.com/ipfs/boxo/blockstore"
"github.com/ipfs/boxo/ipld/unixfs"
"gorm.io/gorm"
"html/template"
"io"
"net/http"
Expand All @@ -17,19 +14,21 @@ import (
"strings"
"time"

"gorm.io/gorm"

"github.com/application-research/edge-ur/core"
"github.com/application-research/whypfs-core"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
"github.com/labstack/echo/v4"

"github.com/gabriel-vasile/mimetype"

blockstore "github.com/ipfs/boxo/blockstore"
uio "github.com/ipfs/boxo/ipld/unixfs/io"
"github.com/ipfs/boxo/path/resolver"
mdagipld "github.com/ipfs/go-ipld-format"
"golang.org/x/xerrors"
)

var (
Expand Down Expand Up @@ -165,15 +164,17 @@ func (gw *GatewayHandler) serveUnixfsDir(ctx context.Context, n mdagipld.Node, w
}
http.ServeContent(w, req, "index.html", time.Time{}, dr)
return nil
case errors.Is(err, os.ErrNotExist):
default:
return err
case xerrors.Is(err, os.ErrNotExist):

}

fmt.Fprintf(w, "<html><body><ul>")

requestURI, err := url.ParseRequestURI(req.RequestURI)
if err != nil {
return err
}

if err := dir.ForEachLink(ctx, func(lnk *mdagipld.Link) error {
href := gopath.Join(requestURI.Path, lnk.Name)
Expand Down Expand Up @@ -358,10 +359,10 @@ func ServeDir(ctx context.Context, n mdagipld.Node, w http.ResponseWriter, req *

http.ServeContent(w, req, "index.html", time.Time{}, dr)
return nil

case errors.Is(err, os.ErrNotExist):
default:
return err
case xerrors.Is(err, os.ErrNotExist):

}

templates, err := template.ParseFiles("templates/dir.html")
Expand All @@ -373,6 +374,9 @@ func ServeDir(ctx context.Context, n mdagipld.Node, w http.ResponseWriter, req *
templates.Lookup("dir.html")

requestURI, err := url.ParseRequestURI(req.RequestURI)
if err != nil {
return err
}

if err := dir.ForEachLink(ctx, func(lnk *mdagipld.Link) error {
href := gopath.Join(requestURI.Path, lnk.Name)
Expand Down
16 changes: 9 additions & 7 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -13,7 +14,6 @@ import (
logging "github.com/ipfs/go-log/v2"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"golang.org/x/xerrors"
)

var (
Expand Down Expand Up @@ -47,7 +47,6 @@ type AuthResponse struct {

// RouterConfig configures the API node
func InitializeEchoRouterConfig(ln *core.LightNode) {
// Echo instance
// Echo instance
e := echo.New()
e.File("/", "templates/index.html")
Expand All @@ -67,6 +66,9 @@ func InitializeEchoRouterConfig(ln *core.LightNode) {
ConfigureHealthCheckRouter(defaultOpenRoute, ln)
ConfigureNodeInfoRouter(defaultOpenRoute, ln)

debugGroup := e.Group("/debug")
ConfigureDebugProfileRouter(debugGroup, ln)

apiGroup := e.Group("/api/v1")
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
Expand Down Expand Up @@ -111,7 +113,7 @@ func InitializeEchoRouterConfig(ln *core.LightNode) {
})
}

if authResp.Result.Validated == false {
if !authResp.Result.Validated {
return c.JSON(http.StatusUnauthorized, HttpErrorResponse{
Error: HttpError{
Code: http.StatusUnauthorized,
Expand All @@ -120,7 +122,7 @@ func InitializeEchoRouterConfig(ln *core.LightNode) {
},
})
}
if authResp.Result.Validated == true {
if !authResp.Result.Validated {
return next(c)
}
return next(c)
Expand Down Expand Up @@ -159,7 +161,7 @@ func GetAuthResponse(resp *http.Response) (AuthResponse, error) {

func ErrorHandler(err error, c echo.Context) {
var httpRespErr *HttpError
if xerrors.As(err, &httpRespErr) {
if errors.As(err, &httpRespErr) {
log.Errorf("handler error: %s", err)
if err := c.JSON(httpRespErr.Code, HttpErrorResponse{Error: *httpRespErr}); err != nil {
log.Errorf("handler error: %s", err)
Expand All @@ -169,7 +171,7 @@ func ErrorHandler(err error, c echo.Context) {
}

var echoErr *echo.HTTPError
if xerrors.As(err, &echoErr) {
if errors.As(err, &echoErr) {
if err := c.JSON(echoErr.Code, HttpErrorResponse{
Error: HttpError{
Code: echoErr.Code,
Expand Down Expand Up @@ -201,7 +203,7 @@ func LoopForever() {
fmt.Printf("Entering infinite loop\n")

signal.Notify(OsSignal, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1)
_ = <-OsSignal
<-OsSignal

fmt.Printf("Exiting infinite loop received OsSignal\n")
}
4 changes: 0 additions & 4 deletions api/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ type Stats struct {

func ConfigureStatsRouter(e *echo.Group, node *core.LightNode) {
e.GET("/stats", func(c echo.Context) error {

var stats Stats
node.DB.Raw("select count(*) as total_content_count, sum(size) as total_size from contents").Scan(&stats)

return c.JSON(200, stats)
return nil
})

}
35 changes: 16 additions & 19 deletions api/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"bytes"
"context"
"fmt"
"strings"
"time"

"github.com/application-research/edge-ur/jobs"
"github.com/application-research/edge-ur/utils"
"github.com/google/uuid"
"github.com/ipfs/boxo/ipld/car"
"github.com/ipfs/go-cid"
"github.com/labstack/echo/v4"
"strings"
"time"

"github.com/application-research/edge-ur/core"
"github.com/labstack/echo/v4"
)

type CidRequest struct {
Expand Down Expand Up @@ -98,6 +99,9 @@ func handleUploadToCarBucketAndMiners(node *core.LightNode, DeltaUploadApi strin
return err
}
src, err := file.Open()
if err != nil {
return err
}
srcR, err := file.Open()
if err != nil {
return err
Expand Down Expand Up @@ -148,10 +152,7 @@ func handleUploadToCarBucketAndMiners(node *core.LightNode, DeltaUploadApi strin
Miner: miner,
CarBucket: bucket.ID,
MakeDeal: func() bool {
if makeDeal == "true" {
return true
}
return false
return makeDeal == "true"
}(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand Down Expand Up @@ -188,6 +189,8 @@ func handleUploadToCarBucketAndMiners(node *core.LightNode, DeltaUploadApi strin
return nil
}
}

// TODO: remove this if not used
func handlePinAddToNodeToMiners(node *core.LightNode, DeltaUploadApi string) func(c echo.Context) error {
return func(c echo.Context) error {
authorizationString := c.Request().Header.Get("Authorization")
Expand All @@ -209,6 +212,9 @@ func handlePinAddToNodeToMiners(node *core.LightNode, DeltaUploadApi string) fun
return err
}
src, err := file.Open()
if err != nil {
return err
}
srcR, err := file.Open()
if err != nil {
return err
Expand All @@ -234,10 +240,7 @@ func handlePinAddToNodeToMiners(node *core.LightNode, DeltaUploadApi string) fun
Status: utils.STATUS_PINNED,
Miner: miner,
MakeDeal: func() bool {
if makeDeal == "true" {
return true
}
return false
return makeDeal == "true"
}(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand Down Expand Up @@ -338,10 +341,7 @@ func handleFetchPinToNodeToMiners(node *core.LightNode, DeltaUploadApi string) f
Status: "fetching",
Miner: miner,
MakeDeal: func() bool {
if makeDeal == "true" {
return true
}
return false
return makeDeal == "true"
}(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Expand Down Expand Up @@ -429,10 +429,7 @@ func handlePinAddCarToNodeToMiners(node *core.LightNode, DeltaUploadApi string)
RequestingApiKey: authParts[1],
Status: utils.STATUS_PINNED,
MakeDeal: func() bool {
if makeDeal == "true" {
return true
}
return false
return makeDeal == "true"
}(),
Miner: miner,
CreatedAt: time.Now(),
Expand Down
7 changes: 4 additions & 3 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package cmd
import (
"context"
"fmt"
"runtime"
"strconv"
"time"

"github.com/application-research/edge-ur/api"
"github.com/application-research/edge-ur/config"
"github.com/application-research/edge-ur/core"
"github.com/application-research/edge-ur/jobs"
"github.com/application-research/edge-ur/utils"
"github.com/urfave/cli/v2"
"runtime"
"strconv"
"time"
)

func DaemonCmd(cfg *config.DeltaConfig) []*cli.Command {
Expand Down
Loading