-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |