Coder Social home page Coder Social logo

Comments (3)

daveoy avatar daveoy commented on June 18, 2024

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.

daveoy avatar daveoy commented on June 18, 2024

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.

daveoy avatar daveoy commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.