Added Javascript endpoints and systemd service example

This commit is contained in:
Stephanie Sunshine 2022-01-04 13:16:53 -08:00 committed by George Shammas
parent a5adaf7e50
commit 984be2f57e
2 changed files with 37 additions and 4 deletions

17
main.go
View File

@ -157,10 +157,15 @@ func mainHandler(c *gin.Context) {
} }
return return
case "headers": 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 return
} }
fieldResult, exists := c.Get(fields[0]) fieldResult, exists := c.Get(fields[0])
if !exists { if !exists {
c.String(404, "Not Found") c.String(404, "Not Found")
@ -168,7 +173,11 @@ func mainHandler(c *gin.Context) {
} }
if wantsJSON { if wantsJSON {
c.JSON(200, fieldResult) 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)) c.String(200, fmt.Sprintln(fieldResult))
} }
@ -194,8 +203,8 @@ 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(fmt.Sprintf("/%s.js", route), mainHandler)
} }
r.GET("/all.js", mainHandler)
r.GET("/", mainHandler) r.GET("/", mainHandler)
errc := make(chan error) errc := make(chan error)

24
web-service.systemd Normal file
View File

@ -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