-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
30 lines (25 loc) · 834 Bytes
/
utils.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
package main
import (
"github.com/hagen1778/chproxy/log"
"net/http"
)
func respondWithErr(rw http.ResponseWriter, err error) {
log.Errorf("proxy failed: %s", err)
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(err.Error()))
}
// getAuth retrieves auth credentials from request
// according to CH documentation @see "http://clickhouse.readthedocs.io/en/latest/reference_en.html#HTTP interface"
func getAuth(req *http.Request) (string, string) {
if name, pass, ok := req.BasicAuth(); ok {
return name, pass
}
// if basicAuth is empty - check URL params `user` and `password`
if name := req.URL.Query().Get("user"); name != "" {
if pass := req.URL.Query().Get("password"); name != "" {
return name, pass
}
}
// if still no credentials - treat it as `default` user request
return "default", ""
}