Working json return for OSes list
This commit is contained in:
parent
895fdedfe5
commit
316a1996c2
@ -1,6 +1,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
db "infra-dashboard/Database"
|
db "infra-dashboard/Database"
|
||||||
opsys "infra-dashboard/OS"
|
opsys "infra-dashboard/OS"
|
||||||
@ -21,16 +22,24 @@ func HealthHandler(w http.ResponseWriter, _ *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetOS(w http.ResponseWriter, r *http.Request) {
|
func GetOS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var list []opsys.OS
|
||||||
|
var err error
|
||||||
|
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
|
status := 200
|
||||||
|
|
||||||
db_conn := db.GetDatabaseConnection()
|
db_conn := db.GetDatabaseConnection()
|
||||||
list, err := opsys.GetOS(db_conn)
|
defer db_conn.Close()
|
||||||
|
list, err = opsys.GetOS(db_conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting OS list")
|
log.Println("Error getting OS list")
|
||||||
|
status = 500
|
||||||
}
|
}
|
||||||
log.Println("list: ", list)
|
log.Println("list: ", list)
|
||||||
|
|
||||||
logRequest(t, r, 200)
|
logRequest(t, r, status)
|
||||||
fmt.Fprint(w, "Hello, World!")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(list)
|
||||||
}
|
}
|
||||||
|
|
||||||
func logRequest(t time.Time, r *http.Request, s int) {
|
func logRequest(t time.Time, r *http.Request, s int) {
|
||||||
|
|||||||
@ -28,5 +28,5 @@ func GetOS(db *sql.DB) ([]OS, error) {
|
|||||||
}
|
}
|
||||||
oses = append(oses, os)
|
oses = append(oses, os)
|
||||||
}
|
}
|
||||||
return oses, nil
|
return oses, err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user