code refactor

This commit is contained in:
MaysWind
2024-07-28 16:04:34 +08:00
parent 2e04affb00
commit c92a9e61b0
2 changed files with 5 additions and 5 deletions
+4 -4
View File
@@ -195,7 +195,7 @@ func startWebServer(c *cli.Context) error {
qrCodeRoute.Use(bindMiddleware(middlewares.RequestId(config))) qrCodeRoute.Use(bindMiddleware(middlewares.RequestId(config)))
{ {
qrCodeCacheStore := persistence.NewInMemoryStore(time.Minute) qrCodeCacheStore := persistence.NewInMemoryStore(time.Minute)
qrCodeRoute.GET("/mobile_url.png", bindCachedPngImage(api.QrCodes.MobileUrlQrCodeHandler, qrCodeCacheStore)) qrCodeRoute.GET("/mobile_url.png", bindCachedImage(api.QrCodes.MobileUrlQrCodeHandler, qrCodeCacheStore))
} }
apiRoute := router.Group("/api") apiRoute := router.Group("/api")
@@ -423,15 +423,15 @@ func bindImage(fn core.ImageHandlerFunc) gin.HandlerFunc {
} }
} }
func bindCachedPngImage(fn core.DataHandlerFunc, store persistence.CacheStore) gin.HandlerFunc { func bindCachedImage(fn core.ImageHandlerFunc, store persistence.CacheStore) gin.HandlerFunc {
return cache.CachePage(store, time.Minute, func(ginCtx *gin.Context) { return cache.CachePage(store, time.Minute, func(ginCtx *gin.Context) {
c := core.WrapContext(ginCtx) c := core.WrapContext(ginCtx)
result, _, err := fn(c) result, contentType, err := fn(c)
if err != nil { if err != nil {
utils.PrintDataErrorResult(c, "text/text", err) utils.PrintDataErrorResult(c, "text/text", err)
} else { } else {
utils.PrintDataSuccessResult(c, "img/png", "", result) utils.PrintDataSuccessResult(c, contentType, "", result)
} }
}) })
} }
+1 -1
View File
@@ -35,7 +35,7 @@ func (a *QrCodesApi) MobileUrlQrCodeHandler(c *core.Context) ([]byte, string, *e
return nil, "", errs.ErrOperationFailed return nil, "", errs.ErrOperationFailed
} }
return data, "", nil return data, "image/png", nil
} }
func (a *QrCodesApi) generateUrlQrCode(c *core.Context, url string) ([]byte, *errs.Error) { func (a *QrCodesApi) generateUrlQrCode(c *core.Context, url string) ([]byte, *errs.Error) {