2024-08-28 07:28:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-08-28 19:40:50 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"log"
|
2024-08-28 07:28:34 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2024-08-28 19:40:50 +00:00
|
|
|
type RequestPayload struct {
|
|
|
|
Action string `string:"action"`
|
|
|
|
Auth AuthPayload ` json:"auth,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type AuthPayload struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
2024-08-28 07:28:34 +00:00
|
|
|
func (app *Config) Broker(w http.ResponseWriter, r *http.Request) {
|
|
|
|
payload := jsonResponse{
|
|
|
|
Error: false,
|
|
|
|
Message: "Hit the broker",
|
|
|
|
}
|
|
|
|
|
2024-08-28 08:04:52 +00:00
|
|
|
app.writeJSON(w, http.StatusOK, payload)
|
2024-08-28 07:28:34 +00:00
|
|
|
}
|
2024-08-28 19:40:50 +00:00
|
|
|
|
|
|
|
func (app *Config) HandleSubmission(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var requestPayload RequestPayload
|
|
|
|
|
|
|
|
err := app.readJSON(w, r, &requestPayload)
|
|
|
|
if err != nil {
|
|
|
|
app.errorJSON(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch requestPayload.Action {
|
|
|
|
case "auth":
|
|
|
|
app.authenticate(w, requestPayload.Auth)
|
|
|
|
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")
|
|
|
|
if err != nil {
|
|
|
|
app.errorJSON(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// call the service
|
|
|
|
req, err := http.NewRequest(
|
|
|
|
"POST",
|
2024-08-28 19:51:51 +00:00
|
|
|
"http://authentication-service:4000/authenticate",
|
2024-08-28 19:40:50 +00:00
|
|
|
bytes.NewBuffer(authPayload),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
app.errorJSON(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
app.errorJSON(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
log.Println(resp.Body)
|
|
|
|
|
|
|
|
// make sure we get back the correct status code
|
|
|
|
if resp.StatusCode != http.StatusAccepted {
|
|
|
|
app.errorJSON(w, errors.New("invalid credentials"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a variable we'll read resp.Body into
|
|
|
|
var respPayload jsonResponse
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&respPayload)
|
|
|
|
if err != nil {
|
|
|
|
app.errorJSON(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if respPayload.Error {
|
|
|
|
app.errorJSON(w, errors.New(respPayload.Message), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload jsonResponse
|
|
|
|
payload.Error = false
|
|
|
|
payload.Message = "Authenticated!"
|
|
|
|
payload.Data = respPayload.Data
|
|
|
|
|
|
|
|
app.writeJSON(w, http.StatusOK, payload)
|
|
|
|
}
|