added /json routing endpoint

This commit is contained in:
Paul 2020-03-09 14:52:10 +01:00
parent e30e37da24
commit 093419252d
2 changed files with 15 additions and 4 deletions

View File

@ -28,6 +28,7 @@ func main() {
var host, port string var host, port string
var p page.Page var p page.Page
p.Title = "Public IP Address Service" p.Title = "Public IP Address Service"
utils.Flags(&host, &port) utils.Flags(&host, &port)
@ -42,6 +43,7 @@ func main() {
e.GET("/static/*", func(c echo.Context) error { e.GET("/static/*", func(c echo.Context) error {
return static.GetStatic(staticbox, c) return static.GetStatic(staticbox, c)
}) })
e.GET("/json", p.JSON)
e.OPTIONS("/", echo.MethodNotAllowedHandler) e.OPTIONS("/", echo.MethodNotAllowedHandler)
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", host, port))) e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", host, port)))

View File

@ -9,18 +9,27 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
// Index returns main webpage func (p *Page) GetContent(c echo.Context) (err error) {
func (p *Page) Index(c echo.Context) (err error) {
var ip ip.IP var ip ip.IP
ip.GetIPInfo(c) ip.GetIPInfo(c)
p.IP = &ip 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) { if strings.Contains(c.Request().Header.Get(echo.HeaderAccept), echo.MIMETextHTML) {
return c.Render(http.StatusOK, "index.html", p) 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 // Page defines Web page