super-graph/serv/core.go

101 lines
1.7 KiB
Go
Raw Normal View History

2019-04-19 07:55:03 +02:00
package serv
import (
"context"
2019-04-20 06:35:57 +02:00
"crypto/sha1"
2019-04-19 07:55:03 +02:00
"encoding/json"
"fmt"
"io"
"strings"
"time"
2019-04-20 06:35:57 +02:00
"github.com/allegro/bigcache"
"github.com/dosco/super-graph/qcode"
2019-04-19 07:55:03 +02:00
"github.com/go-pg/pg"
"github.com/valyala/fasttemplate"
)
2019-04-20 06:35:57 +02:00
var (
cache, _ = bigcache.NewBigCache(bigcache.DefaultConfig(24 * time.Hour))
)
2019-04-19 07:55:03 +02:00
func handleReq(ctx context.Context, w io.Writer, req *gqlReq) error {
2019-04-20 06:35:57 +02:00
var key, finalSQL string
var qc *qcode.QCode
2019-04-19 07:55:03 +02:00
2019-04-20 06:35:57 +02:00
var entry []byte
var err error
2019-04-19 07:55:03 +02:00
2019-04-20 06:35:57 +02:00
cacheEnabled := (conf.EnableTracing == false)
if cacheEnabled {
k := sha1.Sum([]byte(req.Query))
key = string(k[:])
entry, err = cache.Get(key)
2019-04-19 07:55:03 +02:00
}
2019-04-20 06:35:57 +02:00
if len(entry) == 0 || err == bigcache.ErrEntryNotFound {
qc, err = qcompile.CompileQuery(req.Query)
if err != nil {
return err
}
2019-04-19 07:55:03 +02:00
2019-04-20 06:35:57 +02:00
var sqlStmt strings.Builder
2019-04-19 07:55:03 +02:00
2019-04-20 06:35:57 +02:00
if err := pcompile.Compile(&sqlStmt, qc); err != nil {
return err
}
2019-04-19 07:55:03 +02:00
2019-04-20 06:35:57 +02:00
t := fasttemplate.New(sqlStmt.String(), openVar, closeVar)
sqlStmt.Reset()
_, err = t.Execute(&sqlStmt, varMap(ctx, req.Vars))
if err == errNoUserID &&
authFailBlock == authFailBlockPerQuery &&
authCheck(ctx) == false {
return errUnauthorized
}
if err != nil {
return err
}
finalSQL = sqlStmt.String()
} else if err != nil {
2019-04-19 07:55:03 +02:00
return err
2019-04-20 06:35:57 +02:00
} else {
finalSQL = string(entry)
2019-04-19 07:55:03 +02:00
}
if conf.DebugLevel > 0 {
fmt.Println(finalSQL)
}
st := time.Now()
var root json.RawMessage
_, err = db.Query(pg.Scan(&root), finalSQL)
if err != nil {
return err
}
et := time.Now()
resp := gqlResp{Data: json.RawMessage(root)}
2019-04-20 06:35:57 +02:00
if cacheEnabled {
if err = cache.Set(key, []byte(finalSQL)); err != nil {
return err
}
}
2019-04-19 07:55:03 +02:00
if conf.EnableTracing {
resp.Extensions = &extensions{newTrace(st, et, qc)}
}
json.NewEncoder(w).Encode(resp)
return nil
}