adding endpoint /all.js

This commit is contained in:
stephanie 2022-01-04 06:53:48 -08:00 committed by George Shammas
parent cb6219d000
commit a5adaf7e50
1 changed files with 8 additions and 2 deletions

10
main.go
View File

@ -7,7 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"encoding/json"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
proxyproto "github.com/pires/go-proxyproto" proxyproto "github.com/pires/go-proxyproto"
) )
@ -130,6 +130,7 @@ func mainHandler(c *gin.Context) {
} }
wantsJSON := len(fields) >= 2 && fields[1] == "json" wantsJSON := len(fields) >= 2 && fields[1] == "json"
wantsJS := len(fields) >= 2 && fields[1] == "js"
switch fields[0] { switch fields[0] {
case "": case "":
@ -147,6 +148,10 @@ func mainHandler(c *gin.Context) {
case "all": case "all":
if wantsJSON { if wantsJSON {
c.JSON(200, c.Keys) c.JSON(200, c.Keys)
} else if wantsJS {
c.Writer.Header().Set("Content-Type", "application/javascript")
response, _ := json.Marshal(c.Keys)
c.String(200, "ifconfig_io = %v\n", string(response))
} else { } else {
c.String(200, "%v", c.Keys) c.String(200, "%v", c.Keys)
} }
@ -190,6 +195,7 @@ func main() {
r.GET(fmt.Sprintf("/%s", route), mainHandler) r.GET(fmt.Sprintf("/%s", route), mainHandler)
r.GET(fmt.Sprintf("/%s.json", route), mainHandler) r.GET(fmt.Sprintf("/%s.json", route), mainHandler)
} }
r.GET("/all.js", mainHandler)
r.GET("/", mainHandler) r.GET("/", mainHandler)
errc := make(chan error) errc := make(chan error)
@ -229,4 +235,4 @@ func main() {
func isReqFromCmdLine(ua string) bool { func isReqFromCmdLine(ua string) bool {
return strings.HasPrefix(ua, "curl") || strings.HasPrefix(ua, "HTTPie") return strings.HasPrefix(ua, "curl") || strings.HasPrefix(ua, "HTTPie")
} }