package server import ( "fmt" "net/http" "forge.cadoles.com/arcad/arcast/pkg/network" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/api" "gitlab.com/wpetit/goweb/logger" ) type InfoResponse struct { IPs []string `json:"ips"` Port int `json:"port"` TLSPort int `json:"tlsPort"` InstanceID string `json:"instanceId"` AppsEnabled bool `json:"appsEnabled"` ServiceDiscoveryEnabled bool `json:"serviceDiscoveryEnabled"` } func (s *Server) handleInfo(w http.ResponseWriter, r *http.Request) { ips, err := network.GetLANIPv4Addrs() if err != nil { logger.Error(r.Context(), "could not retrieve lan ip addresses", logger.CapturedE(errors.WithStack(err))) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } api.DataResponse(w, http.StatusOK, &InfoResponse{ IPs: ips, TLSPort: s.tlsPort, Port: s.port, InstanceID: s.instanceID, AppsEnabled: s.appsEnabled, ServiceDiscoveryEnabled: s.serviceDiscoveryEnabled, }) } type CastRequest struct { URL string `json:"url" validate:"required"` } func (s *Server) handleCast(w http.ResponseWriter, r *http.Request) { req := &CastRequest{} if ok := api.Bind(w, r, req); !ok { return } if err := s.browser.Load(req.URL); err != nil { logger.Error(r.Context(), "could not load url", logger.CapturedE(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } http.Redirect(w, r, "/api/v1/status", http.StatusSeeOther) } func (s *Server) handleReset(w http.ResponseWriter, r *http.Request) { if err := s.resetBrowser(); err != nil { logger.Error(r.Context(), "could not unload url", logger.CapturedE(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } http.Redirect(w, r, "/api/v1/status", http.StatusSeeOther) } func (s *Server) resetBrowser() error { idleURL := fmt.Sprintf("http://localhost:%d", s.port) if err := s.browser.Reset(idleURL); err != nil { return errors.WithStack(err) } return nil } type StatusResponse struct { ID string `json:"id"` URL string `json:"url"` Status string `json:"status"` Title string `json:"title"` } func (s *Server) handleStatus(w http.ResponseWriter, r *http.Request) { url, err := s.browser.URL() if err != nil { logger.Error(r.Context(), "could not retrieve browser url", logger.CapturedE(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } status, err := s.browser.Status() if err != nil { logger.Error(r.Context(), "could not retrieve browser status", logger.CapturedE(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } title, err := s.browser.Title() if err != nil { logger.Error(r.Context(), "could not retrieve browser page title", logger.CapturedE(errors.WithStack(err))) api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil) return } api.DataResponse(w, http.StatusOK, &StatusResponse{ ID: s.instanceID, URL: url, Status: status.String(), Title: title, }) }