update broker to use logger service
This commit is contained in:
parent
e0bc064ee0
commit
fbfa723289
@ -10,7 +10,8 @@ import (
|
||||
|
||||
type RequestPayload struct {
|
||||
Action string `string:"action"`
|
||||
Auth AuthPayload ` json:"auth,omitempty"`
|
||||
Auth AuthPayload `json:"auth,omitempty"`
|
||||
Log LogPayload `json:"log,omitempty"`
|
||||
}
|
||||
|
||||
type AuthPayload struct {
|
||||
@ -18,6 +19,11 @@ type AuthPayload struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type LogPayload struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
func (app *Config) Broker(w http.ResponseWriter, r *http.Request) {
|
||||
payload := jsonResponse{
|
||||
Error: false,
|
||||
@ -39,14 +45,54 @@ func (app *Config) HandleSubmission(w http.ResponseWriter, r *http.Request) {
|
||||
switch requestPayload.Action {
|
||||
case "auth":
|
||||
app.authenticate(w, requestPayload.Auth)
|
||||
case "log":
|
||||
app.LogItem(w, requestPayload.Log)
|
||||
default:
|
||||
app.errorJSON(w, errors.New("unknown action"))
|
||||
}
|
||||
}
|
||||
|
||||
func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
|
||||
// create some json we'll send to the auth microservice
|
||||
authPayload, err := json.MarshalIndent(a, "", "\t")
|
||||
authService := Microservice{
|
||||
Input: a,
|
||||
Addr: "http://authentication-service/authenticate",
|
||||
ErrCode: statusError{
|
||||
Code: http.StatusUnauthorized,
|
||||
Err: errors.New("invalid credentials"),
|
||||
},
|
||||
SuccessMsg: "Authenticated",
|
||||
}
|
||||
app.callService(w, authService)
|
||||
}
|
||||
|
||||
func (app *Config) LogItem(w http.ResponseWriter, entry LogPayload) {
|
||||
loggerService := Microservice{
|
||||
Input: entry,
|
||||
Addr: "http://logger-service/log",
|
||||
ErrCode: statusError{
|
||||
Code: http.StatusInternalServerError,
|
||||
Err: errors.New("internal error"),
|
||||
},
|
||||
SuccessMsg: "Logged!",
|
||||
}
|
||||
app.callService(w, loggerService)
|
||||
}
|
||||
|
||||
type statusError struct {
|
||||
Code int
|
||||
Err error
|
||||
}
|
||||
|
||||
type Microservice struct {
|
||||
Input any
|
||||
Addr string
|
||||
ErrCode statusError
|
||||
SuccessMsg string
|
||||
}
|
||||
|
||||
func (app *Config) callService(w http.ResponseWriter, ms Microservice) {
|
||||
// create some json we'll send to the microservice
|
||||
inputPayload, err := json.MarshalIndent(ms.Input, "", "\t")
|
||||
if err != nil {
|
||||
app.errorJSON(w, err, http.StatusBadRequest)
|
||||
return
|
||||
@ -55,8 +101,8 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
|
||||
// call the service
|
||||
req, err := http.NewRequest(
|
||||
"POST",
|
||||
"http://authentication-service/authenticate",
|
||||
bytes.NewBuffer(authPayload),
|
||||
ms.Addr,
|
||||
bytes.NewBuffer(inputPayload),
|
||||
)
|
||||
if err != nil {
|
||||
app.errorJSON(w, err, http.StatusBadRequest)
|
||||
@ -75,7 +121,7 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
|
||||
|
||||
// make sure we get back the correct status code
|
||||
if resp.StatusCode != http.StatusAccepted {
|
||||
app.errorJSON(w, errors.New("invalid credentials"))
|
||||
app.errorJSON(w, ms.ErrCode.Err, ms.ErrCode.Code)
|
||||
return
|
||||
}
|
||||
|
||||
@ -89,13 +135,13 @@ func (app *Config) authenticate(w http.ResponseWriter, a AuthPayload) {
|
||||
}
|
||||
|
||||
if respPayload.Error {
|
||||
app.errorJSON(w, errors.New(respPayload.Message), http.StatusUnauthorized)
|
||||
app.errorJSON(w, errors.New(respPayload.Message))
|
||||
return
|
||||
}
|
||||
|
||||
var payload jsonResponse
|
||||
payload.Error = false
|
||||
payload.Message = "Authenticated!"
|
||||
payload.Message = ms.SuccessMsg
|
||||
payload.Data = respPayload.Data
|
||||
|
||||
app.writeJSON(w, http.StatusOK, payload)
|
||||
|
Loading…
Reference in New Issue
Block a user