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