Comments (3)
the goal here is to return a response to a slack slash command immediately, then go off and do the work the user asked for and return those responses later via an http.Client.
things ive tried:
- c.writer.writeheader(http.statusok)
- c.writer.flush()
- c.Status(http.statusaccepted)
- using a copy of the context in the goroutine and calling Next() on that (no handler chain in copy)
middleware chaining is preferred because it simplifies the addition of new routes, all routes will be subject to the same flow (including responding quickly, then doing some async work).
from gin.
somehow even this executes functions in the same order:
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func middlewareOne() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middlewareOne")
}
}
func middlewareTwo() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middlewareTwo")
go func(ctx *gin.Context) {
runhandlers(ctx)
}(c)
c.AbortWithStatusJSON(http.StatusOK, gin.H{"message": "early response"})
}
}
func middlewareThree() func() {
return func() {
fmt.Println("middlewareThree")
}
}
func handler() func() {
return func() {
fmt.Println("handler")
}
}
func getmiddleware() []gin.HandlerFunc {
return []gin.HandlerFunc{middlewareOne(), middlewareTwo()}
}
func gethandlers() []func() {
return []func(){
handler(),
middlewareThree(),
}
}
func runhandlers(c *gin.Context) {
for _, handler := range gethandlers() {
handler()
}
}
func main() {
g := gin.Default()
g.GET("/example", getmiddleware()...)
g.Run(":8080")
}
with and without a time.Sleep
from gin.
so this works, though isn't ideal as we step out of gin for the bulk of the processing:
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func middlewareOne() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middlewareOne")
c.Set("dummy_key", "dummy_value")
}
}
func middlewareTwo() gin.HandlerFunc {
return func(c *gin.Context) {
fmt.Println("middlewareTwo")
go func(ctx *gin.Context) {
runhandlers(ctx)
}(c)
c.AbortWithStatusJSON(http.StatusOK, gin.H{"message": "early response"})
}
}
func middlewareThree(c *gin.Context) func() {
return func() {
fmt.Println(c.Get("dummy_key"))
fmt.Println("middlewareThree")
}
}
func handler(c *gin.Context) func() {
return func() {
fmt.Println(c.Get("dummy_key"))
fmt.Println("handler")
}
}
func getmiddleware() []gin.HandlerFunc {
return []gin.HandlerFunc{middlewareOne(), middlewareTwo()}
}
func gethandlers(c *gin.Context) []func() {
return []func(){
handler(c),
middlewareThree(c),
}
}
func runhandlers(c *gin.Context) {
for _, handler := range gethandlers(c) {
time.Sleep(2 * time.Second)
handler()
}
}
func main() {
g := gin.Default()
g.GET("/example", getmiddleware()...)
g.Run(":8080")
}
curl:
» curl localhost:8080/example
{"message":"early response"}%
log:
» go run example.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /example --> main.main.getmiddleware.middlewareTwo.func2 (4 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080
middlewareOne
middlewareTwo
[GIN] 2024/04/26 - 13:36:16 | 200 | 70.51µs | 127.0.0.1 | GET "/example"
dummy_value true
handler
dummy_value true
middlewareThree
can anyone think of a better way? i would much rather wire up the middleware as in the written example in the issue
from gin.
Related Issues (20)
- Multipart form-data request with empty key for single file upload
- gin with golang 1.21, 1.22 DATA RACE
- gin detects `DATA RACE` with Go1.21, Go1.22 HOT 8
- Please consider remove github.com/bytedance/sonic from dependencies HOT 4
- StringToBytes is slower than the raw convertion
- Bind recursion struct param fatal exit HOT 1
- Warning link doesn't point to anything anymore HOT 1
- verifying github.com/gin-gonic/[email protected]: checksum mismatch, how to fix it? HOT 2
- c.Data and c.DataFromReader overwrite the set header HOT 2
- Vulnerability in golang.org/x/net/http2 version < 0.23.0 HOT 3
- Errors encountered during rendering do not assign the type ErrorTypeRender HOT 2
- [QUESTION] Exclude request method from logger HOT 2
- context.Negotiate does not support application/yaml HOT 3
- Unnecessary engine.With(opts...) call HOT 5
- Update of Swagger document
- Can UnmarshalParam support custom slice of string
- With RedirectFixedPath enabled, the request execution panics when a 404 is expected HOT 1
- RunQUIC does not connect via browser HOT 2
- Why do long requests block each other? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.