From 634de9a907fce944ac6a457b82a6af3422e2dfe4 Mon Sep 17 00:00:00 2001 From: zjregee Date: Sat, 17 Aug 2024 09:09:06 +0000 Subject: [PATCH] feat: add basic middleware --- .github/workflows/code_check.yml | 2 +- .github/workflows/e2e_test.yml | 2 +- .github/workflows/unit_test.yml | 2 +- ahttp/middleware/logger.go | 17 ++++++++++++++++ ahttp/middleware/recovery.go | 34 ++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 ahttp/middleware/logger.go create mode 100644 ahttp/middleware/recovery.go diff --git a/.github/workflows/code_check.yml b/.github/workflows/code_check.yml index 5f33e69..b12c369 100644 --- a/.github/workflows/code_check.yml +++ b/.github/workflows/code_check.yml @@ -17,7 +17,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: stable + go-version: 1.22.0 - name: Install Dependencies run: | diff --git a/.github/workflows/e2e_test.yml b/.github/workflows/e2e_test.yml index 6953c0e..5ccaf49 100644 --- a/.github/workflows/e2e_test.yml +++ b/.github/workflows/e2e_test.yml @@ -17,7 +17,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: stable + go-version: 1.22.0 - name: Install Dependencies run: | diff --git a/.github/workflows/unit_test.yml b/.github/workflows/unit_test.yml index dc9f9d3..31d7483 100644 --- a/.github/workflows/unit_test.yml +++ b/.github/workflows/unit_test.yml @@ -17,7 +17,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: stable + go-version: 1.22.0 - name: Install Dependencies run: | diff --git a/ahttp/middleware/logger.go b/ahttp/middleware/logger.go new file mode 100644 index 0000000..3ebb0c0 --- /dev/null +++ b/ahttp/middleware/logger.go @@ -0,0 +1,17 @@ +package middleware + +import ( + "fmt" + + "github.com/zjregee/anet/ahttp" +) + +func Logger() ahttp.MiddlewareFunc { + return func(next ahttp.HandlerFunc) ahttp.HandlerFunc { + return func(c *ahttp.Context) (returnErr error) { + returnErr = next(c) + fmt.Printf("[MIDDLEWARE LOGGER]: %s %s %v\n", c.Request().Method, c.Request().URL.Path, returnErr) + return + } + } +} diff --git a/ahttp/middleware/recovery.go b/ahttp/middleware/recovery.go new file mode 100644 index 0000000..dad3a49 --- /dev/null +++ b/ahttp/middleware/recovery.go @@ -0,0 +1,34 @@ +package middleware + +import ( + "fmt" + "net/http" + "runtime" + + "github.com/zjregee/anet/ahttp" +) + +const stackSize = 4 * 1024 +const disableStackAll = false + +func Recover() ahttp.MiddlewareFunc { + return func(next ahttp.HandlerFunc) ahttp.HandlerFunc { + return func(c *ahttp.Context) (returnErr error) { + defer func() { + if r := recover(); r != nil { + err, ok := r.(error) + if !ok { + err = fmt.Errorf("%v", r) + } + stack := make([]byte, stackSize) + len := runtime.Stack(stack, !disableStackAll) + stack = stack[:len] + fmt.Printf("[MIDDLEWARE PANIC RECOVER]: %v %s\n", err, stack) + c.Response().WriteHeader(http.StatusInternalServerError) + _, returnErr = c.Response().Write(nil) + } + }() + return next(c) + } + } +}