2023-07-24 20:56:10 +00:00
|
|
|
package client
|
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// import (
|
|
|
|
// "encoding/json"
|
|
|
|
// "fmt"
|
|
|
|
// "io"
|
|
|
|
// "net"
|
|
|
|
// "net/http"
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// "git.faercol.me/faercol/http-boot-server/bootserver/helpers"
|
|
|
|
// "git.faercol.me/faercol/http-boot-server/bootserver/services"
|
|
|
|
// "github.com/sirupsen/logrus"
|
|
|
|
// )
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// const BootRoute = "/boot"
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// type BootController struct {
|
|
|
|
// clientService *services.ClientHandlerService
|
|
|
|
// l *logrus.Logger
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// func NewBootController(logger *logrus.Logger, service *services.ClientHandlerService) *BootController {
|
|
|
|
// return &BootController{
|
|
|
|
// clientService: service,
|
|
|
|
// l: logger,
|
|
|
|
// }
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// func (bc *BootController) getBootOption(clientIP string, w http.ResponseWriter, r *http.Request) (int, []byte, error) {
|
|
|
|
// bootOption, err := bc.clientService.GetClientSelectedBootOption(clientIP)
|
|
|
|
// if err != nil {
|
|
|
|
// return http.StatusInternalServerError, nil, fmt.Errorf("failed to get boot option: %w", err)
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// dat, err := json.Marshal(bootOption)
|
|
|
|
// if err != nil {
|
|
|
|
// return http.StatusInternalServerError, nil, fmt.Errorf("failed to serialize body")
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// w.Header().Add("Content-Type", "application/json")
|
|
|
|
// return http.StatusOK, dat, nil
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// func (bc *BootController) setBootOption(clientIP string, w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
// dat, err := io.ReadAll(r.Body)
|
|
|
|
// if err != nil {
|
|
|
|
// return http.StatusInternalServerError, fmt.Errorf("failed to read body: %w", err)
|
|
|
|
// }
|
|
|
|
// var option string
|
|
|
|
// if err := json.Unmarshal(dat, &option); err != nil {
|
|
|
|
// return http.StatusInternalServerError, fmt.Errorf("failed to parse body: %w", err)
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// if err := bc.clientService.SetClientBootOption(clientIP, option); err != nil {
|
|
|
|
// return http.StatusInternalServerError, fmt.Errorf("failed to set boot option for client: %w", err)
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// return http.StatusAccepted, nil
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// func (bc *BootController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// clientIP, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
// if err != nil {
|
|
|
|
// bc.l.Errorf("Failed to read remote IP: %s", err.Error())
|
|
|
|
// helpers.HandleResponse(w, r, http.StatusInternalServerError, nil, bc.l)
|
|
|
|
// return
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// var returncode int
|
|
|
|
// var content []byte
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// switch r.Method {
|
|
|
|
// case http.MethodGet:
|
|
|
|
// returncode, content, err = bc.getBootOption(clientIP, w, r)
|
|
|
|
// case http.MethodPut:
|
|
|
|
// returncode, err = bc.setBootOption(clientIP, w, r)
|
|
|
|
// default:
|
|
|
|
// returncode = http.StatusMethodNotAllowed
|
|
|
|
// }
|
2023-07-24 20:56:10 +00:00
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
// if err != nil {
|
|
|
|
// bc.l.Errorf("An error occured while handling boot request: %q", err.Error())
|
|
|
|
// }
|
|
|
|
// helpers.HandleResponse(w, r, returncode, content, bc.l)
|
|
|
|
// }
|