57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
db "infra-dashboard/Database"
|
|
opsys "infra-dashboard/OS"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func RequestHandler(w http.ResponseWriter, r *http.Request) {
|
|
t := time.Now()
|
|
logRequest(t, r, 200)
|
|
fmt.Fprint(w, "Hello, World!")
|
|
}
|
|
|
|
func HealthHandler(w http.ResponseWriter, _ *http.Request) {
|
|
_, _ = io.WriteString(w, "OK")
|
|
}
|
|
|
|
func GetOS(w http.ResponseWriter, r *http.Request) {
|
|
var list []opsys.OS
|
|
var err error
|
|
|
|
t := time.Now()
|
|
status := 200
|
|
|
|
db_conn := db.GetDatabaseConnection()
|
|
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, status)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(list)
|
|
}
|
|
|
|
func logRequest(t time.Time, r *http.Request, s int) {
|
|
log.Printf("%s - - %s \"%s %s %s\" %d 0 \"-\" \"%s\" %d\n",
|
|
r.Host,
|
|
t.Format("[02/Jan/2006:15:04:05 -0700]"),
|
|
r.Method,
|
|
r.URL.Path,
|
|
r.Proto,
|
|
s,
|
|
r.UserAgent(),
|
|
time.Since(t).Milliseconds(),
|
|
)
|
|
}
|