add json method
This commit is contained in:
parent
a59ae6f1fb
commit
18d295c993
@ -21,3 +21,6 @@ Disallow: /
|
|||||||
|
|
||||||
User-agent: anthropic-ai
|
User-agent: anthropic-ai
|
||||||
Disallow: /
|
Disallow: /
|
||||||
|
|
||||||
|
User-agent: *
|
||||||
|
Allow: /
|
||||||
|
3
assets/html/txt.tplt
Normal file
3
assets/html/txt.tplt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{{- range .CVEs }}
|
||||||
|
{{ .CveID | ToUpper }} - {{ .Product }} - {{ .ShortDescription }}
|
||||||
|
{{- end }}
|
@ -1,6 +1,7 @@
|
|||||||
package webserver
|
package webserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -12,6 +13,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"istheinternetonfire.app/assets"
|
"istheinternetonfire.app/assets"
|
||||||
|
"istheinternetonfire.app/internal/cisa"
|
||||||
"istheinternetonfire.app/internal/config"
|
"istheinternetonfire.app/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -73,6 +75,8 @@ func Start() {
|
|||||||
IdleTimeout: time.Duration(config.Cfg.WebServerIdleTimeout) * time.Second,
|
IdleTimeout: time.Duration(config.Cfg.WebServerIdleTimeout) * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
path.HandleFunc("/status.json", webJSON)
|
||||||
|
path.HandleFunc("/status.txt", webTXT)
|
||||||
path.HandleFunc("/", webRoot)
|
path.HandleFunc("/", webRoot)
|
||||||
|
|
||||||
if err := connection.ListenAndServe(); err != nil {
|
if err := connection.ListenAndServe(); err != nil {
|
||||||
@ -118,3 +122,41 @@ func webRoot(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func webJSON(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if strings.ToLower(r.Method) != "get" {
|
||||||
|
config.Cfg.Log.Debug("http invalid method", "url", r.URL.Path, "expected", "GET", "received", r.Method)
|
||||||
|
tmpltError(w, http.StatusBadRequest, "Invalid http method.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Add("Content-Type", TYPE_APPLICATION_JSON)
|
||||||
|
|
||||||
|
var (
|
||||||
|
output []byte
|
||||||
|
num int = 3
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
cves := cisa.Read()
|
||||||
|
if len(cves.Vulnerabilities) == 0 {
|
||||||
|
w.Write([]byte("{}"))
|
||||||
|
return
|
||||||
|
} else if len(cves.Vulnerabilities) < 3 {
|
||||||
|
num = len(cves.Vulnerabilities)
|
||||||
|
}
|
||||||
|
|
||||||
|
if output, err = json.MarshalIndent(cves.Vulnerabilities[len(cves.Vulnerabilities)-num:], "", " "); err != nil {
|
||||||
|
config.Cfg.Log.Debug("unable to convert CVES to JSON", "error", err, "url", r.URL.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func webTXT(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if strings.ToLower(r.Method) != "get" {
|
||||||
|
config.Cfg.Log.Debug("http invalid method", "url", r.URL.Path, "expected", "GET", "received", r.Method)
|
||||||
|
tmpltError(w, http.StatusBadRequest, "Invalid http method.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -105,3 +105,7 @@ func tmpltStatusNotFound(w http.ResponseWriter, path string) {
|
|||||||
}
|
}
|
||||||
w.Write(msgBuffer.Bytes()) //nolint:errcheck
|
w.Write(msgBuffer.Bytes()) //nolint:errcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func tmpltTXT(w http.ResponseWriter) {
|
||||||
|
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user