Addming a logEvent function to our listener ms
This commit is contained in:
		@ -1,9 +1,12 @@
 | 
			
		||||
package event
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	amqp "github.com/rabbitmq/amqp091-go"
 | 
			
		||||
)
 | 
			
		||||
@ -95,5 +98,79 @@ func handlePayload(payload Payload) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func logEvent(entry Payload) error {
 | 
			
		||||
	log.Println(entry)
 | 
			
		||||
	loggerService := Microservice{
 | 
			
		||||
		Input: entry,
 | 
			
		||||
		Addr:  "http://logger-service/log",
 | 
			
		||||
		ErrCode: statusError{
 | 
			
		||||
			ExpectedCode: http.StatusAccepted,
 | 
			
		||||
			ErrCode:      http.StatusInternalServerError,
 | 
			
		||||
			Err:          errors.New("internal error"),
 | 
			
		||||
		},
 | 
			
		||||
		SuccessMsg: "Logged!",
 | 
			
		||||
	}
 | 
			
		||||
	return callService(loggerService)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func (app *Config) SendMail(w http.ResponseWriter, entry MailPayload) {
 | 
			
		||||
// 	log.Println(entry)
 | 
			
		||||
// 	mailService := Microservice{
 | 
			
		||||
// 		Input: entry,
 | 
			
		||||
// 		Addr:  "http://mail-service/send-mail",
 | 
			
		||||
// 		ErrCode: statusError{
 | 
			
		||||
// 			ExpectedCode: http.StatusAccepted,
 | 
			
		||||
// 			ErrCode:      http.StatusInternalServerError,
 | 
			
		||||
// 			Err:          errors.New("internal error"),
 | 
			
		||||
// 		},
 | 
			
		||||
// 		SuccessMsg: "Mail sent!",
 | 
			
		||||
// 	}
 | 
			
		||||
// 	app.callService(w, mailService)
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
type statusError struct {
 | 
			
		||||
	ExpectedCode int
 | 
			
		||||
	ErrCode      int
 | 
			
		||||
	Err          error
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Microservice struct {
 | 
			
		||||
	Input      any
 | 
			
		||||
	Addr       string
 | 
			
		||||
	ErrCode    statusError
 | 
			
		||||
	SuccessMsg string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func callService(ms Microservice) error {
 | 
			
		||||
	// create some json we'll send to the microservice
 | 
			
		||||
	inputPayload, err := json.MarshalIndent(ms.Input, "", "\t")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	log.Println(ms.Input)
 | 
			
		||||
 | 
			
		||||
	// call the service
 | 
			
		||||
	req, err := http.NewRequest(
 | 
			
		||||
		"POST",
 | 
			
		||||
		ms.Addr,
 | 
			
		||||
		bytes.NewBuffer(inputPayload),
 | 
			
		||||
	)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	req.Header.Add("Content-Type", "application/json")
 | 
			
		||||
	client := &http.Client{}
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	log.Println(resp.Body)
 | 
			
		||||
 | 
			
		||||
	// make sure we get back the correct status code
 | 
			
		||||
	if resp.StatusCode != ms.ErrCode.ExpectedCode {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user