Working json return for OSes list

This commit is contained in:
2024-03-15 15:05:11 +01:00
parent 895fdedfe5
commit 316a1996c2
2 changed files with 13 additions and 4 deletions

View File

@@ -1,6 +1,7 @@
package http
import (
"encoding/json"
"fmt"
db "infra-dashboard/Database"
opsys "infra-dashboard/OS"
@@ -21,16 +22,24 @@ func HealthHandler(w http.ResponseWriter, _ *http.Request) {
}
func GetOS(w http.ResponseWriter, r *http.Request) {
var list []opsys.OS
var err error
t := time.Now()
status := 200
db_conn := db.GetDatabaseConnection()
list, err := opsys.GetOS(db_conn)
defer db_conn.Close()
list, err = opsys.GetOS(db_conn)
if err != nil {
log.Println("Error getting OS list")
status = 500
}
log.Println("list: ", list)
logRequest(t, r, 200)
fmt.Fprint(w, "Hello, World!")
logRequest(t, r, status)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(list)
}
func logRequest(t time.Time, r *http.Request, s int) {