Fix startup bug in demo script

This commit is contained in:
Vikram Rangnekar
2019-07-30 01:38:05 -04:00
parent dd085fb7fd
commit cf0e4d10fd
14 changed files with 110 additions and 39 deletions

View File

@ -84,7 +84,7 @@ func (al *allowList) load() {
c--
if c == 0 {
q := b[s:(e + 1)]
al.list[relaxHash(q)] = &allowItem{
al.list[gqlHash(q)] = &allowItem{
uri: uri,
gql: string(q),
}
@ -99,7 +99,7 @@ func (al *allowList) load() {
}
func (al *allowList) save(item *allowItem) {
al.list[relaxHash([]byte(item.gql))] = item
al.list[gqlHash([]byte(item.gql))] = item
f, err := os.Create("./config/allow.list")
if err != nil {

View File

@ -10,6 +10,8 @@ type config struct {
AppName string `mapstructure:"app_name"`
Env string
HostPort string `mapstructure:"host_port"`
Host string
Port string
WebUI bool `mapstructure:"web_ui"`
LogLevel string `mapstructure:"log_level"`
EnableTracing bool `mapstructure:"enable_tracing"`

View File

@ -253,7 +253,7 @@ func (c *coreContext) resolveRemotes(
}
func (c *coreContext) resolvePreparedSQL(gql []byte) ([]byte, *preparedItem, error) {
ps, ok := _preparedList[relaxHash(gql)]
ps, ok := _preparedList[gqlHash(gql)]
if !ok {
return nil, nil, errUnauthorized
}

View File

@ -82,6 +82,8 @@ func initConf() (*config, error) {
vi.SetDefault("env", "development")
vi.BindEnv("env", "GO_ENV")
vi.BindEnv("HOST", "HOST")
vi.BindEnv("PORT", "PORT")
vi.SetDefault("auth.rails.max_idle", 80)
vi.SetDefault("auth.rails.max_active", 12000)
@ -213,8 +215,20 @@ func Init() {
}
func startHTTP() {
hp := strings.SplitN(conf.HostPort, ":", 2)
if len(conf.Host) != 0 {
hp[0] = conf.Host
}
if len(conf.Port) != 0 {
hp[1] = conf.Port
}
hostPort := fmt.Sprintf("%s:%s", hp[0], hp[1])
srv := &http.Server{
Addr: conf.HostPort,
Addr: hostPort,
Handler: routeHandler(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
@ -239,7 +253,7 @@ func startHTTP() {
}
})
fmt.Printf("%s listening on %s (%s)\n", serverName, conf.HostPort, conf.Env)
fmt.Printf("%s listening on %s (%s)\n", serverName, hostPort, conf.Env)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
logger.Error().Err(err).Msg("server closed")

View File

@ -17,26 +17,38 @@ func mkkey(h *xxhash.Digest, k1 string, k2 string) uint64 {
return v
}
func relaxHash(b []byte) string {
func gqlHash(b []byte) string {
b = bytes.TrimSpace(b)
h := sha1.New()
s, e := 0, 0
space := []byte{' '}
var b0, b1 byte
for {
if e == (len(b) - 1) {
if s != 0 {
if ws(b[e]) {
for e < len(b) && ws(b[e]) {
e++
h.Write(bytes.ToLower(b[s:e]))
}
break
} else if ws(b[e]) == false && ws(b[(e+1)]) {
e++
h.Write(bytes.ToLower(b[s:e]))
s = 0
} else if ws(b[e]) && ws(b[(e+1)]) == false {
e++
s = e
if e < len(b) {
b1 = b[e]
}
if al(b0) && al(b1) {
h.Write(space)
}
} else {
e++
s = e
for e < len(b) && ws(b[e]) == false {
e++
}
if e != 0 {
b0 = b[(e - 1)]
}
h.Write(bytes.ToLower(b[s:e]))
}
if e >= len(b) {
break
}
}
@ -44,5 +56,9 @@ func relaxHash(b []byte) string {
}
func ws(b byte) bool {
return b == ' ' || b == '\n' || b == '\t'
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')
}

View File

@ -5,7 +5,7 @@ import (
"testing"
)
func TestRelaxHash(t *testing.T) {
func TestRelaxHash1(t *testing.T) {
var v1 = []byte(`
products(
limit: 30,
@ -24,11 +24,39 @@ func TestRelaxHash(t *testing.T) {
price
} `)
h1 := relaxHash(v1)
h2 := relaxHash(v2)
h1 := gqlHash(v1)
h2 := gqlHash(v2)
if strings.Compare(h1, h2) != 0 {
t.Fatal("Hashes don't match they should")
}
}
func TestRelaxHash2(t *testing.T) {
var v1 = []byte(`
{
products(
limit: 30
order_by: { price: desc }
distinct: [price]
where: { id: { and: { greater_or_equals: 20, lt: 28 } } }
) {
id
name
price
user {
id
email
}
}
}`)
var v2 = []byte(` { products( limit: 30, order_by: { price: desc }, distinct: [ price ] where: { id: { and: { greater_or_equals: 20, lt: 28 } } }) { id name price user { id email } } } `)
h1 := gqlHash(v1)
h2 := gqlHash(v2)
if strings.Compare(h1, h2) != 0 {
t.Fatal("Hashes don't match they should")
}
}