2019-04-19 07:55:03 +02:00
|
|
|
package serv
|
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-03 22:08:17 +02:00
|
|
|
"crypto/sha256"
|
2019-07-29 07:13:33 +02:00
|
|
|
"encoding/hex"
|
2019-09-05 06:09:56 +02:00
|
|
|
"io"
|
2020-04-10 08:27:43 +02:00
|
|
|
"os"
|
2019-09-05 06:09:56 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-02-03 07:21:07 +01:00
|
|
|
"sync"
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
"github.com/dosco/super-graph/jsn"
|
2019-07-29 07:13:33 +02:00
|
|
|
)
|
2019-04-19 07:55:03 +02:00
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
// nolint: errcheck
|
2019-10-24 08:07:42 +02:00
|
|
|
func gqlHash(b string, vars []byte, role string) string {
|
2019-09-05 06:09:56 +02:00
|
|
|
b = strings.TrimSpace(b)
|
2020-05-03 22:08:17 +02:00
|
|
|
h := sha256.New()
|
2019-10-25 06:01:22 +02:00
|
|
|
query := "query"
|
2019-07-30 07:38:05 +02:00
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
s, e := 0, 0
|
2019-07-30 07:38:05 +02:00
|
|
|
space := []byte{' '}
|
2019-10-25 06:01:22 +02:00
|
|
|
starting := true
|
2019-07-30 07:38:05 +02:00
|
|
|
|
|
|
|
var b0, b1 byte
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-10-26 21:43:40 +02:00
|
|
|
if len(b) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
for {
|
2019-10-25 06:01:22 +02:00
|
|
|
if starting && b[e] == 'q' {
|
|
|
|
n := 0
|
|
|
|
se := e
|
|
|
|
for e < len(b) && n < len(query) && b[e] == query[n] {
|
|
|
|
n++
|
|
|
|
e++
|
|
|
|
}
|
|
|
|
if n != len(query) {
|
|
|
|
io.WriteString(h, strings.ToLower(b[se:e]))
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 21:43:40 +02:00
|
|
|
if e >= len(b) {
|
|
|
|
break
|
|
|
|
}
|
2019-07-30 07:38:05 +02:00
|
|
|
if ws(b[e]) {
|
|
|
|
for e < len(b) && ws(b[e]) {
|
2019-07-29 07:13:33 +02:00
|
|
|
e++
|
|
|
|
}
|
2019-07-30 07:38:05 +02:00
|
|
|
if e < len(b) {
|
|
|
|
b1 = b[e]
|
|
|
|
}
|
|
|
|
if al(b0) && al(b1) {
|
|
|
|
h.Write(space)
|
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
} else {
|
2019-10-25 06:01:22 +02:00
|
|
|
starting = false
|
2019-07-30 07:38:05 +02:00
|
|
|
s = e
|
2019-11-28 07:25:46 +01:00
|
|
|
for e < len(b) && !ws(b[e]) {
|
2019-07-30 07:38:05 +02:00
|
|
|
e++
|
|
|
|
}
|
|
|
|
if e != 0 {
|
|
|
|
b0 = b[(e - 1)]
|
|
|
|
}
|
2019-09-05 06:09:56 +02:00
|
|
|
io.WriteString(h, strings.ToLower(b[s:e]))
|
2019-07-30 07:38:05 +02:00
|
|
|
}
|
|
|
|
if e >= len(b) {
|
|
|
|
break
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 08:07:42 +02:00
|
|
|
if len(role) != 0 {
|
|
|
|
io.WriteString(h, role)
|
|
|
|
}
|
|
|
|
|
2019-11-28 07:25:46 +01:00
|
|
|
if len(vars) == 0 {
|
2019-09-05 06:09:56 +02:00
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := jsn.Keys([]byte(vars))
|
|
|
|
|
|
|
|
sort.Slice(fields, func(i, j int) bool {
|
|
|
|
return bytes.Compare(fields[i], fields[j]) == -1
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := range fields {
|
|
|
|
h.Write(fields[i])
|
|
|
|
}
|
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ws(b byte) bool {
|
2019-07-30 07:38:05 +02:00
|
|
|
return b == ' ' || b == '\n' || b == '\t' || b == ','
|
|
|
|
}
|
|
|
|
|
|
|
|
func al(b byte) bool {
|
|
|
|
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
2019-10-24 08:07:42 +02:00
|
|
|
|
2020-01-19 09:12:51 +01:00
|
|
|
func fatalInProd(err error, msg string) {
|
2020-02-03 07:21:07 +01:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
if isDev() {
|
|
|
|
log.Printf("ERR %s: %s", msg, err)
|
|
|
|
} else {
|
2020-04-10 08:27:43 +02:00
|
|
|
log.Fatalf("ERR %s: %s", msg, err)
|
2020-01-19 09:12:51 +01:00
|
|
|
}
|
2020-02-03 07:21:07 +01:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
wg.Wait()
|
2020-01-19 09:12:51 +01:00
|
|
|
}
|
2020-04-10 08:27:43 +02:00
|
|
|
|
|
|
|
func isDev() bool {
|
|
|
|
return strings.HasPrefix(os.Getenv("GO_ENV"), "dev")
|
|
|
|
}
|
2020-04-11 08:45:06 +02:00
|
|
|
|
|
|
|
func sanitize(value string) string {
|
|
|
|
return strings.ToLower(strings.TrimSpace(value))
|
|
|
|
}
|
2020-04-19 18:54:37 +02:00
|
|
|
|
|
|
|
// Get path relative to cwd
|
|
|
|
func relpath(p string) string {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(p, cwd) {
|
|
|
|
return "./" + strings.TrimLeft(p[len(cwd):], "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|