Skip to content

Commit

Permalink
clean up, go docs, webocket
Browse files Browse the repository at this point in the history
  • Loading branch information
alvin-reyes committed Feb 28, 2023
1 parent cc76f80 commit 7b13079
Show file tree
Hide file tree
Showing 27 changed files with 798 additions and 183 deletions.
11 changes: 9 additions & 2 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type ImportWalletRequest struct {
PrivateKey string `json:"private_key"`
}

// It creates a new wallet and saves it to the database
// ConfigureAdminRouter It creates a new wallet and saves it to the database
// It configures the admin router
func ConfigureAdminRouter(e *echo.Group, node *core.DeltaNode) {

adminWallet := e.Group("/wallet")
adminStats := e.Group("/stats")
adminStats.GET("/miner/:minerId", handleAdminStatsMiner(node))
Expand All @@ -34,6 +34,8 @@ func ConfigureAdminRouter(e *echo.Group, node *core.DeltaNode) {
}

// It creates a new wallet address and saves it to the database
// `handleAdminCreateWallet` is a function that takes a `DeltaNode` and returns a function that takes an `echo.Context` and
// returns an `error`
func handleAdminCreateWallet(node *core.DeltaNode) func(c echo.Context) error {
return func(c echo.Context) error {
authorizationString := c.Request().Header.Get("Authorization")
Expand Down Expand Up @@ -80,6 +82,8 @@ func handleAdminCreateWallet(node *core.DeltaNode) func(c echo.Context) error {
}

// Creating a new wallet address and saving it to the database.
// `handleAdminRegisterWallet` is a function that takes a `DeltaNode` and returns a function that takes an `echo.Context`
// and returns an `error`
func handleAdminRegisterWallet(node *core.DeltaNode) func(c echo.Context) error {
return func(c echo.Context) error {
authorizationString := c.Request().Header.Get("Authorization")
Expand Down Expand Up @@ -132,6 +136,8 @@ func handleAdminRegisterWallet(node *core.DeltaNode) func(c echo.Context) error

// It takes the authorization header from the request, splits it into two parts, and then uses the second part to find all
// wallets owned by the user
// `handleAdminListWallets` is a function that takes a `DeltaNode` and returns a function that takes an `echo.Context` and
// returns an `error`
func handleAdminListWallets(node *core.DeltaNode) func(c echo.Context) error {
return func(c echo.Context) error {
authorizationString := c.Request().Header.Get("Authorization")
Expand All @@ -153,6 +159,7 @@ func handleAdminListWallets(node *core.DeltaNode) func(c echo.Context) error {
}

// A function that returns a function that returns an error.
// It returns a function that takes a context and returns an error
func handleAdminStatsMiner(node *core.DeltaNode) func(c echo.Context) error {
return func(c echo.Context) error {

Expand Down
49 changes: 33 additions & 16 deletions api/deal.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func ConfigureDealRouter(e *echo.Group, node *core.DeltaNode) {
dealStatus := dealMake.Group("/status")

dealMake.POST("/content", func(c echo.Context) error {
return handleContentAdd(c, node, *statsService)
return handleContentAdd(c, node)
})

dealMake.POST("/existing/content", func(c echo.Context) error {
Expand All @@ -84,16 +84,16 @@ func ConfigureDealRouter(e *echo.Group, node *core.DeltaNode) {
})

dealMake.POST("/piece-commitment", func(c echo.Context) error {
return handleCommPieceAdd(c, node, *statsService)
return handleCommPieceAdd(c, node)
})

dealMake.POST("/existing/piece-commitment", func(c echo.Context) error {
return handleCommPieceAdd(c, node, *statsService)
return handleCommPieceAdd(c, node)
})

// make piece-commitments
dealMake.POST("/piece-commitments", func(c echo.Context) error {
return handleCommPiecesAdd(c, node, *statsService)
return handleCommPiecesAdd(c, node)
})

dealPrepare.POST("/content", func(c echo.Context) error {
Expand Down Expand Up @@ -121,10 +121,10 @@ func ConfigureDealRouter(e *echo.Group, node *core.DeltaNode) {
})

dealStatus.POST("/content/:contentId", func(c echo.Context) error {
return handleContentStats(c, node, *statsService)
return handleContentStats(c, *statsService)
})
dealStatus.POST("/piece-commitment/:piece-commitmentId", func(c echo.Context) error {
return handleCommitmentPieceStats(c, node, *statsService)
return handleCommitmentPieceStats(c, *statsService)

})
}
Expand Down Expand Up @@ -530,12 +530,15 @@ func handleExistingContentAdd(c echo.Context, node *core.DeltaNode) error {

node.Dispatcher.AddJobAndDispatch(dispatchJobs, 1)

c.JSON(200, DealResponse{
err = c.JSON(200, DealResponse{
Status: "success",
Message: "File uploaded and pinned successfully",
ContentId: content.ID,
DealRequest: dealRequest,
})
if err != nil {
return err
}

return nil
}
Expand All @@ -550,7 +553,7 @@ func handleExistingContentAdd(c echo.Context, node *core.DeltaNode) error {
// @Success 200 {object} ContentMakeDealResponse
// @Failure 500 {object} ContentMakeDealResponse
// @Router /deal/content/{contentId} [post]
func handleContentAdd(c echo.Context, node *core.DeltaNode, stats core.StatsService) error {
func handleContentAdd(c echo.Context, node *core.DeltaNode) error {
var dealRequest DealRequest

// lets record this.
Expand All @@ -560,7 +563,10 @@ func handleContentAdd(c echo.Context, node *core.DeltaNode, stats core.StatsServ
meta := c.FormValue("metadata")

// validate the meta
json.Unmarshal([]byte(meta), &dealRequest)
err = json.Unmarshal([]byte(meta), &dealRequest)
if err != nil {
return err
}

err = ValidateMeta(dealRequest)
if err != nil {
Expand Down Expand Up @@ -739,12 +745,15 @@ func handleContentAdd(c echo.Context, node *core.DeltaNode, stats core.StatsServ

node.Dispatcher.AddJobAndDispatch(dispatchJobs, 1)

c.JSON(200, DealResponse{
err = c.JSON(200, DealResponse{
Status: "success",
Message: "File uploaded and pinned successfully",
ContentId: content.ID,
DealRequest: dealRequest,
})
if err != nil {
return err
}

return nil
}
Expand All @@ -755,7 +764,7 @@ func handleContentAdd(c echo.Context, node *core.DeltaNode, stats core.StatsServ
// @Tags deals
// @Accept json
// @Produce json
func handleCommPieceAdd(c echo.Context, node *core.DeltaNode, statsService core.StatsService) error {
func handleCommPieceAdd(c echo.Context, node *core.DeltaNode) error {
var dealRequest DealRequest

// lets record this.
Expand Down Expand Up @@ -933,12 +942,15 @@ func handleCommPieceAdd(c echo.Context, node *core.DeltaNode, statsService core.

node.Dispatcher.AddJobAndDispatch(dispatchJobs, 1)

c.JSON(200, DealResponse{
err = c.JSON(200, DealResponse{
Status: "success",
Message: "File uploaded and pinned successfully",
ContentId: content.ID,
DealRequest: dealRequest,
})
if err != nil {
return err
}
return nil
}

Expand All @@ -948,7 +960,7 @@ func handleCommPieceAdd(c echo.Context, node *core.DeltaNode, statsService core.
// @Tags CommP
// @Accept json
// @Produce json
func handleCommPiecesAdd(c echo.Context, node *core.DeltaNode, statsService core.StatsService) error {
func handleCommPiecesAdd(c echo.Context, node *core.DeltaNode) error {
var dealRequests []DealRequest

// lets record this.
Expand Down Expand Up @@ -1137,13 +1149,16 @@ func handleCommPiecesAdd(c echo.Context, node *core.DeltaNode, statsService core

}
node.Dispatcher.Start(len(dealRequests))
c.JSON(http.StatusOK, dealResponses)
err = c.JSON(http.StatusOK, dealResponses)
if err != nil {
return err
}

return nil
}

// It takes a contentId as a parameter, looks up the status of the content, and returns the status as JSON
func handleContentStats(c echo.Context, node *core.DeltaNode, statsService core.StatsService) error {
func handleContentStats(c echo.Context, statsService core.StatsService) error {
contentIdParam := c.Param("contentId")
contentId, err := strconv.Atoi(contentIdParam)
if err != nil {
Expand All @@ -1163,7 +1178,7 @@ func handleContentStats(c echo.Context, node *core.DeltaNode, statsService core.
}

// It takes a piece commitment ID, looks up the status of the piece commitment, and returns the status
func handleCommitmentPieceStats(c echo.Context, node *core.DeltaNode, statsService core.StatsService) error {
func handleCommitmentPieceStats(c echo.Context, statsService core.StatsService) error {
pieceCommitmentIdParam := c.Param("piece-commitmentId")
pieceCommitmentId, err := strconv.Atoi(pieceCommitmentIdParam)
if err != nil {
Expand Down Expand Up @@ -1206,10 +1221,12 @@ func ValidateMeta(dealRequest DealRequest) error {
return errors.New("miner is required")
}

// connection mode is required
if (DealRequest{} != dealRequest && (dealRequest.ConnectionMode != utils.CONNECTION_MODE_E2E && dealRequest.ConnectionMode != utils.CONNECTION_MODE_IMPORT)) {
return errors.New("connection mode can only be e2e or import")
}

// piece commitment is required
if (PieceCommitmentRequest{} != dealRequest.PieceCommitment && dealRequest.PieceCommitment.Piece == "") {
return errors.New("piece commitment is invalid, make sure you have the cid, piece_cid, size and padded_piece_size or unpadded_piece_size")
}
Expand Down
4 changes: 3 additions & 1 deletion api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// It configures the metrics router
package api

import (
Expand All @@ -7,7 +8,8 @@ import (
"net/http"
)

// Configuring the metrics router.
// ConfigMetricsRouter Configuring the metrics router.
// > ConfigMetricsRouter is a function that takes a pointer to an echo.Group and returns nothing
func ConfigMetricsRouter(e *echo.Group) {
// metrics
phandle := promhttp.Handler()
Expand Down
34 changes: 0 additions & 34 deletions api/miner.go

This file was deleted.

11 changes: 10 additions & 1 deletion api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func InitializeEchoRouterConfig(ln *core.DeltaNode, config config.DeltaConfig) {
authorizationString := c.Request().Header.Get("Authorization")
authParts := strings.Split(authorizationString, " ")

if len(authParts) != 2 {
return c.JSON(http.StatusUnauthorized, HttpErrorResponse{
Error: HttpError{
Code: http.StatusUnauthorized,
Reason: http.StatusText(http.StatusUnauthorized),
Details: "Invalid authorization header",
},
})
}

response, err := http.Post(
"https://auth.estuary.tech/check-api-key",
"application/json",
Expand Down Expand Up @@ -166,7 +176,6 @@ func InitializeEchoRouterConfig(ln *core.DeltaNode, config config.DeltaConfig) {
ConfigureDealRouter(apiGroup, ln)
ConfigureStatsCheckRouter(apiGroup, ln)
ConfigureRepairRouter(apiGroup, ln)
ConfigureMinerRouter(apiGroup, ln)

// open api
ConfigureNodeInfoRouter(openApiGroup, ln)
Expand Down
48 changes: 30 additions & 18 deletions api/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type StatsCheckResponse struct {
}

// ConfigureStatsCheckRouter Creating a new router and adding a route to it.
// It configures the router for the stats check API
func ConfigureStatsCheckRouter(e *echo.Group, node *core.DeltaNode) {

e.GET("/stats/miner/:minerId/content", func(c echo.Context) error {
Expand Down Expand Up @@ -70,7 +71,6 @@ func ConfigureStatsCheckRouter(e *echo.Group, node *core.DeltaNode) {
})

e.GET("/stats/deal-proposal/:id", func(c echo.Context) error {

return nil
})

Expand All @@ -82,25 +82,14 @@ func ConfigureStatsCheckRouter(e *echo.Group, node *core.DeltaNode) {
return handleGetContents(c, node)
})

e.GET("/stats/miner/:minerId", func(c echo.Context) error {
e.GET("/stats/miner/:minerId", handleMinerStats(node))

authorizationString := c.Request().Header.Get("Authorization")
authParts := strings.Split(authorizationString, " ")

var contents []model.Content
node.DB.Raw("select c.* from content_deals cd, contents c where cd.content = c.id and cd.miner = ? and c.requesting_api_key = ?", c.Param("minerId"), authParts[1]).Scan(&contents)

var contentMinerAssignment []model.ContentMiner
node.DB.Raw("select cma.* from content_miners cma, contents c where cma.content = c.id and cma.miner = ? and c.requesting_api_key = ?", c.Param("minerId"), authParts[1]).Scan(&contentMinerAssignment)
e.GET("/stats", handleStats(node))

return c.JSON(200, map[string]interface{}{
"content": contents,
"cmas": contentMinerAssignment,
})
return nil
})
}

e.GET("/stats", func(c echo.Context) error {
func handleStats(node *core.DeltaNode) func(c echo.Context) error {
return func(c echo.Context) error {

authorizationString := c.Request().Header.Get("Authorization")
authParts := strings.Split(authorizationString, " ")
Expand All @@ -121,8 +110,31 @@ func ConfigureStatsCheckRouter(e *echo.Group, node *core.DeltaNode) {
"deals": contentDeal,
"piece_commitments": pieceCommitments,
})
})
}
}

// `handleMinerStats` is a function that takes a `*core.DeltaNode` and returns a function that takes an `echo.Context` and
// returns an `error`
// `handleMinerStats` is a function that takes a `*core.DeltaNode` and returns a function that takes an `echo.Context` and
// returns an `error`
func handleMinerStats(node *core.DeltaNode) func(c echo.Context) error {
return func(c echo.Context) error {

authorizationString := c.Request().Header.Get("Authorization")
authParts := strings.Split(authorizationString, " ")

var contents []model.Content
node.DB.Raw("select c.* from content_deals cd, contents c where cd.content = c.id and cd.miner = ? and c.requesting_api_key = ?", c.Param("minerId"), authParts[1]).Scan(&contents)

var contentMinerAssignment []model.ContentMiner
node.DB.Raw("select cma.* from content_miners cma, contents c where cma.content = c.id and cma.miner = ? and c.requesting_api_key = ?", c.Param("minerId"), authParts[1]).Scan(&contentMinerAssignment)

return c.JSON(200, map[string]interface{}{
"content": contents,
"cmas": contentMinerAssignment,
})
return nil
}
}

// A function that takes in a commitment and a piece number and returns the piece of the commitment.
Expand Down
Loading

0 comments on commit 7b13079

Please sign in to comment.