add json method
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package webserver
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"istheinternetonfire.app/assets"
|
||||
"istheinternetonfire.app/internal/cisa"
|
||||
"istheinternetonfire.app/internal/config"
|
||||
)
|
||||
|
||||
@@ -73,6 +75,8 @@ func Start() {
|
||||
IdleTimeout: time.Duration(config.Cfg.WebServerIdleTimeout) * time.Second,
|
||||
}
|
||||
|
||||
path.HandleFunc("/status.json", webJSON)
|
||||
path.HandleFunc("/status.txt", webTXT)
|
||||
path.HandleFunc("/", webRoot)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func tmpltTXT(w http.ResponseWriter) {
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user