From 53b22bc85daa1a2f88fbcf817cac08d6cef60c21 Mon Sep 17 00:00:00 2001 From: tykim96 Date: Fri, 24 Nov 2023 15:02:03 +0900 Subject: [PATCH] feat: Replace gin to echo --- service/osc/get.go | 2 +- web/templates/mig-ncpToWindows.html | 2 +- web/templates/mig-s3ToWindows.html | 2 +- websrc/controllers/generateHandlers.go | 260 +-------------------- websrc/controllers/migrationGCPHandlers.go | 8 +- websrc/controllers/migrationNCPHandlers.go | 8 +- websrc/controllers/migrationS3Handlers.go | 4 +- websrc/routes/routes.go | 4 - websrc/serve/serve.go | 23 +- 9 files changed, 25 insertions(+), 288 deletions(-) diff --git a/service/osc/get.go b/service/osc/get.go index fe16046..1a741c4 100644 --- a/service/osc/get.go +++ b/service/osc/get.go @@ -102,7 +102,7 @@ func getDownloadList(fileList, objList []*utils.Object, dirPath string) ([]*util for _, file := range fileList { fileName, _ := filepath.Rel(dirPath, file.Key) objName, _ := filepath.Rel(filepath.Base(dirPath), obj.Key) - if objName == fileName { + if strings.Contains(objName, fileName) { chk = true if obj.Size != file.Size { downloadList = append(downloadList, obj) diff --git a/web/templates/mig-ncpToWindows.html b/web/templates/mig-ncpToWindows.html index 5113e88..669f0d1 100644 --- a/web/templates/mig-ncpToWindows.html +++ b/web/templates/mig-ncpToWindows.html @@ -66,7 +66,7 @@

마이그레이션 Naver Object Storage

- +
diff --git a/web/templates/mig-s3ToWindows.html b/web/templates/mig-s3ToWindows.html index 02b8d5d..06b1659 100644 --- a/web/templates/mig-s3ToWindows.html +++ b/web/templates/mig-s3ToWindows.html @@ -18,7 +18,7 @@

마이그레이션 AWS S3

- {{ range $index, $value := .Regions }} diff --git a/websrc/controllers/generateHandlers.go b/websrc/controllers/generateHandlers.go index fa44b78..a7f9ed6 100644 --- a/websrc/controllers/generateHandlers.go +++ b/websrc/controllers/generateHandlers.go @@ -1,6 +1,7 @@ package controllers import ( + "fmt" "net/http" "os" "path/filepath" @@ -10,18 +11,6 @@ import ( "github.com/labstack/echo/v4" ) -// func GenerateLinuxGetHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// logger := getLogger("genlinux") -// logger.Info("genlinux get page accessed") -// ctx.HTML(http.StatusOK, "index.html", map[string]interface{}{ -// "Content": "Generate-Linux", -// "os": runtime.GOOS, -// "error": nil, -// }) -// } -// } - func GenerateLinuxGetHandler(ctx echo.Context) error { logger := getLogger("genlinux") @@ -34,38 +23,6 @@ func GenerateLinuxGetHandler(ctx echo.Context) error { }) } -// func GenerateLinuxPostHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// start := time.Now() - -// logger, logstrings := pageLogInit("genlinux", "Create dummy data in linux", start) - -// if !osCheck(logger, start, "linux") { -// ctx.JSONP(http.StatusBadRequest, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// params := getData("gen", ctx).(GenDataParams) - -// if !dummyCreate(logger, start, params) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// jobEnd(logger, "Successfully creating a dummy with Linux", start) -// ctx.JSONP(http.StatusOK, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// } -// } - func GenerateLinuxPostHandler(ctx echo.Context) error { start := time.Now() @@ -79,7 +36,6 @@ func GenerateLinuxPostHandler(ctx echo.Context) error { }) } - // params := getData("gen", ctx).(GenDataParams) params := GenDataParams{} if err := ctx.Bind(params); err != nil { return err @@ -99,20 +55,6 @@ func GenerateLinuxPostHandler(ctx echo.Context) error { }) } -// func GenerateWindowsGetHandler() gin.HandlerFunc { -// tmpPath := filepath.Join(os.TempDir(), "dummy") -// return func(ctx *gin.Context) { -// logger := getLogger("genwindows") -// logger.Info("genwindows get page accessed") -// ctx.HTML(http.StatusOK, "index.html", map[string]interface{}{ -// "Content": "Generate-Windows", -// "os": runtime.GOOS, -// "tmpPath": tmpPath, -// "error": nil, -// }) -// } -// } - func GenerateWindowsGetHandler(ctx echo.Context) error { tmpPath := filepath.Join(os.TempDir(), "dummy") @@ -127,38 +69,6 @@ func GenerateWindowsGetHandler(ctx echo.Context) error { }) } -// func GenerateWindowsPostHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// start := time.Now() - -// logger, logstrings := pageLogInit("genwindows", "Create dummy data in windows", start) - -// if !osCheck(logger, start, "windows") { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// params := getData("gen", ctx).(GenDataParams) - -// if !dummyCreate(logger, start, params) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// jobEnd(logger, "Successfully creating a dummy with Windows", start) -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// } -// } - func GenerateWindowsPostHandler(ctx echo.Context) error { start := time.Now() @@ -166,6 +76,7 @@ func GenerateWindowsPostHandler(ctx echo.Context) error { logger, logstrings := pageLogInit("genwindows", "Create dummy data in windows", start) if !osCheck(logger, start, "windows") { + fmt.Println("test") return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ "Result": logstrings.String(), "Error": nil, @@ -173,11 +84,7 @@ func GenerateWindowsPostHandler(ctx echo.Context) error { } - // params := getData("gen", ctx).(GenDataParams) - params := GenDataParams{} - if err := ctx.Bind(params); err != nil { - return err - } + params := getData("gen", ctx).(GenDataParams) if !dummyCreate(logger, start, params) { return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ @@ -188,25 +95,12 @@ func GenerateWindowsPostHandler(ctx echo.Context) error { } jobEnd(logger, "Successfully creating a dummy with Windows", start) - return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + return ctx.JSON(http.StatusOK, map[string]interface{}{ "Result": logstrings.String(), "Error": nil, }) } -// func GenerateS3GetHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// logger := getLogger("genS3") -// logger.Info("genS3 get page accessed") -// ctx.HTML(http.StatusOK, "index.html", map[string]interface{}{ -// "Content": "Generate-S3", -// "os": runtime.GOOS, -// "Regions": GetAWSRegions(), -// "Error": nil, -// }) -// } -// } - func GenerateS3GetHandler(ctx echo.Context) error { logger := getLogger("genS3") @@ -219,68 +113,12 @@ func GenerateS3GetHandler(ctx echo.Context) error { }) } -// func GenerateS3PostHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// start := time.Now() - -// logger, logstrings := pageLogInit("genS3", "Create dummy data and import to s3", start) - -// params := getData("gen", ctx).(GenDataParams) - -// tmpDir, ok := createDummyTemp(logger, start) -// if !ok { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } -// defer os.RemoveAll(tmpDir) -// params.DummyPath = tmpDir - -// if !dummyCreate(logger, start, params) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// awsOSC := getS3OSC(logger, start, "gen", params) -// if awsOSC == nil { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// if !oscImport(logger, start, "s3", awsOSC, params.DummyPath) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// jobEnd(logger, "Dummy creation and import successful with s3", start) -// ctx.JSONP(http.StatusOK, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// } -// } - func GenerateS3PostHandler(ctx echo.Context) error { start := time.Now() logger, logstrings := pageLogInit("genS3", "Create dummy data and import to s3", start) - // params := getData("gen", ctx).(GenDataParams) - params := GenDataParams{} - if err := ctx.Bind(params); err != nil { - return err - } + params := getData("gen", ctx).(GenDataParams) tmpDir, ok := createDummyTemp(logger, start) if !ok { @@ -325,19 +163,6 @@ func GenerateS3PostHandler(ctx echo.Context) error { }) } -// func GenerateGCPGetHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// logger := getLogger("genGCP") -// logger.Info("genGCP get page accessed") -// ctx.HTML(http.StatusOK, "index.html", map[string]interface{}{ -// "Content": "Generate-GCP", -// "os": runtime.GOOS, -// "Regions": GetGCPRegions(), -// "error": nil, -// }) -// } -// } - func GenerateGCPGetHandler(ctx echo.Context) error { logger := getLogger("genGCP") logger.Info("genGCP get page accessed") @@ -349,85 +174,12 @@ func GenerateGCPGetHandler(ctx echo.Context) error { }) } -// func GenerateGCPPostHandler() gin.HandlerFunc { -// return func(ctx *gin.Context) { -// start := time.Now() - -// logger, logstrings := pageLogInit("genGCP", "Create dummy data and import to gcp", start) - -// params := GenDataParams{} -// if !getDataWithBind(logger, start, ctx, ¶ms) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// credTmpDir, credFileName, ok := gcpCreateCredFile(logger, start, ctx) -// if !ok { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } -// defer os.RemoveAll(credTmpDir) - -// tmpDir, ok := createDummyTemp(logger, start) -// if !ok { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } -// defer os.RemoveAll(tmpDir) -// params.DummyPath = tmpDir - -// if !dummyCreate(logger, start, params) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// gcpOSC := getGCPCOSC(logger, start, "gen", params, credFileName) -// if gcpOSC == nil { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// if !oscImport(logger, start, "gcp", gcpOSC, params.DummyPath) { -// ctx.JSONP(http.StatusInternalServerError, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// return -// } - -// jobEnd(logger, "Dummy creation and import successful with gcp", start) -// ctx.JSONP(http.StatusOK, map[string]interface{}{ -// "Result": logstrings.String(), -// "Error": nil, -// }) -// } -// } - func GenerateGCPPostHandler(ctx echo.Context) error { start := time.Now() logger, logstrings := pageLogInit("genGCP", "Create dummy data and import to gcp", start) - // params := getData("gen", ctx).(GenDataParams) - params := GenDataParams{} - if err := ctx.Bind(params); err != nil { - return err - } + params := getData("gen", ctx).(GenDataParams) if !getDataWithBind(logger, start, ctx, ¶ms) { return ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ diff --git a/websrc/controllers/migrationGCPHandlers.go b/websrc/controllers/migrationGCPHandlers.go index df68274..cee98e6 100644 --- a/websrc/controllers/migrationGCPHandlers.go +++ b/websrc/controllers/migrationGCPHandlers.go @@ -99,7 +99,7 @@ func MigrationGCPToWindowsPostHandler(ctx echo.Context) error { logger, logstrings := pageLogInit("miggcpwin", "Export gcp data to windows", start) - if !osCheck(logger, start, "linux") { + if !osCheck(logger, start, "windows") { return ctx.JSON(http.StatusBadRequest, gin.H{ "Result": logstrings.String(), "Error": nil, @@ -196,9 +196,10 @@ func MigrationGCPToS3PostHandler(ctx echo.Context) error { }) } + logger.Infof("Start migration of GCP Cloud Storage to AWS S3") if err := gcpOSC.Copy(awsOSC); err != nil { end := time.Now() - logger.Errorf("OSController copy failed : %v", err) + logger.Errorf("OSController migration failed : %v", err) logger.Infof("End time : %s", end.Format("2006-01-02T15:04:05-07:00")) logger.Infof("Elapsed time : %s", end.Sub(start).String()) return ctx.JSON(http.StatusOK, gin.H{ @@ -267,9 +268,10 @@ func MigrationGCPToNCPPostHandler(ctx echo.Context) error { }) } + logger.Infof("Start migration of GCP Cloud Storage to NCP Object Storage") if err := gcpOSC.Copy(ncpOSC); err != nil { end := time.Now() - logger.Errorf("OSController copy failed : %v", err) + logger.Errorf("OSController migration failed : %v", err) logger.Infof("End time : %s", end.Format("2006-01-02T15:04:05-07:00")) logger.Infof("Elapsed time : %s", end.Sub(start).String()) return ctx.JSON(http.StatusOK, gin.H{ diff --git a/websrc/controllers/migrationNCPHandlers.go b/websrc/controllers/migrationNCPHandlers.go index e345770..d52e294 100644 --- a/websrc/controllers/migrationNCPHandlers.go +++ b/websrc/controllers/migrationNCPHandlers.go @@ -77,7 +77,7 @@ func MigrationNCPToWindowsGetHandler(ctx echo.Context) error { "Content": "Migration-NCP-Windows", "Regions": GetNCPRegions(), "os": runtime.GOOS, - "tmpPaht": tmpPath, + "tmpPath": tmpPath, "error": nil, }) } @@ -169,9 +169,10 @@ func MigrationNCPToS3PostHandler(ctx echo.Context) error { }) } + logger.Infof("Start migration of NCP Object Storage to AWS S3") if err := ncpOSC.Copy(awsOSC); err != nil { end := time.Now() - logger.Errorf("OSController copy failed : %v", err) + logger.Errorf("OSController migration failed : %v", err) logger.Infof("End time : %s", end.Format("2006-01-02T15:04:05-07:00")) logger.Infof("Elapsed time : %s", end.Sub(start).String()) return ctx.JSON(http.StatusOK, map[string]interface{}{ @@ -240,9 +241,10 @@ func MigrationNCPToGCPPostHandler(ctx echo.Context) error { }) } + logger.Infof("Start migration of NCP Object Storage to GCP Cloud Storage") if err := ncpOSC.Copy(gcpOSC); err != nil { end := time.Now() - logger.Errorf("OSController copy failed : %v", err) + logger.Errorf("OSController migration failed : %v", err) logger.Infof("End time : %s", end.Format("2006-01-02T15:04:05-07:00")) logger.Infof("Elapsed time : %s", end.Sub(start).String()) return ctx.JSON(http.StatusOK, map[string]interface{}{ diff --git a/websrc/controllers/migrationS3Handlers.go b/websrc/controllers/migrationS3Handlers.go index 42bc91c..e7e30d7 100644 --- a/websrc/controllers/migrationS3Handlers.go +++ b/websrc/controllers/migrationS3Handlers.go @@ -179,9 +179,10 @@ func MigrationS3ToGCPPostHandler(ctx echo.Context) error { }) } + logger.Infof("Start migration of AWS S3 to GCP Cloud Storage") if err := awsOSC.Copy(gcpOSC); err != nil { end := time.Now() - logger.Errorf("OSController copy failed : %v", err) + logger.Errorf("OSController migration failed : %v", err) logger.Infof("End time : %s", end.Format("2006-01-02T15:04:05-07:00")) logger.Infof("Elapsed time : %s", end.Sub(start).String()) return ctx.JSON(http.StatusOK, map[string]interface{}{ @@ -240,6 +241,7 @@ func MigrationS3ToNCPPostHandler(ctx echo.Context) error { }) } + logger.Info("Start migration of AWS S3 to NCP Objest Storage") if err := awsOSC.Copy(ncpOSC); err != nil { end := time.Now() logger.Errorf("OSController copy failed : %v", err) diff --git a/websrc/routes/routes.go b/websrc/routes/routes.go index c8e7ba2..202fdc6 100644 --- a/websrc/routes/routes.go +++ b/websrc/routes/routes.go @@ -5,10 +5,6 @@ import ( "github.com/labstack/echo/v4" ) -func MainRoutes(g *echo.Group) { - g.GET("/", controllers.MainGetHandler) -} - func GenerateRoutes(g *echo.Group) { g.GET("/linux", controllers.GenerateLinuxGetHandler) g.POST("/linux", controllers.GenerateLinuxPostHandler) diff --git a/websrc/serve/serve.go b/websrc/serve/serve.go index 8338c4a..8cd7b6e 100644 --- a/websrc/serve/serve.go +++ b/websrc/serve/serve.go @@ -7,6 +7,7 @@ import ( "net/http" "strings" + "github.com/cloud-barista/cm-data-mold/websrc/controllers" "github.com/cloud-barista/cm-data-mold/websrc/routes" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -48,22 +49,13 @@ func TrustedProxiesMiddleware(trustedProxies []string) echo.MiddlewareFunc { } func InitServer() *echo.Echo { - // router = gin.New() - // router.Use(gin.Logger()) - e := echo.New() // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) + e.Use(TrustedProxiesMiddleware([]string{"127.0.0.1", "::1"})) - // router.ForwardedByClientIP = true - // router.SetTrustedProxies([]string{"127.0.0.1"}) - e.Use(TrustedProxiesMiddleware([]string{"127.0.0.1"})) - - // router.Static("/res", "./web") - // router.LoadHTMLGlob("./web/templates/*") - // router.StaticFile("/favicon.ico", "./web/assets/favicon.ico") e.Static("/res", "./web") e.File("/favicon.ico", "./web/assets/favicon.ico") renderer := &TemplateRenderer{ @@ -71,27 +63,18 @@ func InitServer() *echo.Echo { } e.Renderer = renderer - // mainGroup := router.Group("/") - // routes.MainRoutes(mainGroup) - mainGroup := e.Group("/") - routes.MainRoutes(mainGroup) + e.GET("/", controllers.MainGetHandler) - // generateGroup := router.Group("/generate") - // routes.GenerateRoutes(generateGroup) generateGroup := e.Group("/generate") routes.GenerateRoutes(generateGroup) - // migrationGroup := router.Group("/migration") - // routes.MigrationRoutes(migrationGroup) migrationGroup := e.Group("/migration") routes.MigrationRoutes(migrationGroup) - // return router return e } func Run(rt *echo.Echo, port string) { - // rt.Run(":" + port) port = fmt.Sprintf(":%s", port) if err := rt.Start(port); err != nil && err != http.ErrServerClosed { rt.Logger.Error(err)