Skip to content

Commit

Permalink
Add samples of proxy middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Jul 30, 2024
2 parents a595b13 + d2e829c commit b24379b
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pkg/example/proxy/proxy-server-regex-rewrite/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"bytes"
"io"
"log"
"net/http"
"net/url"
"regexp"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

func main() {
e := echo.New()

target := "http://localhost:8080" // 대상 서버 URL (Tumblebug 서버)
url, err := url.Parse(target)
if err != nil {
e.Logger.Fatal(err)
}

// 정규 표현식 경로 재작성 설정
regexRewrite := map[*regexp.Regexp]string{
regexp.MustCompile(`^/beetle/ns$`): "/tumblebug/ns",
regexp.MustCompile(`^/beetle/ns/([^/]+)$`): "/tumblebug/ns/$1",
regexp.MustCompile(`^/beetle/ns/([^/]+)/mcis`): "",
}

e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
Balancer: middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{
{
URL: url,
},
}),
RegexRewrite: regexRewrite,
ModifyResponse: func(res *http.Response) error {
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

log.Printf("Response from target: %s", string(body))

res.Body = io.NopCloser(bytes.NewReader(body))
return nil
},
}))

// 무시할 경로에 대한 핸들러 추가
e.Any("/beetle/ns/:nsId/mcis/*", func(c echo.Context) error {
return c.String(http.StatusNotFound, "Not Found")
})

e.Logger.Fatal(e.Start(":1323"))
}
59 changes: 59 additions & 0 deletions pkg/example/proxy/proxy-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"bytes"
"io"
"log"
"net/http"
"net/url"
"strings"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

func main() {
e := echo.New()

target := "http://localhost:8080" // target server url
url, err := url.Parse(target)
if err != nil {
e.Logger.Fatal(err)
}

e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{
Balancer: middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{
{
URL: url,
},
}),
Skipper: func(c echo.Context) bool {
// Skip url patterns that start with /beetle/ns/mcis
path := c.Request().URL.Path
if strings.HasPrefix(path, "/beetle/ns/") {
parts := strings.Split(path, "/")
if len(parts) > 3 && parts[3] == "mcis" {
return true
}
}
return false
},
Rewrite: map[string]string{
"/beetle/ns": "/tumblebug/ns",
"/beetle/ns/*": "/tumblebug/ns/$1",
},
ModifyResponse: func(res *http.Response) error {
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

log.Printf("Response from target: %s", string(body))

res.Body = io.NopCloser(bytes.NewReader(body))
return nil
},
}))

e.Logger.Fatal(e.Start(":1323"))
}
37 changes: 37 additions & 0 deletions pkg/example/proxy/target-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"net/http"

"github.com/labstack/echo/v4"
)

func main() {
e := echo.New()

// Pseudo Tumblebug API
e.GET("/tumblebug/ns", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"message": "GET /tumblebug/ns"})
})

e.POST("/tumblebug/ns", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"message": "POST /tumblebug/ns"})
})

e.DELETE("/tumblebug/ns/:nsId", func(c echo.Context) error {
nsId := c.Param("nsId")
return c.JSON(http.StatusOK, map[string]string{"message": "DELETE /tumblebug/ns/" + nsId})
})

e.GET("/tumblebug/ns/:nsId", func(c echo.Context) error {
nsId := c.Param("nsId")
return c.JSON(http.StatusOK, map[string]string{"message": "GET /tumblebug/ns/" + nsId})
})

e.PUT("/tumblebug/ns/:nsId", func(c echo.Context) error {
nsId := c.Param("nsId")
return c.JSON(http.StatusOK, map[string]string{"message": "PUT /tumblebug/ns/" + nsId})
})

e.Logger.Fatal(e.Start(":8080"))
}

0 comments on commit b24379b

Please sign in to comment.