Skip to content

Commit 2970a88

Browse files
committed
feat!: split gin-metrics out to several modules
Specifically, these are the new modules: * gin-caching-response-headers * gin-json-abort * gin-preconditions
1 parent dd78d58 commit 2970a88

29 files changed

Lines changed: 1107 additions & 216 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Hung T. Nguyen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Set response caching headers in Gin handlers
2+
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/nguyengg/go-aws-commons/gin-caching-response-headers.svg)](https://pkg.go.dev/github.com/nguyengg/go-aws-commons/gin-caching-response-headers)
4+
5+
```shell
6+
go get github.com/nguyengg/go-aws-commons/gin-caching-response-headers
7+
```
8+
9+
Usage:
10+
```shell
11+
package main
12+
13+
import (
14+
"fmt"
15+
"strings"
16+
"time"
17+
18+
"github.com/gin-gonic/gin"
19+
cachingheaders "github.com/nguyengg/go-aws-commons/gin-caching-response-headers"
20+
cc "github.com/nguyengg/go-aws-commons/gin-caching-response-headers/cachecontrol"
21+
)
22+
23+
// Response implements all three Cache-Control, ETag, and Last-Modified.
24+
type Response struct {
25+
Data []byte `json:"data"`
26+
ETag string `json:"etag"`
27+
LastModified time.Time `json:"lastModified"`
28+
}
29+
30+
var _ cachingheaders.HasCacheControl = &Response{}
31+
var _ cachingheaders.HasCacheControl = (*Response)(nil)
32+
33+
func (r Response) GetCacheControl() string {
34+
return cc.Join(cc.SMaxAge(3*time.Hour), cc.Private)
35+
}
36+
37+
var _ cachingheaders.HasETag = &Response{}
38+
var _ cachingheaders.HasETag = (*Response)(nil)
39+
40+
func (r Response) GetETag() string {
41+
// must be wrapped by quotes such as "xyyyz", and if it's a weak ETag, it must be prefixed with W/ like W/"xyyyz".
42+
return fmt.Sprintf(`"%s"`, strings.Trim(r.ETag, `"`))
43+
}
44+
45+
var _ cachingheaders.HasLastModified = &Response{}
46+
var _ cachingheaders.HasLastModified = (*Response)(nil)
47+
48+
func (r Response) GetLastModified() time.Time {
49+
return r.LastModified
50+
}
51+
52+
func main() {
53+
r := gin.New()
54+
r.GET("/", func(c *gin.Context) {
55+
res := Response{
56+
ETag: `W/"xyyyz"`,
57+
LastModified: time.Now(),
58+
}
59+
60+
// this will set "Cache-Control", "ETag", and "Last-Modified".
61+
cachingheaders.Set(c, res)
62+
c.JSON(200, res)
63+
})
64+
}
65+
66+
```

gin-metrics/cachecontrol/cachecontrol.go renamed to gin-caching-response-headers/cachecontrol/cachecontrol.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package cachecontrol provides a declarative way to create the Cache-Control directives.
12
package cachecontrol
23

34
import (
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package caching
1+
package cachingheaders
22

33
import (
44
"net/http"
@@ -7,10 +7,11 @@ import (
77
"github.com/gin-gonic/gin"
88
)
99

10-
// Headers sets the caching headers (Cache-Control, ETag, and/or Last-Modified) retrieved from the given obj.
10+
// Set sets the caching response headers (Cache-Control, ETag, and/or Last-Modified) from the given obj.
1111
//
12-
// Objects should implement HasCacheControl, HasETag, and/or HasLastModified for this method to do any work.
13-
func Headers(c *gin.Context, obj any) {
12+
// Objects should implement HasCacheControl, HasETag, and/or HasLastModified for this method to do any work. Expires
13+
// header is deprecated so there is no support for it.
14+
func Set(c *gin.Context, obj any) {
1415
if v, ok := obj.(HasCacheControl); ok {
1516
if s := v.GetCacheControl(); s != "" {
1617
c.Header("Cache-Control", s)
@@ -33,6 +34,8 @@ func Headers(c *gin.Context, obj any) {
3334
// HasCacheControl implements GetCacheControl for objects that should be returned with response header "Cache-Control".
3435
type HasCacheControl interface {
3536
// GetCacheControl returns the "Cache-Control" response header.
37+
//
38+
// You should use cachecontrol.Join to help generate this string.
3639
GetCacheControl() string
3740
}
3841

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module github.com/nguyengg/go-aws-commons/gin-caching-response-headers
2+
3+
go 1.25.0
4+
5+
require github.com/gin-gonic/gin v1.11.0
6+
7+
require (
8+
github.com/bytedance/sonic v1.14.0 // indirect
9+
github.com/bytedance/sonic/loader v0.3.0 // indirect
10+
github.com/cloudwego/base64x v0.1.6 // indirect
11+
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
12+
github.com/gin-contrib/sse v1.1.0 // indirect
13+
github.com/go-playground/locales v0.14.1 // indirect
14+
github.com/go-playground/universal-translator v0.18.1 // indirect
15+
github.com/go-playground/validator/v10 v10.27.0 // indirect
16+
github.com/goccy/go-json v0.10.2 // indirect
17+
github.com/goccy/go-yaml v1.18.0 // indirect
18+
github.com/json-iterator/go v1.1.12 // indirect
19+
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
20+
github.com/leodido/go-urn v1.4.0 // indirect
21+
github.com/mattn/go-isatty v0.0.20 // indirect
22+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
23+
github.com/modern-go/reflect2 v1.0.2 // indirect
24+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
25+
github.com/quic-go/qpack v0.5.1 // indirect
26+
github.com/quic-go/quic-go v0.54.0 // indirect
27+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
28+
github.com/ugorji/go/codec v1.3.0 // indirect
29+
go.uber.org/mock v0.5.0 // indirect
30+
golang.org/x/arch v0.20.0 // indirect
31+
golang.org/x/crypto v0.40.0 // indirect
32+
golang.org/x/mod v0.25.0 // indirect
33+
golang.org/x/net v0.42.0 // indirect
34+
golang.org/x/sync v0.16.0 // indirect
35+
golang.org/x/sys v0.35.0 // indirect
36+
golang.org/x/text v0.27.0 // indirect
37+
golang.org/x/tools v0.34.0 // indirect
38+
google.golang.org/protobuf v1.36.9 // indirect
39+
)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
2+
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
3+
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
4+
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
5+
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
6+
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
7+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
9+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
11+
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
12+
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
13+
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
14+
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
15+
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
16+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
17+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
18+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
19+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
20+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
21+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
22+
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
23+
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
24+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
25+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
26+
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
27+
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
28+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
29+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
30+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
31+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
32+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
33+
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
34+
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
35+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
36+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
37+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
38+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
39+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
40+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
41+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
42+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
43+
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
44+
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
45+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
47+
github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
48+
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
49+
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
50+
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
51+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
52+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
53+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
54+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
55+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
56+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
57+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
58+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
59+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
60+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
61+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
62+
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
63+
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
64+
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
65+
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
66+
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
67+
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
68+
golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM=
69+
golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY=
70+
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
71+
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
72+
golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs=
73+
golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8=
74+
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
75+
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
76+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
78+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
79+
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
80+
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
81+
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
82+
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
83+
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
84+
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
85+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
86+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
87+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
88+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

gin-json-abort/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2026 Hung T. Nguyen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

gin-json-abort/README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Abort gin request with JSON response
2+
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/nguyengg/go-aws-commons/gin-json-abort.svg)](https://pkg.go.dev/github.com/nguyengg/go-aws-commons/gin-json-abort)
4+
5+
```shell
6+
go get github.com/nguyengg/go-aws-commons/gin-json-abort
7+
```
8+
9+
Usage
10+
11+
```shell
12+
package main
13+
14+
import (
15+
"context"
16+
"fmt"
17+
"net/http"
18+
19+
"github.com/aws/aws-sdk-go-v2/aws"
20+
"github.com/aws/aws-sdk-go-v2/service/s3"
21+
"github.com/gin-gonic/gin"
22+
configcache "github.com/nguyengg/go-aws-commons/config-cache"
23+
"github.com/nguyengg/go-aws-commons/errors"
24+
ginmetrics "github.com/nguyengg/go-aws-commons/gin-metrics"
25+
preconditions "github.com/nguyengg/go-aws-commons/gin-preconditions"
26+
"github.com/nguyengg/go-aws-commons/gin-preconditions/s3headers"
27+
"github.com/nguyengg/go-aws-commons/must"
28+
)
29+
30+
func main() {
31+
ctx := context.Background()
32+
client := s3.NewFromConfig(configcache.MustGet(ctx))
33+
r := gin.New()
34+
r.Use(ginmetrics.Logger(ginmetrics.WithRequestId()))
35+
r.GET("/v1/:bucket/:key", func(c *gin.Context) {
36+
output := must.Must(client.GetObject(ctx, &s3.GetObjectInput{
37+
Bucket: aws.String(c.Param("bucket")),
38+
Key: aws.String(c.Param("key")),
39+
}))
40+
41+
// if user sets If-Modified-Since, check it so that we can avoid having to return the entire content.
42+
switch ignored, isModifiedSince := preconditions.IfModifiedSince(c, aws.ToTime(output.LastModified)); {
43+
case !ignored && !isModifiedSince:
44+
c.Status(http.StatusNotModified)
45+
return
46+
}
47+
48+
// If-Match is also supported, which is supposed to be "stronger" than If-Modified-Since.
49+
switch _, matches, err := preconditions.IfMatch(c, preconditions.NewStrongETag(*output.ETag)); {
50+
case !matches && err == nil:
51+
c.Status(http.StatusNotModified)
52+
return
53+
case err != nil:
54+
_ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("invalid If-Match: %v", err))
55+
return
56+
}
57+
58+
c.DataFromReader(200, *output.ContentLength, *output.ContentType, output.Body, nil)
59+
return
60+
})
61+
62+
// you can also use s3headers module to skip the GetObject call as well like this.
63+
// this makes the handler almost a proxy.
64+
r.GET("/v2/:bucket/:key", func(c *gin.Context) {
65+
input := &s3.GetObjectInput{
66+
Bucket: aws.String(c.Param("bucket")),
67+
Key: aws.String(c.Param("key")),
68+
}
69+
70+
// this will copy any If-Match, If-None-Match, etc. headers.
71+
s3headers.CopyToGetObjectInput(c, input)
72+
73+
output, err := client.GetObject(ctx, input)
74+
if errors.StatusCode(err) == http.StatusNotModified {
75+
c.Status(http.StatusNotFound)
76+
return
77+
}
78+
79+
// now copy the headers back to the response.
80+
s3headers.CopyFromGetObjectOutput(c, output)
81+
82+
c.DataFromReader(200, *output.ContentLength, *output.ContentType, output.Body, nil)
83+
return
84+
})
85+
}
86+
87+
```

0 commit comments

Comments
 (0)