forked from dragonflyoss/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dfdaemon add object storage rest api (dragonflyoss#1390)
* feat: dfdaemon add object storage rest api Signed-off-by: Gaius <[email protected]>
- Loading branch information
Showing
12 changed files
with
268 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ run: | |
|
||
linters-settings: | ||
gocyclo: | ||
min-complexity: 44 | ||
min-complexity: 50 | ||
gci: | ||
sections: | ||
- standard | ||
|
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
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
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,116 @@ | ||
/* | ||
* Copyright 2022 The Dragonfly Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package objectstorage | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
"d7y.io/dragonfly/v2/client/config" | ||
"d7y.io/dragonfly/v2/client/daemon/peer" | ||
"d7y.io/dragonfly/v2/client/daemon/storage" | ||
) | ||
|
||
// ObjectStorage is the interface used for object storage server. | ||
type ObjectStorage interface { | ||
// Started object storage server. | ||
Serve(lis net.Listener) error | ||
|
||
// Stop object storage server. | ||
Stop() error | ||
} | ||
|
||
// objectStorage provides object storage function. | ||
type objectStorage struct { | ||
*http.Server | ||
dynconfig config.Dynconfig | ||
peeTaskManager peer.TaskManager | ||
storageManager storage.Manager | ||
} | ||
|
||
// New returns a new ObjectStorage instence. | ||
func New(dynconfig config.Dynconfig, peerTaskManager peer.TaskManager, storageManager storage.Manager) ObjectStorage { | ||
o := &objectStorage{ | ||
dynconfig: dynconfig, | ||
peeTaskManager: peerTaskManager, | ||
storageManager: storageManager, | ||
} | ||
|
||
router := o.initRouter() | ||
o.Server = &http.Server{ | ||
Handler: router, | ||
} | ||
|
||
return o | ||
} | ||
|
||
// Started object storage server. | ||
func (o *objectStorage) Serve(lis net.Listener) error { | ||
return o.Server.Serve(lis) | ||
} | ||
|
||
// Stop object storage server. | ||
func (o *objectStorage) Stop() error { | ||
return o.Server.Shutdown(context.Background()) | ||
} | ||
|
||
// Initialize router of gin. | ||
func (o *objectStorage) initRouter() *gin.Engine { | ||
r := gin.Default() | ||
|
||
// Health Check. | ||
r.GET("/healthy", o.getHealth) | ||
|
||
// Buckets | ||
b := r.Group("/buckets") | ||
b.GET(":id/objects/*object_key", o.getObject) | ||
b.POST(":id/objects", o.createObject) | ||
|
||
return r | ||
} | ||
|
||
// getHealth uses to check server health. | ||
func (o *objectStorage) getHealth(ctx *gin.Context) { | ||
ctx.JSON(http.StatusOK, http.StatusText(http.StatusOK)) | ||
} | ||
|
||
// getObject uses to download object data. | ||
func (o *objectStorage) getObject(ctx *gin.Context) { | ||
var params GetObjectParams | ||
if err := ctx.ShouldBindUri(¶ms); err != nil { | ||
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()}) | ||
return | ||
} | ||
} | ||
|
||
// createObject uses to upload object data. | ||
func (o *objectStorage) createObject(ctx *gin.Context) { | ||
var params CreateObjectParams | ||
if err := ctx.ShouldBindUri(¶ms); err != nil { | ||
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()}) | ||
return | ||
} | ||
|
||
var form CreateObjectRequset | ||
if err := ctx.ShouldBind(&form); err != nil { | ||
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()}) | ||
return | ||
} | ||
} |
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,33 @@ | ||
/* | ||
* Copyright 2022 The Dragonfly Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package objectstorage | ||
|
||
import "mime/multipart" | ||
|
||
type GetObjectParams struct { | ||
ID string `uri:"id" binding:"required"` | ||
ObjectKey string `uri:"object_key" binding:"required"` | ||
} | ||
|
||
type CreateObjectParams struct { | ||
ID string `uri:"id" binding:"required"` | ||
} | ||
|
||
type CreateObjectRequset struct { | ||
Key string `form:"key" binding:"required"` | ||
File *multipart.FileHeader `form:"file" binding:"required"` | ||
} |
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
Oops, something went wrong.