GoWeb: Remove everything to use Gin
This commit is contained in:
		@ -3,19 +3,18 @@ package middleware
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"git.vinchent.xyz/vinchent/go-web/framework"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Recovery is a middleware that recovers from the panic
 | 
			
		||||
func Recovery() framework.ControllerHandler {
 | 
			
		||||
	return func(c *framework.Context) error {
 | 
			
		||||
func Recovery() gin.HandlerFunc {
 | 
			
		||||
	return func(c *gin.Context) {
 | 
			
		||||
		defer func() {
 | 
			
		||||
			if err := recover(); err != nil {
 | 
			
		||||
				c.WriteJSON(http.StatusInternalServerError, err)
 | 
			
		||||
				c.JSON(http.StatusInternalServerError, err)
 | 
			
		||||
			}
 | 
			
		||||
		}()
 | 
			
		||||
 | 
			
		||||
		c.Next()
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -3,32 +3,29 @@ package middleware
 | 
			
		||||
import (
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"git.vinchent.xyz/vinchent/go-web/framework"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Test1() framework.ControllerHandler {
 | 
			
		||||
	return func(c *framework.Context) error {
 | 
			
		||||
func Test1() gin.HandlerFunc {
 | 
			
		||||
	return func(c *gin.Context) {
 | 
			
		||||
		log.Println("middleware test1 pre")
 | 
			
		||||
		c.Next()
 | 
			
		||||
		log.Println("middleware test1 post")
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Test2() framework.ControllerHandler {
 | 
			
		||||
	return func(c *framework.Context) error {
 | 
			
		||||
func Test2() gin.HandlerFunc {
 | 
			
		||||
	return func(c *gin.Context) {
 | 
			
		||||
		log.Println("middleware test2 pre")
 | 
			
		||||
		c.Next()
 | 
			
		||||
		log.Println("middleware test2 post")
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Test3() framework.ControllerHandler {
 | 
			
		||||
	return func(c *framework.Context) error {
 | 
			
		||||
func Test3() gin.HandlerFunc {
 | 
			
		||||
	return func(c *gin.Context) {
 | 
			
		||||
		log.Println("middleware test3 pre")
 | 
			
		||||
		c.Next()
 | 
			
		||||
		log.Println("middleware test3 post")
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,15 +6,15 @@ import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.vinchent.xyz/vinchent/go-web/framework"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Timeout(d time.Duration) framework.ControllerHandler {
 | 
			
		||||
	return func(c *framework.Context) error {
 | 
			
		||||
func Timeout(d time.Duration) gin.HandlerFunc {
 | 
			
		||||
	return func(c *gin.Context) {
 | 
			
		||||
		finish := make(chan struct{}, 1)
 | 
			
		||||
		panicChan := make(chan interface{}, 1)
 | 
			
		||||
 | 
			
		||||
		durationCtx, cancel := context.WithTimeout(c.BaseContext(), d)
 | 
			
		||||
		durationCtx, cancel := context.WithTimeout(c.Request.Context(), d)
 | 
			
		||||
		defer cancel()
 | 
			
		||||
 | 
			
		||||
		go func() {
 | 
			
		||||
@ -35,15 +35,12 @@ func Timeout(d time.Duration) framework.ControllerHandler {
 | 
			
		||||
		case p := <-panicChan:
 | 
			
		||||
			// panic
 | 
			
		||||
			log.Println(p)
 | 
			
		||||
			c.GetResponseWriter().WriteHeader(http.StatusInternalServerError)
 | 
			
		||||
			c.Status(http.StatusInternalServerError)
 | 
			
		||||
		case <-finish:
 | 
			
		||||
			// finish normally
 | 
			
		||||
			log.Println("finish")
 | 
			
		||||
		case <-durationCtx.Done():
 | 
			
		||||
			c.SetHasTimeout()
 | 
			
		||||
			c.GetResponseWriter().WriteHeader(http.StatusRequestTimeout)
 | 
			
		||||
			c.GetResponseWriter().Write([]byte("time out"))
 | 
			
		||||
			c.JSON(http.StatusRequestTimeout, "time out")
 | 
			
		||||
		}
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -8,30 +8,28 @@ import (
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.vinchent.xyz/vinchent/go-web/framework"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestTimeout(t *testing.T) {
 | 
			
		||||
	t.Run("Test timeout handler", func(t *testing.T) {
 | 
			
		||||
		timeoutHandler := Timeout(1 * time.Millisecond)
 | 
			
		||||
 | 
			
		||||
		longHandler := func(c *framework.Context) error {
 | 
			
		||||
		longHandler := func(c *gin.Context) {
 | 
			
		||||
			time.Sleep(2 * time.Millisecond)
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		res := prepareMiddlewareTest(t, timeoutHandler, longHandler)
 | 
			
		||||
 | 
			
		||||
		assertCode(t, res.StatusCode, http.StatusRequestTimeout)
 | 
			
		||||
		assertBody(t, res.Body, "time out")
 | 
			
		||||
		assertBody(t, res.Body, "\"time out\"")
 | 
			
		||||
	})
 | 
			
		||||
	t.Run("Test no timeout", func(t *testing.T) {
 | 
			
		||||
		timeoutHandler := Timeout(2 * time.Millisecond)
 | 
			
		||||
 | 
			
		||||
		quickHandler := func(c *framework.Context) error {
 | 
			
		||||
		quickHandler := func(c *gin.Context) {
 | 
			
		||||
			// time.Sleep(1 * time.Millisecond)
 | 
			
		||||
			c.WriteJSON(http.StatusOK, "ok")
 | 
			
		||||
			return nil
 | 
			
		||||
			c.JSON(http.StatusOK, "ok")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		res := prepareMiddlewareTest(t, timeoutHandler, quickHandler)
 | 
			
		||||
@ -44,7 +42,7 @@ func TestRecover(t *testing.T) {
 | 
			
		||||
	t.Run("Test panic", func(t *testing.T) {
 | 
			
		||||
		recoverer := Recovery()
 | 
			
		||||
 | 
			
		||||
		panicHandler := func(c *framework.Context) error {
 | 
			
		||||
		panicHandler := func(c *gin.Context) {
 | 
			
		||||
			panic("panic")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -55,9 +53,8 @@ func TestRecover(t *testing.T) {
 | 
			
		||||
	t.Run("Test no panic", func(t *testing.T) {
 | 
			
		||||
		recoverer := Recovery()
 | 
			
		||||
 | 
			
		||||
		normalHandler := func(c *framework.Context) error {
 | 
			
		||||
			c.WriteJSON(http.StatusOK, "ok")
 | 
			
		||||
			return nil
 | 
			
		||||
		normalHandler := func(c *gin.Context) {
 | 
			
		||||
			c.JSON(http.StatusOK, "ok")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		res := prepareMiddlewareTest(t, recoverer, normalHandler)
 | 
			
		||||
@ -68,20 +65,18 @@ func TestRecover(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
func prepareMiddlewareTest(
 | 
			
		||||
	t testing.TB,
 | 
			
		||||
	mid framework.ControllerHandler,
 | 
			
		||||
	in framework.ControllerHandler,
 | 
			
		||||
	mid gin.HandlerFunc,
 | 
			
		||||
	in gin.HandlerFunc,
 | 
			
		||||
) *http.Response {
 | 
			
		||||
	t.Helper()
 | 
			
		||||
	request := httptest.NewRequest(http.MethodGet, "/", nil)
 | 
			
		||||
	response := httptest.NewRecorder()
 | 
			
		||||
	c := framework.NewContext(response, request)
 | 
			
		||||
	_, r := gin.CreateTestContext(response)
 | 
			
		||||
 | 
			
		||||
	c.SetHandlers([]framework.ControllerHandler{in})
 | 
			
		||||
	r.Use(mid)
 | 
			
		||||
	r.GET("/", in)
 | 
			
		||||
 | 
			
		||||
	err := mid(c)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	r.ServeHTTP(response, request)
 | 
			
		||||
 | 
			
		||||
	res := response.Result()
 | 
			
		||||
	return res
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user