diff --git a/app/handler/file.go b/app/handler/file.go index 2273813..8bf7395 100644 --- a/app/handler/file.go +++ b/app/handler/file.go @@ -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) } diff --git a/app/operator/file.go b/app/operator/file.go index a6f3015..4e5df6c 100644 --- a/app/operator/file.go +++ b/app/operator/file.go @@ -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, - } - return res, err + res := &FileExistsResult{} + code := http.StatusOK + return code, 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 } diff --git a/app/router/pathc.go b/app/router/pathc.go index bef6469..bb8ba65 100644 --- a/app/router/pathc.go +++ b/app/router/pathc.go @@ -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 { diff --git a/app/router/router.go b/app/router/router.go index 8f00e18..d700643 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -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) diff --git a/app/service/path.go b/app/service/path.go index 9d2f914..f7966f7 100644 --- a/app/service/path.go +++ b/app/service/path.go @@ -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" ) diff --git a/app/service/service.go b/app/service/service.go index 8ea8b00..0b9bc5d 100644 --- a/app/service/service.go +++ b/app/service/service.go @@ -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) diff --git a/cmd/mstorectl/file.go b/cmd/mstorectl/file.go index fc4e9df..728f2e6 100644 --- a/cmd/mstorectl/file.go +++ b/cmd/mstorectl/file.go @@ -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) { +} diff --git a/cmd/mstorectl/main.go b/cmd/mstorectl/main.go index 3700cf9..bb14d60 100644 --- a/cmd/mstorectl/main.go +++ b/cmd/mstorectl/main.go @@ -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", } diff --git a/test/main_test.go b/test/main_test.go index edecd56..56e5404 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -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)) -} diff --git a/test/svc_test.go b/test/svc_test.go index 4cc85ae..1691929 100644 --- a/test/svc_test.go +++ b/test/svc_test.go @@ -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()