Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Commit

Permalink
feat: use middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
AH-dark committed Jan 7, 2024
1 parent 74cb80a commit f750428
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ module github.com/AH-dark/gravatar-with-qq-avatar

go 1.21

replace (
github.com/gocql/gocql => github.com/scylladb/gocql v1.12.0
)
replace github.com/gocql/gocql => github.com/scylladb/gocql v1.12.0

require (
github.com/AH-dark/bytestring v1.0.0
Expand All @@ -13,6 +11,8 @@ require (
github.com/go-redis/redis_rate/v10 v10.0.1
github.com/gocql/gocql v1.6.0
github.com/google/uuid v1.5.0
github.com/hertz-contrib/etag v0.1.0
github.com/hertz-contrib/gzip v0.0.3
github.com/hertz-contrib/obs-opentelemetry/tracing v0.3.1
github.com/imroc/req/v3 v3.42.3
github.com/kolesa-team/go-webp v1.0.4
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/cloudwego/hertz v0.6.8/go.mod h1:KhztQcZtMQ46gOjZcmCy557AKD29cbumGEV0BzwevwA=
github.com/cloudwego/hertz v0.7.3 h1:VM1DxditA6vxI97rG5SBu4hHB24xdzDbKBQfUy7sfVE=
github.com/cloudwego/hertz v0.7.3/go.mod h1:WliNtVbwihWHHgAaIQEbVXl0O3aWj0ks1eoPrcEAnjs=
github.com/cloudwego/netpoll v0.3.2/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/netpoll v0.5.0/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
github.com/cloudwego/netpoll v0.5.1 h1:zDUF7xF0C97I10fGlQFJ4jg65khZZMUvSu/TWX44Ohc=
github.com/cloudwego/netpoll v0.5.1/go.mod h1:xVefXptcyheopwNDZjDPcfU6kIjZXZ4nY550k1yH9eQ=
Expand Down Expand Up @@ -102,6 +104,10 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8/go.mod h1:Nhe/DM3671a5udlv2AdV2ni/MZzgfv2qrPL5nIi3EGQ=
github.com/hertz-contrib/etag v0.1.0 h1:PGWEsrgrQ6J3gy7nBhIIlMA2pfKm5zvFEBmB1jRHYfk=
github.com/hertz-contrib/etag v0.1.0/go.mod h1:BzfTbiJYQTJWKnq6LE86ThKnK6E/i4x44r8z/5hE1qc=
github.com/hertz-contrib/gzip v0.0.3 h1:x+XamFkUYhPPqMzGWejPh1wdDiwkIpHkgRVIg74Xvt4=
github.com/hertz-contrib/gzip v0.0.3/go.mod h1:5rQsvQp1qBmt2TYATbbofNAJBSws2Wwst4EdiHLbx4w=
github.com/hertz-contrib/obs-opentelemetry/tracing v0.3.1 h1:N/rbPCZgrupBNjTEe8Cq+cnTOpgj/y4SU6MYe6uQl0g=
github.com/hertz-contrib/obs-opentelemetry/tracing v0.3.1/go.mod h1:oEnsG4CpBuLx1vcCnxpE/MsNHUrF4qQKw8DiOdtdBa0=
github.com/imroc/req/v3 v3.42.3 h1:ryPG2AiwouutAopwPxKpWKyxgvO8fB3hts4JXlh3PaE=
Expand Down Expand Up @@ -218,6 +224,7 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.13.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand Down
10 changes: 0 additions & 10 deletions server/controllers/avatar/get_avatar.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package avatar

import (
"bytes"
"context"
"net/http"
"strings"
Expand All @@ -13,7 +12,6 @@ import (
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.uber.org/zap"

"github.com/AH-dark/gravatar-with-qq-avatar/pkg/cryptor"
"github.com/AH-dark/gravatar-with-qq-avatar/services/avatar"
)

Expand Down Expand Up @@ -73,14 +71,6 @@ func (h *handlers) GetAvatar(ctx context.Context, c *app.RequestContext) {
if !lastModified.IsZero() {
c.Header("Last-Modified", lastModified.UTC().Format(time.RFC1123))
}

md5 := cryptor.Md5(avatarData)
c.Header("ETag", bytestring.BytesToString(md5))
if bytes.Equal(md5, c.GetHeader("If-None-Match")) {
c.Status(http.StatusNotModified)
return
}

c.Header("Cache-Control", "public, max-age=86400, immutable")
c.Header("Expires", time.Now().Add(86400*time.Second).UTC().Format(time.RFC1123))
c.Header("X-Content-Type-Options", "nosniff")
Expand Down
15 changes: 12 additions & 3 deletions server/controllers/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package controllers

import (
"context"

"github.com/AH-dark/gravatar-with-qq-avatar/server/middlewares"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/hertz-contrib/etag"
"github.com/hertz-contrib/gzip"
"go.opentelemetry.io/otel"
"go.uber.org/fx"

Expand All @@ -21,6 +23,13 @@ func BindControllers(ctx context.Context, svr *server.Hertz, handlers HandlerGro
ctx, span := tracer.Start(ctx, "server.controllers.BindControllers")
defer span.End()

svr.GET("/avatar", handlers.AvatarHandlers.GetAvatar)
svr.GET("/avatar/:hash", handlers.AvatarHandlers.GetAvatar)
svr.Use(middlewares.RequestId())

avatarRouter := svr.Group("/avatar")
avatarRouter.Use(gzip.Gzip(gzip.BestCompression))
avatarRouter.Use(etag.New())
{
avatarRouter.GET("", handlers.AvatarHandlers.GetAvatar)
avatarRouter.GET("/:hash", handlers.AvatarHandlers.GetAvatar)
}
}
5 changes: 5 additions & 0 deletions server/middlewares/middlewares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package middlewares

import "go.opentelemetry.io/otel"

var tracer = otel.Tracer("github.com/AH-dark/gravatar-with-qq-avatar/internal/server/middlewares")
18 changes: 18 additions & 0 deletions server/middlewares/request_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middlewares

import (
"context"
"github.com/cloudwego/hertz/pkg/app"
)

const HeaderRequestId = "X-Request-Id"

func RequestId() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
ctx, span := tracer.Start(ctx, "server.middlewares.RequestId")
defer span.End()

c.Header(HeaderRequestId, span.SpanContext().TraceID().String())
c.Next(ctx)
}
}

0 comments on commit f750428

Please sign in to comment.