super-graph/serv/allow.go

134 lines
1.9 KiB
Go
Raw Normal View History

2019-07-29 07:13:33 +02:00
package serv
import (
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strings"
)
type allowItem struct {
uri string
gql string
}
var _allowList allowList
type allowList struct {
list map[string]*allowItem
saveChan chan *allowItem
}
func initAllowList() {
_allowList = allowList{
list: make(map[string]*allowItem),
saveChan: make(chan *allowItem),
}
_allowList.load()
go func() {
for v := range _allowList.saveChan {
_allowList.save(v)
}
}()
}
func (al *allowList) add(req *gqlReq) {
if len(req.ref) == 0 || len(req.Query) == 0 {
return
}
al.saveChan <- &allowItem{
uri: req.ref,
gql: req.Query,
}
}
func (al *allowList) load() {
filename := "./config/allow.list"
if _, err := os.Stat(filename); os.IsNotExist(err) {
return
}
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
if len(b) == 0 {
return
}
var uri string
s, e, c := 0, 0, 0
for {
if c == 0 && b[e] == '#' {
s = e
2019-07-29 07:49:48 +02:00
for b[e] != '\n' && e < len(b) {
e++
}
if (e - s) > 2 {
uri = strings.TrimSpace(string(b[(s + 1):e]))
2019-07-29 07:13:33 +02:00
}
}
if b[e] == '{' {
if c == 0 {
s = e
}
c++
} else if b[e] == '}' {
c--
if c == 0 {
q := b[s:(e + 1)]
2019-07-30 07:38:05 +02:00
al.list[gqlHash(q)] = &allowItem{
2019-07-29 07:13:33 +02:00
uri: uri,
gql: string(q),
}
}
}
e++
if e >= len(b) {
break
}
}
}
func (al *allowList) save(item *allowItem) {
2019-07-30 07:38:05 +02:00
al.list[gqlHash([]byte(item.gql))] = item
2019-07-29 07:13:33 +02:00
f, err := os.Create("./config/allow.list")
if err != nil {
panic(err)
}
defer f.Close()
keys := []string{}
urlMap := make(map[string][]string)
for _, v := range al.list {
urlMap[v.uri] = append(urlMap[v.uri], v.gql)
}
for k := range urlMap {
keys = append(keys, k)
}
sort.Strings(keys)
for i := range keys {
k := keys[i]
v := urlMap[k]
f.WriteString(fmt.Sprintf("# %s\n\n", k))
for i := range v {
f.WriteString(fmt.Sprintf("query %s\n\n", v[i]))
}
}
}