Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to configure proxy to write communication to the application log #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions baskets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ const DoNotForwardHeader = "X-Do-Not-Forward"

// BasketConfig describes single basket configuration.
type BasketConfig struct {
ForwardURL string `json:"forward_url"`
ProxyResponse bool `json:"proxy_response"`
InsecureTLS bool `json:"insecure_tls"`
ExpandPath bool `json:"expand_path"`
Capacity int `json:"capacity"`
ForwardURL string `json:"forward_url"`
ProxyResponse bool `json:"proxy_response"`
InsecureTLS bool `json:"insecure_tls"`
ExpandPath bool `json:"expand_path"`
Capacity int `json:"capacity"`
LogCommunication bool `json:"log"`
}

// ResponseConfig describes response that is generates by service upon HTTP request sent to a basket.
Expand Down
71 changes: 67 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"html/template"
Expand All @@ -22,6 +23,13 @@ var indexPageTemplate = template.Must(template.New("index").Parse(indexPageConte
var basketPageTemplate = template.Must(template.New("basket").Parse(basketPageContentTemplate))
var basketsPageTemplate = template.Must(template.New("baskets").Parse(basketsPageContentTemplate))

type ResponseLogItem struct {
Name string
Headers string
Status string
Body string
}

// writeJSON writes JSON content to HTTP response
func writeJSON(w http.ResponseWriter, status int, json []byte, err error) {
if err != nil {
Expand Down Expand Up @@ -375,6 +383,14 @@ func AcceptBasketRequests(w http.ResponseWriter, r *http.Request) {
if !validBasketName.MatchString(name) {
http.Error(w, "Invalid basket name; ["+name+"] does not match pattern: "+validBasketName.String(), http.StatusBadRequest)
} else if basket := basketsDb.Get(name); basket != nil {

data := ToRequestData(r)

//log
if basket.Config().LogCommunication == true {
log.Printf("[info] basket: %s request %s", name, data)
}

request := basket.Add(r)

// forward request if configured and it's a first forwarding
Expand All @@ -400,7 +416,16 @@ func forwardAndForget(request *RequestData, config BasketConfig, name string) {
if err != nil {
log.Printf("[warn] failed to forward request for basket: %s - %s", name, err)
} else {
io.Copy(ioutil.Discard, response.Body)
body, err := ioutil.ReadAll(response.Body)
if err == nil {
//log
logItem := ResponseLogItem{}
logItem.Name = name
logItem.Headers = fmt.Sprintf("%s", response.Header)
logItem.Status = fmt.Sprintf("%s", response.StatusCode)
logItem.Body = string(body)
logResponse(logItem, config)
}
response.Body.Close()
}
}
Expand All @@ -420,12 +445,24 @@ func forwardAndProxyResponse(w http.ResponseWriter, request *RequestData, config
w.WriteHeader(response.StatusCode)

// body
_, err := io.Copy(w, response.Body)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("[warn] failed to proxy response body for basket: %s - %s", name, err)
io.Copy(ioutil.Discard, response.Body)
} else {

w.Write(body)

//log
logItem := ResponseLogItem{}
logItem.Name = name
logItem.Headers = fmt.Sprintf("%s", response.Header)
logItem.Status = fmt.Sprintf("%s", response.StatusCode)
logItem.Body = string(body)
logResponse(logItem, config)
}
response.Body.Close()

}
}

Expand All @@ -451,12 +488,38 @@ func writeBasketResponse(w http.ResponseWriter, r *http.Request, name string, ba
// status
w.WriteHeader(response.Status)
// templated body
t.Execute(w, r.URL.Query())
var buff bytes.Buffer
t.Execute(&buff, r.URL.Query())
w.Write(buff.Bytes())

//log
logItem := ResponseLogItem{}
logItem.Name = name
logItem.Headers = fmt.Sprintf("%s", response.Headers)
logItem.Status = fmt.Sprintf("%s", response.Status)
logItem.Body = buff.String()
logResponse(logItem, basket.Config())
}
} else {
// status
w.WriteHeader(response.Status)
// plain body
w.Write([]byte(response.Body))
body := []byte(response.Body)
w.Write(body)

//log
logItem := ResponseLogItem{}
logItem.Name = name
logItem.Headers = fmt.Sprintf("%s", response.Headers)
logItem.Status = fmt.Sprintf("%s", response.Status)
logItem.Body = string(body)
logResponse(logItem, basket.Config())
}
}

func logResponse(logItem ResponseLogItem, basketConfig BasketConfig) {
if basketConfig.LogCommunication == true {
log.Printf("[info] basket: %s response headers: %s status: %s body: %s",
logItem.Name, logItem.Headers, logItem.Status, logItem.Body)
}
}
5 changes: 5 additions & 0 deletions pkg/sumdb/sum.golang.org/latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go.sum database tree
1575235
SceD+C12kiu+7Iv3DzbiSYkrzDRGEQwgtHngFfxJQBE=

— sum.golang.org Az3grv6GdPmlc6JUoPyLYixs3ethNyxh7Y93fGBBPQaADn3hOB7sav1N711gt/IcBFcAOwSmnDUZZ6QuqIfO72pZ/gQ=
7 changes: 7 additions & 0 deletions web/basket.html
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,13 @@
if (currentConfig && (
currentConfig.forward_url != $("#basket_forward_url").val() ||
currentConfig.expand_path != $("#basket_expand_path").prop("checked") ||
currentConfig.log != $("#basket_log").prop("checked") ||
currentConfig.insecure_tls != $("#basket_insecure_tls").prop("checked") ||
currentConfig.capacity != $("#basket_capacity").val()
)) {
currentConfig.forward_url = $("#basket_forward_url").val();
currentConfig.expand_path = $("#basket_expand_path").prop("checked");
currentConfig.log = $("#basket_log").prop("checked");
currentConfig.insecure_tls = $("#basket_insecure_tls").prop("checked");
currentConfig.capacity = parseInt($("#basket_capacity").val());

Expand Down Expand Up @@ -470,7 +472,9 @@
if (data) {
currentConfig = data;
$("#basket_forward_url").val(currentConfig.forward_url);
$("#basket_proxy_response").prop("checked", currentConfig.proxy_response);
$("#basket_expand_path").prop("checked", currentConfig.expand_path);
$("#basket_log").prop("checked", currentConfig.log);
$("#basket_insecure_tls").prop("checked", currentConfig.insecure_tls);
$("#basket_capacity").val(currentConfig.capacity);
$("#config_dialog").modal();
Expand Down Expand Up @@ -713,6 +717,9 @@ <h4 class="modal-title" id="config_dialog_label">Configuration Settings</h4>
<div class="checkbox">
<label><input type="checkbox" id="basket_expand_path"> Expand Forward Path</label>
</div>
<div class="checkbox">
<label><input type="checkbox" id="basket_log"> Write requests/response to the default loogger</label>
</div>
<div class="form-group">
<label for="basket_capacity" class="control-label">Basket Capacity:</label>
<input type="input" class="form-control" id="basket_capacity">
Expand Down
6 changes: 6 additions & 0 deletions web_basket.html.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,12 +408,14 @@ const (
currentConfig.forward_url != $("#basket_forward_url").val() ||
currentConfig.proxy_response != $("#basket_proxy_response").prop("checked") ||
currentConfig.expand_path != $("#basket_expand_path").prop("checked") ||
currentConfig.log != $("#basket_log").prop("checked") ||
currentConfig.insecure_tls != $("#basket_insecure_tls").prop("checked") ||
currentConfig.capacity != $("#basket_capacity").val()
)) {
currentConfig.forward_url = $("#basket_forward_url").val();
currentConfig.proxy_response = $("#basket_proxy_response").prop("checked");
currentConfig.expand_path = $("#basket_expand_path").prop("checked");
currentConfig.log = $("#basket_log").prop("checked");
currentConfig.insecure_tls = $("#basket_insecure_tls").prop("checked");
currentConfig.capacity = parseInt($("#basket_capacity").val());

Expand Down Expand Up @@ -469,6 +471,7 @@ const (
$("#basket_forward_url").val(currentConfig.forward_url);
$("#basket_proxy_response").prop("checked", currentConfig.proxy_response);
$("#basket_expand_path").prop("checked", currentConfig.expand_path);
$("#basket_log").prop("checked", currentConfig.log);
$("#basket_insecure_tls").prop("checked", currentConfig.insecure_tls);
$("#basket_capacity").val(currentConfig.capacity);
$("#config_dialog").modal();
Expand Down Expand Up @@ -715,6 +718,9 @@ const (
<div class="checkbox">
<label><input type="checkbox" id="basket_expand_path"> Expand Forward Path</label>
</div>
<div class="checkbox">
<label><input type="checkbox" id="basket_log"> Write requests/response to the default loogger</label>
</div>
<div class="form-group">
<label for="basket_capacity" class="control-label">Basket Capacity:</label>
<input type="input" class="form-control" id="basket_capacity">
Expand Down