package client import ( "encoding/json" "errors" "net/http" "git.faercol.me/faercol/http-boot-server/bootserver/bootoption" "git.faercol.me/faercol/http-boot-server/bootserver/config" "git.faercol.me/faercol/http-boot-server/bootserver/helpers" "git.faercol.me/faercol/http-boot-server/bootserver/services" "github.com/google/uuid" "github.com/sirupsen/logrus" ) const ConfigRoute = "/config" type clientNetConfig struct { MulticastGroup string `json:"multicast_group"` Port int `json:"port"` } type clientConfig struct { EFIConfig *bootoption.Client `json:"efi_config"` NetConfig *clientNetConfig `json:"net_config"` } type GetConfigController struct { clientService *services.ClientHandlerService config *config.AppConfig l *logrus.Logger } func NewGetConfigController(l *logrus.Logger, service *services.ClientHandlerService, config *config.AppConfig) *GetConfigController { return &GetConfigController{ clientService: service, l: l, config: config, } } func (cc *GetConfigController) getConfig(w http.ResponseWriter, r *http.Request) (int, []byte, error) { if r.Method != http.MethodGet { return http.StatusMethodNotAllowed, nil, nil } id := r.URL.Query().Get("id") parsedId, err := uuid.Parse(id) if err != nil { cc.l.Errorf("Invalid uuid %q", id) return http.StatusBadRequest, []byte("invalid uuid format"), errors.New("invalid uuid format") } efiConfig, err := cc.clientService.GetClientConfig(parsedId) if err != nil { if errors.Is(err, services.ErrUnknownClient) { cc.l.Errorf("Unknown client %q", id) return http.StatusNotFound, []byte("unknown client"), err } else { cc.l.Errorf("Unexpected error getting config for client: %s", err.Error()) return http.StatusInternalServerError, nil, err } } cltConfig := clientConfig{ EFIConfig: efiConfig, NetConfig: &clientNetConfig{ MulticastGroup: cc.config.UPDMcastGroup, Port: cc.config.UDPPort, }, } jsonDat, err := json.Marshal(cltConfig) if err != nil { cc.l.Errorf("Failed to serialize EFI config: %s", err.Error()) return http.StatusInternalServerError, nil, err } w.Header().Add("Content-Type", "application/json") return http.StatusOK, jsonDat, nil } func (cc *GetConfigController) ServeHTTP(w http.ResponseWriter, r *http.Request) { returncode, content, err := cc.getConfig(w, r) if err != nil { cc.l.Errorf("Error getting client config: %s", err.Error()) } helpers.HandleResponse(w, r, returncode, content, cc.l) }