From 093419252dd622994d5dcb16249bd6b611159d34 Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 9 Mar 2020 14:52:10 +0100 Subject: [PATCH] added /json routing endpoint --- cmd/dip/dip.go | 2 ++ src/page/main.go | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/dip/dip.go b/cmd/dip/dip.go index 83b5a84..c78685b 100644 --- a/cmd/dip/dip.go +++ b/cmd/dip/dip.go @@ -28,6 +28,7 @@ func main() { var host, port string var p page.Page + p.Title = "Public IP Address Service" utils.Flags(&host, &port) @@ -42,6 +43,7 @@ func main() { e.GET("/static/*", func(c echo.Context) error { return static.GetStatic(staticbox, c) }) + e.GET("/json", p.JSON) e.OPTIONS("/", echo.MethodNotAllowedHandler) e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", host, port))) diff --git a/src/page/main.go b/src/page/main.go index 8d4775b..45b8a4a 100644 --- a/src/page/main.go +++ b/src/page/main.go @@ -9,18 +9,27 @@ import ( "github.com/labstack/echo/v4" ) -// Index returns main webpage -func (p *Page) Index(c echo.Context) (err error) { +func (p *Page) GetContent(c echo.Context) (err error) { var ip ip.IP ip.GetIPInfo(c) - p.IP = &ip + return +} + +// Index returns main webpage +func (p *Page) Index(c echo.Context) (err error) { + p.GetContent(c) if strings.Contains(c.Request().Header.Get(echo.HeaderAccept), echo.MIMETextHTML) { return c.Render(http.StatusOK, "index.html", p) } - return c.JSONPretty(http.StatusOK, ip, " ") + return c.JSONPretty(http.StatusOK, p.IP, " ") +} + +func (p *Page) JSON(c echo.Context) (err error) { + p.GetContent(c) + return c.JSONPretty(http.StatusOK, p.IP, " ") } // Page defines Web page