Cleanup backend handler
This commit is contained in:
@@ -97,7 +97,7 @@ func ConnectWithBackend(c *gin.Context) {
|
|||||||
c.Request.Header.Add("x-session-id", sessionId)
|
c.Request.Header.Add("x-session-id", sessionId)
|
||||||
|
|
||||||
// Connect to the database
|
// Connect to the database
|
||||||
cl, err := client.NewFromUrl(cred.DatabaseUrl, nil)
|
cl, err := client.NewFromUrl(cred.DatabaseURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
badRequest(c, err)
|
badRequest(c, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -12,22 +11,26 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Backend represents a third party configuration source
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
Endpoint string
|
Endpoint string
|
||||||
Token string
|
Token string
|
||||||
PassHeaders string
|
PassHeaders string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BackendRequest represents a payload sent to the third-party source
|
||||||
type BackendRequest struct {
|
type BackendRequest struct {
|
||||||
Resource string `json:"resource"`
|
Resource string `json:"resource"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
Headers map[string]string `json:"headers"`
|
Headers map[string]string `json:"headers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BackendCredential represents the third-party response
|
||||||
type BackendCredential struct {
|
type BackendCredential struct {
|
||||||
DatabaseUrl string `json:"database_url"`
|
DatabaseURL string `json:"database_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FetchCredential sends an authentication request to a third-party service
|
||||||
func (be Backend) FetchCredential(resource string, c *gin.Context) (*BackendCredential, error) {
|
func (be Backend) FetchCredential(resource string, c *gin.Context) (*BackendCredential, error) {
|
||||||
request := BackendRequest{
|
request := BackendRequest{
|
||||||
Resource: resource,
|
Resource: resource,
|
||||||
@@ -35,6 +38,7 @@ func (be Backend) FetchCredential(resource string, c *gin.Context) (*BackendCred
|
|||||||
Headers: map[string]string{},
|
Headers: map[string]string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pass white-listed client headers to the backend request
|
||||||
for _, name := range strings.Split(be.PassHeaders, ",") {
|
for _, name := range strings.Split(be.PassHeaders, ",") {
|
||||||
request.Headers[strings.ToLower(name)] = c.Request.Header.Get(name)
|
request.Headers[strings.ToLower(name)] = c.Request.Header.Get(name)
|
||||||
}
|
}
|
||||||
@@ -58,17 +62,12 @@ func (be Backend) FetchCredential(resource string, c *gin.Context) (*BackendCred
|
|||||||
return nil, fmt.Errorf("Got HTTP error %v from backend", resp.StatusCode)
|
return nil, fmt.Errorf("Got HTTP error %v from backend", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
respBody, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cred := &BackendCredential{}
|
cred := &BackendCredential{}
|
||||||
if err := json.Unmarshal(respBody, cred); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(cred); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if cred.DatabaseUrl == "" {
|
if cred.DatabaseURL == "" {
|
||||||
return nil, fmt.Errorf("Database url was not provided")
|
return nil, fmt.Errorf("Database URL was not provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
return cred, nil
|
return cred, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user