| 1 | |
| 2 | |
| 3 | |
| 4 | package server |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "net/http" |
| 12 | "strings" |
| 13 | |
| 14 | "github.com/lima-vm/lima/v2/pkg/driver" |
| 15 | "github.com/lima-vm/lima/v2/pkg/hostagent" |
| 16 | "github.com/lima-vm/lima/v2/pkg/httputil" |
| 17 | ) |
| 18 | |
| 19 | type Backend struct { |
| 20 | Agent *hostagent.HostAgent |
| 21 | } |
| 22 | |
| 23 | func (b *Backend) onError(w http.ResponseWriter, err error, ec int) { |
| 24 | |
| 25 | w.Header().Set("Content-Type", "application/json") |
| 26 | w.WriteHeader(ec) |
| 27 | |
| 28 | |
| 29 | e := httputil.ErrorJSON{ |
| 30 | Message: err.Error(), |
| 31 | } |
| 32 | _ = json.NewEncoder(w).Encode(e) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | func (b *Backend) GetInfo(w http.ResponseWriter, r *http.Request) { |
| 37 | if r.Method != http.MethodGet { |
| 38 | w.WriteHeader(http.StatusMethodNotAllowed) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | ctx := r.Context() |
| 43 | ctx, cancel := context.WithCancel(ctx) |
| 44 | defer cancel() |
| 45 | |
| 46 | info, err := b.Agent.Info(ctx) |
| 47 | if err != nil { |
| 48 | b.onError(w, err, http.StatusInternalServerError) |
| 49 | return |
| 50 | } |
| 51 | m, err := json.Marshal(info) |
| 52 | if err != nil { |
| 53 | b.onError(w, err, http.StatusInternalServerError) |
| 54 | return |
| 55 | } |
| 56 | w.Header().Set("Content-Type", "application/json") |
| 57 | w.WriteHeader(http.StatusOK) |
| 58 | _, _ = w.Write(m) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | func (b *Backend) GetScreenshot(w http.ResponseWriter, r *http.Request) { |
| 63 | if r.Method != http.MethodGet { |
| 64 | w.Header().Set("Allow", "GET") |
| 65 | w.WriteHeader(http.StatusMethodNotAllowed) |
| 66 | return |
| 67 | } |
| 68 | ctx := r.Context() |
| 69 | ctx, cancel := context.WithCancel(ctx) |
| 70 | defer cancel() |
| 71 | |
| 72 | format := strings.ToLower(r.URL.Query().Get("format")) |
| 73 | if format == "" { |
| 74 | format = "png" |
| 75 | } |
| 76 | if format != "png" && format != "bmp" { |
| 77 | b.onError(w, fmt.Errorf("unsupported format %q: must be png or bmp", format), http.StatusBadRequest) |
| 78 | return |
| 79 | } |
| 80 | data, err := b.Agent.Screenshot(ctx, format) |
| 81 | if err != nil { |
| 82 | ec := http.StatusInternalServerError |
| 83 | switch { |
| 84 | case errors.Is(err, driver.ErrDriverNotScreenshotter): |
| 85 | ec = http.StatusNotImplemented |
| 86 | case errors.Is(err, driver.ErrNoDisplay): |
| 87 | ec = http.StatusUnprocessableEntity |
| 88 | } |
| 89 | b.onError(w, err, ec) |
| 90 | return |
| 91 | } |
| 92 | ct := "image/png" |
| 93 | if format == "bmp" { |
| 94 | ct = "image/bmp" |
| 95 | } |
| 96 | w.Header().Set("Content-Type", ct) |
| 97 | w.WriteHeader(http.StatusOK) |
| 98 | _, _ = w.Write(data) |
| 99 | } |
| 100 | |
| 101 | func AddRoutes(r *http.ServeMux, b *Backend) { |
| 102 | r.Handle("/v1/info", http.HandlerFunc(b.GetInfo)) |
| 103 | r.Handle("/v1/screenshot", http.HandlerFunc(b.GetScreenshot)) |
| 104 | } |