working commit

This commit is contained in:
2026-01-23 19:26:56 +02:00
parent 772657d9be
commit d704a76bee
10 changed files with 161 additions and 76 deletions
+8 -3
View File
@@ -8,7 +8,12 @@ import (
func (hand *Handler) FileExists(rctx *router.Context) { func (hand *Handler) FileExists(rctx *router.Context) {
hand.logg.Debugf("handle FileExists") hand.logg.Debugf("handle FileExists")
params := &operator.FileExistsParams{} filepath := rctx.PathMap["filepath"]
res, _ := hand.oper.FileExists(params) params := &operator.FileExistsParams{
rctx.SetStatus(res.Code) Filepath: filepath,
}
hand.logg.Debugf("filepath: %s", filepath)
code, _, _ := hand.oper.FileExists(params)
rctx.SetStatus(code)
} }
+52 -9
View File
@@ -1,19 +1,62 @@
package operator package operator
import ( import (
"io"
"net/http" "net/http"
) )
type FileExistsParams struct{} // File exists
type FileExistsParams struct {
type FileExistsResult struct { Filepath string
Code int 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 var err error
res := &FileExistsResult{ res := &FileExistsResult{}
Code: http.StatusOK, 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
View File
@@ -56,7 +56,7 @@ func pathCompiler(path string) (string, error) {
} }
const ( const (
defaultRegexp = `[a-zA-Z0-9_][\-\.a-zA-Z0-9_%%=:~]+` defaultRegexp = `[a-zA-Z0-9_][/\-\.a-zA-Z0-9_%%=:~]+`
) )
func convertRegexp(src []byte) []byte { func convertRegexp(src []byte) []byte {
+8
View File
@@ -30,6 +30,10 @@ func (rout *Router) AddRoute(method, path string, handlerFunc HandlerFunc) {
rout.routeHandler.AddRoute(method, path, 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) { func (rout *Router) Get(path string, handlerFunc HandlerFunc) {
rout.routeHandler.AddRoute("GET", path, 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) 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) { func (rout *Router) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
rctx := NewContext(writer, req) rctx := NewContext(writer, req)
rout.routeHandler.ServeHTTP(rctx) rout.routeHandler.ServeHTTP(rctx)
+5 -7
View File
@@ -4,13 +4,11 @@ import (
"fmt" "fmt"
) )
const (
PathServiceHello = "/service/hello"
)
var ( var (
pathRegexp = `{path:[a-zA-Z_][\-a-zA-Z0-9_\/\.%~]+}` filepathRegexp = `{path:[a-zA-Z_][\-a-zA-Z0-9_\/\.%~]+}`
fileOperPrefix = "/v3/file" fileApiPrefix = `/v3/file`
PathFileExists = fmt.Sprintf("%s/%s", fileOperPrefix, pathRegexp) FileExistsPath = fmt.Sprintf("%s/%s", fileApiPrefix, filepathRegexp)
ServiceHelloPath = "/service/hello"
) )
+2 -2
View File
@@ -43,8 +43,8 @@ func (svc *Service) Build() error {
svc.logg.Infof("Service build ") svc.logg.Infof("Service build ")
svc.rout = router.NewRouter() svc.rout = router.NewRouter()
svc.rout.Get(PathServiceHello, svc.hand.SendHello) svc.rout.Get(ServiceHelloPath, svc.hand.SendHello)
svc.rout.Get(PathFileExists, svc.hand.FileExists) svc.rout.Get(FileExistsPath, svc.hand.FileExists)
listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum) listenAddress := fmt.Sprintf("%s:%d", svc.address, svc.portnum)
svc.listen, err = net.Listen(protocol, listenAddress) svc.listen, err = net.Listen(protocol, listenAddress)
+45 -4
View File
@@ -20,10 +20,40 @@ func (util *Util) AddFileCmds() {
putFileCmd.Flags().StringVarP(&util.putFileParams.Dest, "dest", "d", "", "Desctination path") putFileCmd.Flags().StringVarP(&util.putFileParams.Dest, "dest", "d", "", "Desctination path")
subCmd.AddCommand(putFileCmd) 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 { type PutFileParams struct {
Source string Source string
Dest string Dest string
@@ -31,8 +61,10 @@ type PutFileParams struct {
Password string 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 { type GetFileParams struct {
Source string Source string
Dest string Dest string
@@ -40,6 +72,15 @@ type GetFileParams struct {
Password string 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) {
}
+3 -3
View File
@@ -21,8 +21,8 @@ func main() {
} }
type Util struct { type Util struct {
rootCmd *cobra.Command FileUtil
putFileParams PutFileParams rootCmd cobra.Command
} }
func NewUtil() *Util { func NewUtil() *Util {
@@ -31,7 +31,7 @@ func NewUtil() *Util {
func (util *Util) Build() error { func (util *Util) Build() error {
var err error var err error
rootCmd := &cobra.Command{ rootCmd := cobra.Command{
Use: "cmd", Use: "cmd",
Short: "A brief description the command", Short: "A brief description the command",
} }
-45
View File
@@ -1,46 +1 @@
package test 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
View File
@@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestServiceHello(t *testing.T) { func TestFileExists(t *testing.T) {
var err error var err error
srv, err := server.NewServer() srv, err := server.NewServer()
require.NoError(t, err) require.NoError(t, err)
@@ -25,7 +25,42 @@ func TestServiceHello(t *testing.T) {
err = srv.Build() err = srv.Build()
require.NoError(t, err) 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() rout := router.NewRouter()
hand := srv.Handler() hand := srv.Handler()