diff --git a/main.go b/main.go index c2a076d..d863ca4 100644 --- a/main.go +++ b/main.go @@ -157,10 +157,15 @@ func mainHandler(c *gin.Context) { } return case "headers": - c.JSON(200, c.Request.Header) + if wantsJS { + c.Writer.Header().Set("Content-Type", "application/javascript") + response, _ := json.Marshal(c.Request.Header) + c.String(200, "ifconfig_io = %v\n", string(response)) + } else { + c.JSON(200, c.Request.Header) + } return } - fieldResult, exists := c.Get(fields[0]) if !exists { c.String(404, "Not Found") @@ -168,7 +173,11 @@ func mainHandler(c *gin.Context) { } if wantsJSON { c.JSON(200, fieldResult) - } else { + } else if wantsJS { + c.Writer.Header().Set("Content-Type", "application/javascript") + response, _ := json.Marshal(map[string]interface{}{fields[0]: fieldResult}) + c.String(200, "ifconfig_io = %v\n", string(response)) + } else { c.String(200, fmt.Sprintln(fieldResult)) } @@ -194,8 +203,8 @@ func main() { } { r.GET(fmt.Sprintf("/%s", route), mainHandler) r.GET(fmt.Sprintf("/%s.json", route), mainHandler) + r.GET(fmt.Sprintf("/%s.js", route), mainHandler) } - r.GET("/all.js", mainHandler) r.GET("/", mainHandler) errc := make(chan error) diff --git a/web-service.systemd b/web-service.systemd new file mode 100644 index 0000000..4b577c4 --- /dev/null +++ b/web-service.systemd @@ -0,0 +1,24 @@ +[Unit] +Description=ifconfig.io web service +ConditionPathExists=/opt/ifconfig.io +After=network.target + +[Service] +Type=simple +User=root +Group=root +Environment="GIN_MODE=release" +Restart=on-failure +RestartSec=10 +startLimitIntervalSec=60 + +WorkingDirectory=/opt/ifconfig.io +ExecStart=/opt/ifconfig.io/server + +PermissionsStartOnly=true +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=web-server + +[Install] +WantedBy=multi-user.target