Skip to content

Commit

Permalink
fix(api): also add etag
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Apr 18, 2024
1 parent b49c284 commit c0e57b9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
7 changes: 6 additions & 1 deletion _oas/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ paths:
description: "The Cache-Control header provides important info on how long a response may be considered fresh"
schema:
type: string
example: "Cache-Control: max-age=3600, public, must-revalidate"
example: "max-age=3600, public, must-revalidate"
ETag:
description: "The ETag header provides a unique identifier for the response"
schema:
type: string
example: "W/\"123456789\""
content:
"image/svg+xml":
schema:
Expand Down
8 changes: 8 additions & 0 deletions internal/api/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"bytes"
"context"
"fmt"
"hash/crc32"
"io"
"net/http"
"net/url"
Expand All @@ -27,6 +29,11 @@ func generateBadgePath(name, text, style string) string {
}, "-")
}

func etag(name string, data []byte) string {
crc := crc32.ChecksumIEEE(data)
return fmt.Sprintf(`W/"%s-%d-%08X"`, name, len(data), crc)
}

func (s Server) GetTelegramBadge(ctx context.Context, params oas.GetTelegramBadgeParams) (*oas.GetTelegramBadgeOKHeaders, error) {
members := map[string]int{
"gotd_en": 237,
Expand Down Expand Up @@ -60,6 +67,7 @@ func (s Server) GetTelegramBadge(ctx context.Context, params oas.GetTelegramBadg

return &oas.GetTelegramBadgeOKHeaders{
CacheControl: oas.NewOptString("no-cache"),
ETag: oas.NewOptString(etag(params.GroupName, data)),
Response: oas.GetTelegramBadgeOK{
Data: bytes.NewReader(data),
},
Expand Down
37 changes: 37 additions & 0 deletions internal/oas/oas_response_decoders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/oas/oas_response_encoders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/oas/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c0e57b9

Please sign in to comment.