forked from CrunchyData/pg_tileserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
85 lines (76 loc) · 2.01 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bytes"
"fmt"
"net/http"
"strings"
"text/template"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/theckman/httpforwarded"
// log "github.com/sirupsen/logrus"
)
// serverURLBase returns the server URL
// that the client used to access this service.
// In the case of access via a proxy service, if
// the standard headers are set, we return that
// URL base. If necessary the automatic calculation
// can be over-ridden by setting the "UrlBase"
// configuration option
func serverURLBase(r *http.Request) string {
// Use configuration file settings if we have them
configUrl := viper.GetString("UrlBase")
if configUrl != "" {
return configUrl
}
// Preferred scheme
ps := "http"
// Preferred host:port
ph := strings.TrimRight(r.Host, "/")
// Check for the IETF standard "Forwarded" header
// for reverse proxy information
xf := http.CanonicalHeaderKey("Forwarded")
if f, ok := r.Header[xf]; ok {
if fm, err := httpforwarded.Parse(f); err == nil {
ph = fm["host"][0]
ps = fm["proto"][0]
return fmt.Sprintf("%v://%v", ps, ph)
}
}
// Check the X-Forwarded-Host and X-Forwarded-Proto
// headers
xfh := http.CanonicalHeaderKey("X-Forwarded-Host")
if fh, ok := r.Header[xfh]; ok {
ph = fh[0]
}
xfp := http.CanonicalHeaderKey("X-Forwarded-Proto")
if fp, ok := r.Header[xfp]; ok {
ps = fp[0]
}
return fmt.Sprintf("%v://%v", ps, ph)
}
var globalTemplates map[string](*template.Template) = make(map[string](*template.Template))
func getSqlTemplate(name string, tmpl string) *template.Template {
tp, ok := globalTemplates[name]
if ok {
return tp
}
t := template.New(name)
tp, err := t.Parse(tmpl)
if err != nil {
log.Fatal(err)
}
globalTemplates[name] = tp
return tp
}
func renderSqlTemplate(name string, tmpl string, data interface{}) (string, error) {
var buf bytes.Buffer
t := getSqlTemplate(name, tmpl)
err := t.Execute(&buf, data)
if err != nil {
return string(buf.Bytes()), err
}
sql := string(buf.Bytes())
log.Debug(sql)
return sql, nil
}