diff --git a/controller/author.go b/controller/author.go index de23db9..1435643 100644 --- a/controller/author.go +++ b/controller/author.go @@ -20,6 +20,8 @@ func LoginPage(c *gin.Context) { func UserSignInHandler(jwtC *jwt.Jwt) func(c *gin.Context) error { return func(c *gin.Context) error { + log, _ := utils.GetLogFromContext(c) + var user models.UserInfoParams if err := c.ShouldBindJSON(&user); err != nil { @@ -39,10 +41,10 @@ func UserSignInHandler(jwtC *jwt.Jwt) func(c *gin.Context) error { return err } - fmt.Println(tokenStr) - token := fmt.Sprintf("%s%s", models.GinAuthorPrefixKey, tokenStr) + log.Sugar().Infof("Set token: %s", token) + c.SetCookie(models.GinAuthorKey, token, 24*60*60, "/", "", false, true) c.JSON(http.StatusOK, gin.H{"message": "Login successful"}) @@ -55,6 +57,8 @@ func UserLogOutHandler(c *gin.Context) { log, _ := utils.GetLogFromContext(c) c.SetCookie(models.GinAuthorKey, "", -1, "/", "", false, true) + log.Sugar().Info("Logout successful") + c.JSON(http.StatusOK, gin.H{"message": "Logout successful"}) } diff --git a/controller/files.go b/controller/files.go index 5a7da9f..f58d72e 100644 --- a/controller/files.go +++ b/controller/files.go @@ -45,7 +45,7 @@ func FileUploadHandle(c *gin.Context) error { } // 限制文件大小(10MB) - const maxFileSize = 10 << 20 // 10MB + const maxFileSize = 100 << 20 // 100MB if file.Size > maxFileSize { log.Sugar().Errorf("File too large: %d bytes", file.Size) return errors.New("File size exceeds 10MB") @@ -64,7 +64,8 @@ func FileUploadHandle(c *gin.Context) error { return err } - log.Sugar().Debug("File uploaded successfully to %s", targetPath) + log.Sugar().Debugf("File uploaded successfully to %s", targetPath) + c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("File uploaded to %s", targetPath)}) return nil @@ -114,7 +115,8 @@ func FileListHandle(c *gin.Context) error { }) } - log.Sugar().Debug("Listed files in %s: %d entries", cleanPath, len(files)) + log.Sugar().Debugf("Listed files in %s: %d entries", cleanPath, len(files)) + c.JSON(http.StatusOK, gin.H{"files": files}) return nil @@ -151,7 +153,7 @@ func FileDownloadHandle(c *gin.Context) error { if !fileInfo.IsDir() { // 处理文件下载 - const maxFileSize = 10 << 20 // 10MB + const maxFileSize = 100 << 20 // 100MB if fileInfo.Size() > maxFileSize { log.Sugar().Errorf("File too large: %d bytes", fileInfo.Size()) return errors.New("File size exceeds 10MB") @@ -160,6 +162,7 @@ func FileDownloadHandle(c *gin.Context) error { c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filepath.Base(cleanPath))) c.Header("Content-Type", "application/octet-stream") c.File(cleanPath) + return nil } @@ -173,7 +176,7 @@ func FileDownloadHandle(c *gin.Context) error { zipWriter := zip.NewWriter(tmpFile) totalSize := int64(0) - const maxZipSize = 10 << 20 // 10MB + const maxZipSize = 100 << 20 // 100MB err = filepath.Walk(cleanPath, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -199,7 +202,7 @@ func FileDownloadHandle(c *gin.Context) error { header.Method = zip.Deflate totalSize += info.Size() if totalSize > maxZipSize { - return fmt.Errorf("directory size exceeds 10MB") + return fmt.Errorf("directory size exceeds 100MB") } } @@ -238,7 +241,8 @@ func FileDownloadHandle(c *gin.Context) error { c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%q", filepath.Base(cleanPath)+".zip")) c.Header("Content-Type", "application/zip") c.File(tmpFile.Name()) - log.Sugar().Debug("Directory %s downloaded as zip", cleanPath) + + log.Sugar().Debugf("Directory %s downloaded as zip", cleanPath) return nil } diff --git a/routes/middleware.go b/routes/middleware.go index 72ef1cb..c0f76fe 100644 --- a/routes/middleware.go +++ b/routes/middleware.go @@ -133,15 +133,17 @@ func GinJwtAuthor(jwtC *jwt.Jwt) appHandler { func GinRateLimit(rateC settings.RateLimitConfig) appHandler { lrate, err := rate.New(rate.WithCapacity(rateC.Capacity), - rate.WithFillInterval(time.Duration(rateC.FillInterval)), + rate.WithFillInterval(time.Duration(rateC.FillInterval)*time.Millisecond), ) - if err != nil { + if err != nil || lrate.Bucket == nil { panic(err) } return func(c *gin.Context) error { - if lrate.Available() ==0 { + if !lrate.WaitMaxDuration(1, time.Duration(rateC.MaxWait)*time.Second) { + time.Sleep(time.Duration(rateC.MaxWait) * time.Second) + c.Abort() return &models.BaseError{ Code: http.StatusServiceUnavailable, Msg: "Exceeded rate limit", diff --git a/routes/wrapperr.go b/routes/wrapperr.go index 22b9d00..44e960a 100644 --- a/routes/wrapperr.go +++ b/routes/wrapperr.go @@ -21,7 +21,7 @@ func errWapper(appH appHandler) gin.HandlerFunc { if err != nil { var baseErr *models.BaseError if errors.As(err, &baseErr) { - log.Error("Base error", zap.Any("res", baseErr)) + log.Error("Base error", zap.Any("baseErr", baseErr)) c.JSON(http.StatusBadRequest, baseErr) diff --git a/static/index.html b/static/index.html index ed989ba..d1f279f 100755 --- a/static/index.html +++ b/static/index.html @@ -3,7 +3,7 @@
-