working commit
This commit is contained in:
+8
-3
@@ -8,7 +8,12 @@ import (
|
||||
func (hand *Handler) FileExists(rctx *router.Context) {
|
||||
hand.logg.Debugf("handle FileExists")
|
||||
|
||||
params := &operator.FileExistsParams{}
|
||||
res, _ := hand.oper.FileExists(params)
|
||||
rctx.SetStatus(res.Code)
|
||||
filepath := rctx.PathMap["filepath"]
|
||||
params := &operator.FileExistsParams{
|
||||
Filepath: filepath,
|
||||
}
|
||||
hand.logg.Debugf("filepath: %s", filepath)
|
||||
|
||||
code, _, _ := hand.oper.FileExists(params)
|
||||
rctx.SetStatus(code)
|
||||
}
|
||||
|
||||
+51
-8
@@ -1,19 +1,62 @@
|
||||
package operator
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type FileExistsParams struct{}
|
||||
|
||||
type FileExistsResult struct {
|
||||
Code int
|
||||
// File exists
|
||||
type FileExistsParams struct {
|
||||
Filepath string
|
||||
Source string
|
||||
Dest string
|
||||
}
|
||||
type FileExistsResult struct{}
|
||||
|
||||
func (oper *Operator) FileExists(param *FileExistsParams) (*FileExistsResult, error) {
|
||||
func (oper *Operator) FileExists(param *FileExistsParams) (int, *FileExistsResult, error) {
|
||||
var err error
|
||||
res := &FileExistsResult{
|
||||
Code: http.StatusOK,
|
||||
res := &FileExistsResult{}
|
||||
code := http.StatusOK
|
||||
return code, res, err
|
||||
}
|
||||
return res, err
|
||||
|
||||
// Put file
|
||||
type PutFileParams struct {
|
||||
Filepath string
|
||||
Source string
|
||||
}
|
||||
type PutFileResult struct{}
|
||||
|
||||
func (oper *Operator) PutFile(param *PutFileParams) (int, *PutFileResult, error) {
|
||||
var err error
|
||||
res := &PutFileResult{}
|
||||
code := http.StatusOK
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
// Get file
|
||||
type GetFileParams struct {
|
||||
Filepath string
|
||||
Dest io.Writer
|
||||
}
|
||||
type GetFileResult struct{}
|
||||
|
||||
func (oper *Operator) GetFile(param *GetFileParams) (int, *GetFileResult, error) {
|
||||
var err error
|
||||
res := &GetFileResult{}
|
||||
code := http.StatusOK
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
// Delete file
|
||||
type DeleteFileParams struct {
|
||||
Filepath string
|
||||
}
|
||||
type DeleteFileResult struct{}
|
||||
|
||||
func (oper *Operator) DeleteFile(param *DeleteFileParams) (int, *DeleteFileResult, error) {
|
||||
var err error
|
||||
res := &DeleteFileResult{}
|
||||
code := http.StatusOK
|
||||
return code, res, err
|
||||
}
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ func pathCompiler(path string) (string, error) {
|
||||
}
|
||||
|
||||
const (
|
||||
defaultRegexp = `[a-zA-Z0-9_][\-\.a-zA-Z0-9_%%=:~]+`
|
||||
defaultRegexp = `[a-zA-Z0-9_][/\-\.a-zA-Z0-9_%%=:~]+`
|
||||
)
|
||||
|
||||
func convertRegexp(src []byte) []byte {
|
||||
|
||||
@@ -30,6 +30,10 @@ func (rout *Router) AddRoute(method, path string, handlerFunc HandlerFunc) {
|
||||
rout.routeHandler.AddRoute(method, path, handlerFunc)
|
||||
}
|
||||
|
||||
func (rout *Router) Head(path string, handlerFunc HandlerFunc) {
|
||||
rout.routeHandler.AddRoute("HEAD", path, handlerFunc)
|
||||
}
|
||||
|
||||
func (rout *Router) Get(path string, handlerFunc HandlerFunc) {
|
||||
rout.routeHandler.AddRoute("GET", path, handlerFunc)
|
||||
}
|
||||
@@ -38,6 +42,10 @@ func (rout *Router) Post(path string, handlerFunc HandlerFunc) {
|
||||
rout.routeHandler.AddRoute("POST", path, handlerFunc)
|
||||
}
|
||||
|
||||
func (rout *Router) Put(path string, handlerFunc HandlerFunc) {
|
||||
rout.routeHandler.AddRoute("PUT", path, handlerFunc)
|
||||
}
|
||||
|
||||
func (rout *Router) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
|
||||
rctx := NewContext(writer, req)
|
||||
rout.routeHandler.ServeHTTP(rctx)
|
||||
|
||||
+5
-7
@@ -4,13 +4,11 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
PathServiceHello = "/service/hello"
|
||||
)
|
||||
|
||||
var (
|
||||
pathRegexp = `{path:[a-zA-Z_][\-a-zA-Z0-9_\/\.%~]+}`
|
||||
fileOperPrefix = "/v3/file"
|
||||
filepathRegexp = `{path:[a-zA-Z_][\-a-zA-Z0-9_\/\.%~]+}`
|
||||
fileApiPrefix = `/v3/file`
|
||||
|
||||
PathFileExists = fmt.Sprintf("%s/%s", fileOperPrefix, pathRegexp)
|
||||
FileExistsPath = fmt.Sprintf("%s/%s", fileApiPrefix, filepathRegexp)
|
||||
|
||||
ServiceHelloPath = "/service/hello"
|
||||
)
|
||||
|
||||
@@ -43,8 +43,8 @@ func (svc *Service) Build() error {
|
||||
svc.logg.Infof("Service build ")
|
||||
|
||||
svc.rout = router.NewRouter()
|
||||
svc.rout.Get(PathServiceHello, svc.hand.SendHello)
|
||||
svc.rout.Get(PathFileExists, svc.hand.FileExists)
|
||||
svc.rout.Get(ServiceHelloPath, svc.hand.SendHello)
|
||||
svc.rout.Get(FileExistsPath, svc.hand.FileExists)
|
||||
|
||||
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
|
||||
svc.listen, err = net.Listen(protocol, listenAddress)
|
||||
|
||||
+45
-4
@@ -20,10 +20,40 @@ func (util *Util) AddFileCmds() {
|
||||
putFileCmd.Flags().StringVarP(&util.putFileParams.Dest, "dest", "d", "", "Desctination path")
|
||||
|
||||
subCmd.AddCommand(putFileCmd)
|
||||
util.rootCmd.AddCommand(subCmd)
|
||||
|
||||
var getFileCmd = &cobra.Command{
|
||||
Use: "put",
|
||||
Short: "Put file to storage",
|
||||
Run: util.PutFile,
|
||||
}
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Username, "username", "u", "", "Username")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Password, "password", "p", "", "Password")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Source, "source", "s", "", "Source path")
|
||||
getFileCmd.Flags().StringVarP(&util.getFileParams.Dest, "dest", "d", "", "Desctination path")
|
||||
|
||||
subCmd.AddCommand(getFileCmd)
|
||||
|
||||
util.rootCmd.AddCommand(subCmd)
|
||||
}
|
||||
|
||||
type FileUtil struct {
|
||||
fileExists FileExistsParams
|
||||
putFileParams PutFileParams
|
||||
getFileParams GetFileParams
|
||||
deleteFileParams DeleteFileParams
|
||||
}
|
||||
|
||||
// File exists
|
||||
type FileExistsParams struct {
|
||||
Filepath string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (util *Util) FileExists(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
// Put file
|
||||
type PutFileParams struct {
|
||||
Source string
|
||||
Dest string
|
||||
@@ -31,8 +61,10 @@ type PutFileParams struct {
|
||||
Password string
|
||||
}
|
||||
|
||||
func (util *Util) PutFile(cmd *cobra.Command, args []string) {}
|
||||
func (util *FileUtil) PutFile(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
// Get file
|
||||
type GetFileParams struct {
|
||||
Source string
|
||||
Dest string
|
||||
@@ -40,6 +72,15 @@ type GetFileParams struct {
|
||||
Password string
|
||||
}
|
||||
|
||||
func (util *Util) GetFile(cmd *cobra.Command, args []string) {}
|
||||
func (util *FileUtil) GetFile(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
func (util *Util) DeleteFile(cmd *cobra.Command, args []string) {}
|
||||
// Delete file
|
||||
type DeleteFileParams struct {
|
||||
Filepath string
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func (util *FileUtil) DeleteFile(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ func main() {
|
||||
}
|
||||
|
||||
type Util struct {
|
||||
rootCmd *cobra.Command
|
||||
putFileParams PutFileParams
|
||||
FileUtil
|
||||
rootCmd cobra.Command
|
||||
}
|
||||
|
||||
func NewUtil() *Util {
|
||||
@@ -31,7 +31,7 @@ func NewUtil() *Util {
|
||||
|
||||
func (util *Util) Build() error {
|
||||
var err error
|
||||
rootCmd := &cobra.Command{
|
||||
rootCmd := cobra.Command{
|
||||
Use: "cmd",
|
||||
Short: "A brief description the command",
|
||||
}
|
||||
|
||||
@@ -1,46 +1 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"mstore/app/router"
|
||||
"mstore/app/server"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGreeterHello(t *testing.T) {
|
||||
var err error
|
||||
srv, err := server.NewServer()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Configure()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Build()
|
||||
require.NoError(t, err)
|
||||
|
||||
reqPath := "/service/hello"
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
rout.Get(reqPath, hand.SendHello)
|
||||
|
||||
request, err := http.NewRequest("GET", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
bodyReader := recorder.Body
|
||||
bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
|
||||
+37
-2
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestServiceHello(t *testing.T) {
|
||||
func TestFileExists(t *testing.T) {
|
||||
var err error
|
||||
srv, err := server.NewServer()
|
||||
require.NoError(t, err)
|
||||
@@ -25,7 +25,42 @@ func TestServiceHello(t *testing.T) {
|
||||
err = srv.Build()
|
||||
require.NoError(t, err)
|
||||
|
||||
reqPath := service.PathServiceHello
|
||||
reqPath := "/api/v3/file/foo/bare/abc123"
|
||||
routePath := `/api/v3/file/{filepath}`
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
require.NotNil(t, hand)
|
||||
|
||||
rout.Head(routePath, hand.FileExists)
|
||||
|
||||
request, err := http.NewRequest("HEAD", reqPath, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
rout.ServeHTTP(recorder, request)
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
|
||||
fmt.Printf("Response code: %d\n", recorder.Code)
|
||||
|
||||
//bodyReader := recorder.Body
|
||||
//bodyBytes, err := io.ReadAll(bodyReader)
|
||||
|
||||
//fmt.Printf("Response body: %s\n", string(bodyBytes))
|
||||
}
|
||||
|
||||
func xxxTestServiceHello(t *testing.T) {
|
||||
var err error
|
||||
srv, err := server.NewServer()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Configure()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = srv.Build()
|
||||
require.NoError(t, err)
|
||||
|
||||
reqPath := service.ServiceHelloPath
|
||||
|
||||
rout := router.NewRouter()
|
||||
hand := srv.Handler()
|
||||
|
||||
Reference in New Issue
Block a user