Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add samples of proxy middleware #104

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
}