github.com/ethersphere/bee/v2@v2.2.0/pkg/api/welcome_message.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"encoding/json"
     9  	"net/http"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    12  )
    13  
    14  const welcomeMessageMaxRequestSize = 512
    15  
    16  type welcomeMessageRequest struct {
    17  	WelcomeMesssage string `json:"welcomeMessage"`
    18  }
    19  
    20  type welcomeMessageResponse struct {
    21  	WelcomeMesssage string `json:"welcomeMessage"`
    22  }
    23  
    24  func (s *Service) getWelcomeMessageHandler(w http.ResponseWriter, _ *http.Request) {
    25  	val := s.p2p.GetWelcomeMessage()
    26  	jsonhttp.OK(w, welcomeMessageResponse{
    27  		WelcomeMesssage: val,
    28  	})
    29  }
    30  
    31  func (s *Service) setWelcomeMessageHandler(w http.ResponseWriter, r *http.Request) {
    32  	logger := s.logger.WithName("post_welcome_message").Build()
    33  
    34  	var data welcomeMessageRequest
    35  	err := json.NewDecoder(r.Body).Decode(&data)
    36  	if err != nil {
    37  		logger.Debug("failed to read body", "error", err)
    38  		jsonhttp.BadRequest(w, err)
    39  		return
    40  	}
    41  
    42  	if err := s.p2p.SetWelcomeMessage(data.WelcomeMesssage); err != nil {
    43  		logger.Debug("set welcome message failed", "error", err)
    44  		logger.Error(nil, "set welcome message failed")
    45  		jsonhttp.InternalServerError(w, err)
    46  		return
    47  	}
    48  	jsonhttp.OK(w, nil)
    49  }