Skip to content

Commit

Permalink
style(gateway): polish gateway code style (#49)
Browse files Browse the repository at this point in the history
Co-authored-by: will-2012-nr <will.nodereal@gmail.com>
  • Loading branch information
will-2012 and xibao-nr authored Jan 13, 2023
1 parent 45ddc78 commit 982a91a
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 230 deletions.
33 changes: 33 additions & 0 deletions model/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,36 @@ const (
// server and client max send or recv msg size
MaxCallMsgSize = 40 * 1024 * 1024
)

// Gateway
const (
// path
AdminPath = "/greenfield/admin/v1/"
GetApprovalSubPath = "get-approval"

// query key
TransactionQuery = "transaction"
PutObjectV2Query = "putobjectv2"
ActionQuery = "action"

// bfs header key
BFSRequestIDHeader = "X-Bfs-Request-ID"
BFSContentLengthHeader = "X-Bfs-Content-Length"
BFSContentTypeHeader = "X-Bfs-Content-Type"
BFSChecksumHeader = "X-Bfs-Checksum"
BFSIsPrivateHeader = "X-Bfs-Is-Private"
BFSTransactionHashHeader = "X-Bfs-Txn-Hash"
BFSResourceHeader = "X-Bfs-Resource"
BFSPreSignatureHeader = "X-Bfs-Pre-Signature"
// BFSRedundancyTypeHeader can be EC or Replica, EC is default
BFSRedundancyTypeHeader = "X-Bfs-Redundancy-Type"

// http header key
ContentTypeHeader = "Content-Type"
ETagHeader = "ETag"
ContentLengthHeader = "Content-Length"

// header value
ContentTypeXMLHeaderValue = "application/xml"
ReplicaRedundancyTypeHeaderValue = "Replica"
)
27 changes: 14 additions & 13 deletions service/gateway/admin_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/hex"
"net/http"

"github.com/bnb-chain/inscription-storage-provider/model"
"github.com/bnb-chain/inscription-storage-provider/util/log"
)

