-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: liuminjian <[email protected]>
- Loading branch information
liuminjian
committed
Nov 24, 2023
1 parent
580b26d
commit e17126e
Showing
80 changed files
with
829 additions
and
174 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* 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 main | ||
|
||
import ( | ||
"github.com/opencurve/curveadm/http" | ||
"github.com/opencurve/pigeon" | ||
) | ||
|
||
func main() { | ||
admServer := http.NewServer() | ||
pigeon.Serve(admServer) | ||
} |
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,71 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* 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 core | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/opencurve/curveadm/internal/errno" | ||
"github.com/opencurve/pigeon" | ||
) | ||
|
||
func Exit(r *pigeon.Request, err error) bool { | ||
var status int | ||
if err == nil { | ||
status = 200 | ||
r.SendJSON(pigeon.JSON{ | ||
"errorCode": "0", | ||
"errorMsg": "success", | ||
}) | ||
} else { | ||
code := err.(*errno.ErrorCode) | ||
if code.IsHttpErr() { | ||
status = code.HttpCode() | ||
} else { | ||
status = 503 | ||
} | ||
r.SendJSON(pigeon.JSON{ | ||
"errorCode": strconv.Itoa(code.GetCode()), | ||
"errorMsg": fmt.Sprintf("desc: %s; clue: %s", code.GetDescription(), code.GetClue()), | ||
}) | ||
} | ||
return r.Exit(status) | ||
} | ||
|
||
func Default(r *pigeon.Request) bool { | ||
r.Logger().Warn("unupport request uri", pigeon.Field("uri", r.Uri)) | ||
return Exit(r, errno.ERR_UNSUPPORT_REQUEST_URI) | ||
} | ||
|
||
func ExitSuccessWithData(r *pigeon.Request, data interface{}) bool { | ||
r.SendJSON(pigeon.JSON{ | ||
"data": data, | ||
"errorCode": "0", | ||
"errorMsg": "success", | ||
}) | ||
return r.Exit(200) | ||
} | ||
|
||
func ExitFailWithData(r *pigeon.Request, data interface{}, message string) bool { | ||
r.SendJSON(pigeon.JSON{ | ||
"errorCode": "503", | ||
"errorMsg": message, | ||
"data": data, | ||
}) | ||
return r.Exit(503) | ||
} |
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,81 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* 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 manager | ||
|
||
import ( | ||
"github.com/opencurve/pigeon" | ||
"mime/multipart" | ||
"net/http" | ||
) | ||
|
||
var METHOD_REQUEST map[string]Request | ||
|
||
type ( | ||
HandlerFunc func(r *pigeon.Request, ctx *Context) bool | ||
|
||
Context struct { | ||
Data interface{} | ||
} | ||
|
||
Request struct { | ||
httpMethod string | ||
method string | ||
vType interface{} | ||
handler HandlerFunc | ||
} | ||
) | ||
|
||
func init() { | ||
METHOD_REQUEST = map[string]Request{} | ||
for _, request := range requests { | ||
METHOD_REQUEST[request.method] = request | ||
} | ||
} | ||
|
||
type DeployClusterCmdRequest struct { | ||
Command string `json:"command" binding:"required"` | ||
} | ||
|
||
type DeployClusterUploadRequest struct { | ||
FilePath string `json:"filepath" form:"filepath" binding:"required"` | ||
File *multipart.FileHeader `form:"file" binding:"required"` | ||
} | ||
|
||
type DeployClusterDownloadRequest struct { | ||
FilePath string `json:"filepath" form:"filepath" binding:"required"` | ||
} | ||
|
||
var requests = []Request{ | ||
{ | ||
http.MethodPost, | ||
"cluster.deploy.cmd", | ||
DeployClusterCmdRequest{}, | ||
DeployClusterCmd, | ||
}, | ||
{ | ||
http.MethodPost, | ||
"cluster.deploy.upload", | ||
DeployClusterUploadRequest{}, | ||
DeployClusterUpload, | ||
}, | ||
{ | ||
http.MethodGet, | ||
"cluster.deploy.download", | ||
DeployClusterDownloadRequest{}, | ||
DeployClusterDownload, | ||
}, | ||
} |
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,50 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* 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 manager | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/mcuadros/go-defaults" | ||
"github.com/opencurve/curveadm/http/core" | ||
"github.com/opencurve/curveadm/internal/errno" | ||
"github.com/opencurve/pigeon" | ||
) | ||
|
||
func Entrypoint(r *pigeon.Request) bool { | ||
if r.Method != pigeon.HTTP_METHOD_GET && | ||
r.Method != pigeon.HTTP_METHOD_POST { | ||
return core.Exit(r, errno.ERR_UNSUPPORT_HTTP_METHOD) | ||
} | ||
|
||
request, ok := METHOD_REQUEST[r.Args["method"]] | ||
if !ok { | ||
return core.Exit(r, errno.ERR_UNSUPPORT_METHOD_ARGUMENT) | ||
} else if request.httpMethod != r.Method { | ||
return core.Exit(r, errno.ERR_HTTP_METHOD_MISMATCHED) | ||
} | ||
|
||
vType := reflect.TypeOf(request.vType) | ||
data := reflect.New(vType).Interface() | ||
if err := r.BindBody(data); err != nil { | ||
r.Logger().Error("bad request form param", | ||
pigeon.Field("error", err)) | ||
return core.Exit(r, errno.ERR_BAD_REQUEST_FORM_PARAM) | ||
} | ||
defaults.SetDefaults(data) | ||
return request.handler(r, &Context{data}) | ||
} |
Oops, something went wrong.