This commit is contained in:
2024-02-24 11:40:26 +01:00
parent 55ce327ff5
commit 74432b1929
6 changed files with 144 additions and 35 deletions

View File

@@ -1,10 +1,12 @@
package api
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"nvidiadashboard/pkg/constant"
"nvidiadashboard/pkg/nvidia"
"os"
"path/filepath"
@@ -37,6 +39,7 @@ type (
Username string
DriverVersion string
CUDAVersion string
Version string
}
)
@@ -72,6 +75,7 @@ func New(repo NVIDIARepository) *Server {
s.mux.Use(middleware.URLFormat)
s.mux.Get("/", s.handleRoot)
s.mux.Get("/{uuid:GPU-(.*)}/json", s.handleGPUJSON)
s.mux.Get("/{uuid:GPU-(.*)}", s.handleGPU)
s.mux.Get("/{path:(.*).html}", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
@@ -95,7 +99,7 @@ func (s *Server) Serve(port uint) error {
func (s *Server) handleRoot(w http.ResponseWriter, _ *http.Request) {
gpus, err := s.repo.GetGPUs()
if err != nil {
sendInternalServerErrorHTML(w, err)
internalServerErrorHTML(w, err)
return
}
if len(gpus) > 0 {
@@ -105,11 +109,13 @@ func (s *Server) handleRoot(w http.ResponseWriter, _ *http.Request) {
}
wp := WebPack{
Username: "anonymous",
Version: constant.Version,
}
t, err := template.ParseFiles("static/no_gpu.html")
if err != nil {
sendInternalServerErrorHTML(w, err)
internalServerErrorHTML(w, err)
return
}
err = t.Execute(w, wp)
if err != nil {
@@ -122,7 +128,7 @@ func (s *Server) handleGPU(w http.ResponseWriter, r *http.Request) {
gpus, err := s.repo.GetGPUs()
if err != nil {
sendInternalServerErrorHTML(w, err)
internalServerErrorHTML(w, err)
return
}
i := -1
@@ -137,7 +143,7 @@ func (s *Server) handleGPU(w http.ResponseWriter, r *http.Request) {
}
gpu, err := s.repo.GetGPU(i)
if err != nil {
sendInternalServerErrorHTML(w, err)
internalServerErrorHTML(w, err)
return
}
wp := WebPack{
@@ -146,11 +152,13 @@ func (s *Server) handleGPU(w http.ResponseWriter, r *http.Request) {
GPU: gpu,
DriverVersion: s.repo.DriverVersion(),
CUDAVersion: s.repo.CUDAVersion(),
Version: constant.Version,
}
t, err := template.New("index").Funcs(templateFuncMap).ParseFiles("static/index.html")
if err != nil {
sendInternalServerErrorHTML(w, err)
internalServerErrorHTML(w, err)
return
}
err = t.ExecuteTemplate(w, "index.html", wp)
if err != nil {
@@ -158,6 +166,38 @@ func (s *Server) handleGPU(w http.ResponseWriter, r *http.Request) {
}
}
func (s *Server) handleGPUJSON(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
gpus, err := s.repo.GetGPUs()
if err != nil {
internalServerErrorJSON(w, err)
return
}
i := -1
for _, gpu := range gpus {
if gpu.UUID == uuid {
i = gpu.Index
}
}
if i == -1 {
notFoundJSON(w)
return
}
gpu, err := s.repo.GetGPU(i)
if err != nil {
internalServerErrorJSON(w, err)
return
}
body, err := json.Marshal(gpu)
if err != nil {
internalServerErrorJSON(w, err)
return
}
w.Header().Add("content-type", "application/json")
w.Write([]byte(body))
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func fileServer(r chi.Router, path string, root http.FileSystem) {
@@ -179,7 +219,7 @@ func fileServer(r chi.Router, path string, root http.FileSystem) {
})
}
func sendInternalServerErrorHTML(w http.ResponseWriter, v any) {
func internalServerErrorHTML(w http.ResponseWriter, v any) {
w.WriteHeader(http.StatusInternalServerError)
body := fmt.Sprintf(internalServerErrorPage, v)
w.Write([]byte(body))
@@ -191,6 +231,6 @@ func convertByteSize(v int) string {
}
func percentageRounded(a, b int) int {
p := (a / b) * 100
return p
p := (float64(a) / float64(b)) * 100
return int(p)
}

58
api/json.go Normal file
View File

@@ -0,0 +1,58 @@
package api
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
type (
JSONBase struct {
Status int `json:"status"`
Timestamp time.Time `json:"timestamp"`
}
JSONError struct {
JSONBase
Message string
}
)
var (
errLog = log.New(os.Stderr, log.Prefix(), log.Flags())
)
func internalServerErrorJSON(w http.ResponseWriter, v any) {
w.WriteHeader(http.StatusInternalServerError)
body, err := json.Marshal(JSONError{
JSONBase: JSONBase{
Status: http.StatusInternalServerError,
Timestamp: time.Now(),
},
Message: fmt.Sprintf("%v", v),
})
if err != nil {
errLog.Println("[ERROR]", err)
}
w.Write([]byte(body))
}
func notFoundJSON(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
body, err := json.Marshal(JSONError{
JSONBase: JSONBase{
Status: http.StatusNotFound,
Timestamp: time.Now(),
},
Message: "404 page not found",
})
if err != nil {
errLog.Println("[ERROR]", err)
}
w.Write([]byte(body))
}