Expand All @@ -12,42 +13,42 @@ func (g *Gateway) getAuthenticationHandler(w http.ResponseWriter, r *http.Reques
var (
err error
errorDescription *errorDescription
reqCtx *requestContext
opt *getAuthenticationOption
requestContext *requestContext
option *getAuthenticationOption
)

defer func() {
statusCode := 200
if errorDescription != nil {
statusCode = errorDescription.statusCode
_ = errorDescription.errorResponse(w, reqCtx)
_ = errorDescription.errorResponse(w, requestContext)
}
if statusCode == 200 {
log.Infof("action(%v) statusCode(%v) %v", "getAuthentication", statusCode, generateRequestDetail(reqCtx))
log.Infof("action(%v) statusCode(%v) %v", "getAuthentication", statusCode, generateRequestDetail(requestContext))
} else {
log.Warnf("action(%v) statusCode(%v) %v", "getAuthentication", statusCode, generateRequestDetail(reqCtx))
log.Warnf("action(%v) statusCode(%v) %v", "getAuthentication", statusCode, generateRequestDetail(requestContext))
}
}()

reqCtx = newRequestContext(r)
if reqCtx.bucket == "" {
requestContext = newRequestContext(r)
if requestContext.bucketName == "" {
errorDescription = InvalidBucketName
return
}

if err := reqCtx.verifySign(); err != nil {
if err := requestContext.verifySign(); err != nil {
errorDescription = SignatureDoesNotMatch
return
}

opt = &getAuthenticationOption{
reqCtx: reqCtx,
option = &getAuthenticationOption{
requestContext: requestContext,
}
info, err := g.uploadProcessor.getAuthentication(opt)
info, err := g.uploadProcessor.getAuthentication(option)
if err != nil {
errorDescription = InternalError
return
}
w.Header().Set(BFSRequestIDHeader, reqCtx.requestID)
w.Header().Set(BFSPreSignatureHeader, hex.EncodeToString(info.preSignature))
w.Header().Set(model.BFSRequestIDHeader, requestContext.requestID)
w.Header().Set(model.BFSPreSignatureHeader, hex.EncodeToString(info.preSignature))
}
20 changes: 10 additions & 10 deletions service/gateway/bucket_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@ func (g *Gateway) createBucketHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
errorDescription *errorDescription
reqCtx *requestContext
requestContext *requestContext
)

defer func() {
statusCode := 200
if errorDescription != nil {
statusCode = errorDescription.statusCode
_ = errorDescription.errorResponse(w, reqCtx)
_ = errorDescription.errorResponse(w, requestContext)
}
if statusCode == 200 {
log.Infof("action(%v) statusCode(%v) %v", "createBucket", statusCode, generateRequestDetail(reqCtx))
log.Infof("action(%v) statusCode(%v) %v", "createBucket", statusCode, generateRequestDetail(requestContext))
} else {
log.Warnf("action(%v) statusCode(%v) %v", "createBucket", statusCode, generateRequestDetail(reqCtx))
log.Warnf("action(%v) statusCode(%v) %v", "createBucket", statusCode, generateRequestDetail(requestContext))
}
}()

reqCtx = newRequestContext(r)
if reqCtx.bucket == "" {
requestContext = newRequestContext(r)
if requestContext.bucketName == "" {
errorDescription = InvalidBucketName
return
}
if err := reqCtx.verifySign(); err != nil {
if err := requestContext.verifySign(); err != nil {
errorDescription = SignatureDoesNotMatch
return
}

var opt = &createBucketOption{
reqCtx: reqCtx,
option := &createBucketOption{
requestContext: requestContext,
}
err = g.chain.createBucket(reqCtx.bucket, opt)
err = g.chain.createBucket(requestContext.bucketName, option)
if err != nil {
if err == errors.ErrDuplicateBucket {
errorDescription = BucketAlreadyExists
Expand Down
9 changes: 7 additions & 2 deletions service/gateway/chain_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// createBucketOption is the createBucket option.
type createBucketOption struct {
reqCtx *requestContext
requestContext *requestContext
}

// chainClientInterface define some interfaces to maintain metadata in the blockchain.
Expand Down Expand Up @@ -57,13 +57,18 @@ type chainClientConfig struct {
DebugDir string
}

var defaultChainClientConfig = &chainClientConfig{
Mode: "DebugMode",
DebugDir: "./debug",
}

// chainClient is a wrapper of maintaining metadata in the blockchain.
// todo: impl of call UpdateChainService, forward msg to blockchain.
type chainClient struct {
impl chainClientInterface
}

func newChainClient(c chainClientConfig) (*chainClient, error) {
func newChainClient(c *chainClientConfig) (*chainClient, error) {
switch {
case c.Mode == "DebugMode":
if c.DebugDir == "" {
Expand Down
32 changes: 0 additions & 32 deletions service/gateway/const.go

This file was deleted.

32 changes: 19 additions & 13 deletions service/gateway/download_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

// getObjectOption is the getObject Option.
type getObjectOption struct {
reqCtx *requestContext
offset uint64
length uint64
requestContext *requestContext
offset uint64
length uint64
}

// downloaderClientInterface define interface to download object.
Expand All @@ -31,10 +31,10 @@ type debugDownloaderImpl struct {
}

// getObject is used to get object data from local directory file for debugging.
func (ddl *debugDownloaderImpl) getObject(name string, writer io.Writer, option *getObjectOption) error {
func (ddl *debugDownloaderImpl) getObject(objectName string, writer io.Writer, option *getObjectOption) error {
var (
bucketDir = ddl.localDir + "/" + option.reqCtx.bucket
objectDataFile = bucketDir + "/" + name + ".data"
bucketDir = ddl.localDir + "/" + option.requestContext.bucketName
objectDataFile = bucketDir + "/" + objectName + ".data"
buf = make([]byte, 65536)
readN, writeN int
size uint64
Expand Down Expand Up @@ -93,13 +93,19 @@ type downloadProcessorConfig struct {
Address string
}

var defaultDownloadProcessorConfig = &downloadProcessorConfig{
Mode: "DebugMode",
DebugDir: "./debug",
Address: "127.0.0.1:5523",
}

// downloaderClient is a wrapper of download object.
type downloadProcessor struct {
impl downloaderClientInterface
}

// newDownloaderClient return a downloader wrapper.
func newDownloadProcessor(c downloadProcessorConfig) (*downloadProcessor, error) {
func newDownloadProcessor(c *downloadProcessorConfig) (*downloadProcessor, error) {
switch {
case c.Mode == "DebugMode":
return &downloadProcessor{impl: &debugDownloaderImpl{localDir: c.DebugDir}}, nil
Expand All @@ -115,8 +121,8 @@ func newDownloadProcessor(c downloadProcessorConfig) (*downloadProcessor, error)
}

// getObject get object from downloader.
func (dc *downloadProcessor) getObject(name string, writer io.Writer, option *getObjectOption) error {
return dc.impl.getObject(name, writer, option)
func (dc *downloadProcessor) getObject(objectName string, writer io.Writer, option *getObjectOption) error {
return dc.impl.getObject(objectName, writer, option)
}

// grpcDownloaderImpl is an implement of call grpc downloader service.
Expand All @@ -125,17 +131,17 @@ type grpcDownloaderImpl struct {
}

// getObject get object from downloader.
func (d *grpcDownloaderImpl) getObject(name string, writer io.Writer, option *getObjectOption) error {
func (d *grpcDownloaderImpl) getObject(objectName string, writer io.Writer, option *getObjectOption) error {
var (
err error
readN, writeN int
size int
)

req := &service.DownloaderServiceDownloaderObjectRequest{
TraceId: option.reqCtx.requestID,
BucketName: option.reqCtx.bucket,
ObjectName: option.reqCtx.object,
TraceId: option.requestContext.requestID,
BucketName: option.requestContext.bucketName,
ObjectName: objectName,
}
ctx := log.Context(context.Background(), req)
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion service/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewGatewayService(cfg *GatewayConfig) (*Gateway, error) {
config: cfg,
name: ServiceNameGateway,
}
if g.uploadProcessor, err = newUploadProcessor(&g.config.UploaderConfig); err != nil {
if g.uploadProcessor, err = newUploadProcessor(g.config.UploaderConfig); err != nil {
log.Warnw("failed to create uploader", "err", err)
return nil, err
}
Expand Down
27 changes: 8 additions & 19 deletions service/gateway/gateway_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,15 @@ package gateway
type GatewayConfig struct {
Address string
Domain string
UploaderConfig uploadProcessorConfig
ChainConfig chainClientConfig
DownloaderConfig downloadProcessorConfig
UploaderConfig *uploadProcessorConfig
ChainConfig *chainClientConfig
DownloaderConfig *downloadProcessorConfig
}

var DefaultGatewayConfig = &GatewayConfig{
Address: "127.0.0.1:5310",
Domain: "bfs.nodereal.com",
UploaderConfig: struct {
Mode string
DebugDir string
Address string
}{Mode: "DebugMode", DebugDir: "./debug", Address: ""},
ChainConfig: struct {
Mode string
DebugDir string
}{Mode: "DebugMode", DebugDir: "./debug"},
DownloaderConfig: struct {
Mode string
DebugDir string
Address string
}{Mode: "DebugMode", DebugDir: "./debug", Address: "127.0.0.1:5523"},
Address: "127.0.0.1:5310",
Domain: "bfs.nodereal.com",
UploaderConfig: defaultUploadProcessorConfig,
ChainConfig: defaultChainClientConfig,
DownloaderConfig: defaultDownloadProcessorConfig,
}
Loading

0 comments on commit 982a91a

Please sign in to comment.