first commit
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
module gitverse.ru/andoma/gin-contrib/examples
|
||||
|
||||
go 1.21.5
|
||||
|
||||
replace gitverse.ru/andoma/gin-contrib => ../
|
||||
|
||||
require (
|
||||
gitverse.ru/andoma/gin-contrib v0.0.0-00010101000000-000000000000
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.10.2 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
||||
github.com/chenzhuoyu/iasm v0.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.18.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
golang.org/x/arch v0.7.0 // indirect
|
||||
golang.org/x/crypto v0.19.0 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
golang.org/x/sys v0.17.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/protobuf v1.33.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/gzip"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
r.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/nocache"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
router.Use(nocache.NoCache())
|
||||
router.Run(":8080")
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/pprof"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
adminGroup := router.Group("/admin", func(c *gin.Context) {
|
||||
if c.Request.Header.Get("Authorization") != "foobar" {
|
||||
c.AbortWithStatus(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
pprof.RouteRegister(adminGroup, "pprof")
|
||||
router.Run(":8080")
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/pprof"
|
||||
)
|
||||
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
pprof.Register(router)
|
||||
router.Run(":8080")
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/requestid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.New()
|
||||
|
||||
r.Use(requestid.New())
|
||||
|
||||
// Example ping request.
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Example / request.
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "id:"+requestid.Get(c))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/requestid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.New()
|
||||
|
||||
r.Use(
|
||||
requestid.New(
|
||||
requestid.WithGenerator(func() string {
|
||||
return "test"
|
||||
}),
|
||||
requestid.WithCustomHeaderStrKey("your-customer-key"),
|
||||
requestid.WithHandler(func(c *gin.Context, requestID string) {
|
||||
log.Printf("RequestID: %s", requestID)
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
// Example ping request.
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Example / request.
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "id:"+requestid.Get(c))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/secure"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(secure.Secure(secure.Options{
|
||||
AllowedHosts: []string{"example.com", "ssl.example.com"},
|
||||
SSLRedirect: true,
|
||||
SSLHost: "ssl.example.com",
|
||||
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
||||
STSSeconds: 315360000,
|
||||
STSIncludeSubdomains: true,
|
||||
FrameDeny: true,
|
||||
ContentTypeNosniff: true,
|
||||
BrowserXssFilter: true,
|
||||
ContentSecurityPolicy: "default-src 'self'",
|
||||
}))
|
||||
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/static"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// if Allow DirectoryIndex
|
||||
// r.Use(static.Serve("/", static.LocalFile("/tmp", true)))
|
||||
// set prefix
|
||||
// r.Use(static.Serve("/static", static.LocalFile("/tmp", true)))
|
||||
|
||||
r.Use(static.Serve("/", static.LocalFile("/tmp", false)))
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "test")
|
||||
})
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/timeout"
|
||||
)
|
||||
|
||||
func emptySuccessResponse(c *gin.Context) {
|
||||
time.Sleep(200 * time.Microsecond)
|
||||
c.String(http.StatusOK, "")
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.New()
|
||||
|
||||
r.GET("/", timeout.New(
|
||||
timeout.WithTimeout(100*time.Microsecond),
|
||||
timeout.WithHandler(emptySuccessResponse),
|
||||
))
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitverse.ru/andoma/gin"
|
||||
"gitverse.ru/andoma/gin-contrib/timeout"
|
||||
)
|
||||
|
||||
func testResponse(c *gin.Context) {
|
||||
c.String(http.StatusRequestTimeout, "timeout")
|
||||
}
|
||||
|
||||
func timeoutMiddleware() gin.HandlerFunc {
|
||||
return timeout.New(
|
||||
timeout.WithTimeout(500*time.Millisecond),
|
||||
timeout.WithHandler(func(c *gin.Context) {
|
||||
c.Next()
|
||||
}),
|
||||
timeout.WithResponse(testResponse),
|
||||
)
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := gin.New()
|
||||
r.Use(timeoutMiddleware())
|
||||
r.GET("/slow", func(c *gin.Context) {
|
||||
time.Sleep(800 * time.Millisecond)
|
||||
c.Status(http.StatusOK)
|
||||
})
|
||||
if err := r.Run(":8080"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user