-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance indexer to provide REST API to download files (#41)
- Loading branch information
Showing
4 changed files
with
178 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package gateway | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
|
||
"github.com/0glabs/0g-storage-client/node" | ||
"github.com/0glabs/0g-storage-client/transfer" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
var clients []*node.ZgsClient | ||
var maxDownloadFileSize uint64 | ||
|
||
func downloadFile(c *gin.Context) { | ||
var input struct { | ||
Name string `form:"name" json:"name"` | ||
Root string `form:"root" json:"root"` | ||
TxSeq uint64 `form:"txSeq" json:"txSeq"` | ||
} | ||
|
||
if err := c.ShouldBind(&input); err != nil { | ||
c.JSON(http.StatusBadRequest, fmt.Sprintf("Failed to bind input parameters, %v", err.Error())) | ||
return | ||
} | ||
|
||
var fileInfo *node.FileInfo | ||
var err error | ||
|
||
if len(input.Root) == 0 { | ||
if fileInfo, err = clients[0].GetFileInfoByTxSeq(context.Background(), input.TxSeq); err != nil { | ||
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to get file info by tx seq, %v", err.Error())) | ||
return | ||
} | ||
} else { | ||
if fileInfo, err = clients[0].GetFileInfo(context.Background(), common.HexToHash(input.Root)); err != nil { | ||
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to get file info by root, %v", err.Error())) | ||
return | ||
} | ||
} | ||
|
||
if fileInfo == nil { | ||
c.JSON(http.StatusNotFound, "File not found") | ||
return | ||
} | ||
|
||
if !fileInfo.Finalized { | ||
c.JSON(http.StatusBadRequest, "File not finalized yet") | ||
return | ||
} | ||
|
||
if fileInfo.Tx.Size > maxDownloadFileSize { | ||
errMsg := fmt.Sprintf("Requested file size too large, actual = %v, max = %v", fileInfo.Tx.Size, maxDownloadFileSize) | ||
c.JSON(http.StatusBadRequest, errMsg) | ||
return | ||
} | ||
|
||
downloader, err := transfer.NewDownloader(clients) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to create downloader, %v", err.Error())) | ||
return | ||
} | ||
|
||
root := fileInfo.Tx.DataMerkleRoot.Hex() | ||
tmpfile := path.Join(os.TempDir(), fmt.Sprintf("zgs_indexer_download_%v", root)) | ||
defer os.Remove(tmpfile) | ||
|
||
if err = downloader.Download(context.Background(), fileInfo.Tx.DataMerkleRoot.Hex(), tmpfile, true); err != nil { | ||
c.JSON(http.StatusInternalServerError, fmt.Sprintf("Failed to download file, %v", err.Error())) | ||
return | ||
} | ||
|
||
if len(input.Name) == 0 { | ||
c.FileAttachment(tmpfile, root) | ||
} else { | ||
c.FileAttachment(tmpfile, input.Name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package gateway | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/0glabs/0g-storage-client/common/util" | ||
"github.com/0glabs/0g-storage-client/node" | ||
"github.com/gin-contrib/cors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type Config struct { | ||
Endpoint string | ||
|
||
Nodes []string // storage nodes to download files | ||
MaxDownloadFileSize uint64 | ||
|
||
RPCHandler http.Handler // enable to provide both RPC and REST API service | ||
} | ||
|
||
func MustServeWithRPC(config Config) { | ||
if len(config.Nodes) == 0 { | ||
logrus.Fatal("Nodes not specified to start HTTP server") | ||
} | ||
|
||
// init global variables | ||
clients = node.MustNewZgsClients(config.Nodes) | ||
maxDownloadFileSize = config.MaxDownloadFileSize | ||
|
||
// init router | ||
router := newRouter() | ||
if config.RPCHandler != nil { | ||
router.POST("/", gin.WrapH(config.RPCHandler)) | ||
} | ||
|
||
util.MustServe(config.Endpoint, router) | ||
} | ||
|
||
func newRouter() *gin.Engine { | ||
router := gin.New() | ||
|
||
// middlewares | ||
router.Use(gin.Recovery()) | ||
if logrus.IsLevelEnabled(logrus.DebugLevel) { | ||
router.Use(gin.Logger()) | ||
} | ||
router.Use(middlewareCors()) | ||
|
||
// handlers | ||
router.GET("/file", downloadFile) | ||
|
||
return router | ||
} | ||
|
||
func middlewareCors() gin.HandlerFunc { | ||
conf := cors.DefaultConfig() | ||
conf.AllowMethods = append(conf.AllowMethods, "OPTIONS") | ||
conf.AllowHeaders = append(conf.AllowHeaders, "*") | ||
conf.AllowAllOrigins = true | ||
|
||
return cors.New(conf) | ||
} |