61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
// Package varnish provides functions to build the list of varnish servers that will be used
|
|
package varnish
|
|
|
|
import (
|
|
prometheus2 "http-broadcaster/Prometheus"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
VarnishList []string
|
|
status = 200
|
|
)
|
|
|
|
// InitializeVarnishList sets varnishList variable according to the LIST_METHOD env var
|
|
func InitializeVarnishList(l string) []string {
|
|
data := os.Getenv("VARNISH_SERVERS")
|
|
sliceData := strings.Split(string(data), ",")
|
|
return sliceData
|
|
}
|
|
|
|
// SendToVarnish send to all varnish servers define in varnishList the request with the PURGE or BAN method
|
|
// and the X-Cache-Tags header if necessary.
|
|
func SendToVarnish(method string, url string, tag string) int {
|
|
status = 200
|
|
|
|
// Take url to ban as argument.
|
|
// Loop over the list of Varnish servers and send PURGE request to each.
|
|
// Update status variable to check if servers have successfully purge url.
|
|
for i := 0; i < len(VarnishList); i++ {
|
|
client := &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
}
|
|
// sanitize varnish server host.
|
|
domain := strings.Trim(VarnishList[i], "\r\n")
|
|
req, err := http.NewRequest(method, domain+url, nil)
|
|
if err != nil {
|
|
log.Fatal("Create new request : %s", err)
|
|
}
|
|
// If X-Cache-Tags header is not empty with pass it to varnish.
|
|
if tag != "" {
|
|
req.Header.Add("X-Cache-Tags", tag)
|
|
}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
log.Println("Send new request : ", err)
|
|
}
|
|
if prometheus2.MetricsEnabled {
|
|
prometheus2.IncrementBackendCounterVec(method)
|
|
}
|
|
if resp.StatusCode != 200 {
|
|
status = 405
|
|
}
|
|
resp.Body.Close()
|
|
}
|
|
return status
|
|
}
|