Actually use rate limited storage, add /version with -dirty

master
breel 2020-07-30 16:20:51 -06:00
parent 5a383a1e71
commit 21fa963085
14 changed files with 98 additions and 31 deletions

View File

@ -7,21 +7,37 @@ function main() {
if [ -z "$NOTEST" ]; then if [ -z "$NOTEST" ]; then
go test ./... go test ./...
fi fi
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=0 go build -o $exec -a -installsuffix cgo GOARCH=arm GOARM=5 gobuild $exec
for authed in dndex dndex-auth; do for authed in dndex dndex-auth; do
scp ./public/swagger/* zach@tickle.lan:./$authed/files/swagger/ scp ./public/swagger/* zach@tickle.lan:./$authed/files/swagger/
scp $exec zach@tickle.lan:./$authed/dndex.new scp $exec zach@tickle.lan:./$authed/dndex.new
ssh zach@tickle.lan bash -c "true; while [ -e ./$authed/dndex.new ]; do printf '.'; sleep 3; done; echo" ssh zach@tickle.lan bash -c "true; while [ -e ./$authed/dndex.new ]; do printf '.'; sleep 3; done; echo"
done & done &
wait wait
GOOS=linux CGO_ENABLED=0 go build -o $exec -a -installsuffix cgo gobuild $exec
scp $exec bel@remote.blapointe.com:/home/bel/services/bin/dndex.new scp $exec bel@remote.blapointe.com:/home/bel/services/bin/dndex.new
ssh bel@remote.blapointe.com bash -c 'true; md5sum ./services/bin/dndex*; while [ -e ./services/bin/dndex.new ]; do printf "."; sleep 3; done; md5sum ./services/bin/dndex*'
rm $exec rm $exec
if [ -n "$BIG" ]; then if [ -n "$BIG" ]; then
big big
fi fi
} }
function gobuild() {
GOOS=linux CGO_ENABLED=0 go build \
-o $1 \
-a \
-installsuffix cgo \
-ldflags "-s -w -X main.GitCommit=$(
(
git rev-list -1 HEAD
if git diff | grep . > /dev/null; then
echo "-dirty"
fi
) | tr -d '\n'
)"
}
function big() { function big() {
pushd ./public/vue/dndex-ui pushd ./public/vue/dndex-ui
git checkout master git checkout master

View File

@ -7,10 +7,13 @@ import (
"log" "log"
) )
var GitCommit string
func main() { func main() {
c := config.New() c := config.New()
log.Println(c) log.Println(c)
g := storage.NewGraph() g := storage.NewRateLimitedGraph()
view.GitCommit = GitCommit
if err := view.JSON(g); err != nil { if err := view.JSON(g); err != nil {
panic(err) panic(err)
} }

View File

@ -24,7 +24,7 @@ const (
UserKey = "DnDex-User" UserKey = "DnDex-User"
) )
func Auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func Auth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
if !config.New().Auth { if !config.New().Auth {
return nil return nil
} }
@ -35,7 +35,7 @@ func Auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
func auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func auth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
if isPublic(g, r) { if isPublic(g, r) {
return nil return nil
} }
@ -45,7 +45,7 @@ func auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return checkAuth(g, w, r) return checkAuth(g, w, r)
} }
func isPublic(g storage.Graph, r *http.Request) bool { func isPublic(g storage.RateLimitedGraph, r *http.Request) bool {
namespace, err := getAuthNamespace(r) namespace, err := getAuthNamespace(r)
if err != nil { if err != nil {
return false return false
@ -65,7 +65,7 @@ func hasAuth(r *http.Request) bool {
return err == nil return err == nil
} }
func checkAuth(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func checkAuth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getAuthNamespace(r) namespace, err := getAuthNamespace(r)
if err != nil { if err != nil {
return err return err
@ -85,7 +85,7 @@ func checkAuth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
func requestAuth(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func requestAuth(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getAuthNamespace(r) namespace, err := getAuthNamespace(r)
if err != nil { if err != nil {
http.Error(w, `{"error": "namespace required"}`, http.StatusBadRequest) http.Error(w, `{"error": "namespace required"}`, http.StatusBadRequest)

View File

@ -28,7 +28,7 @@ func TestAuth(t *testing.T) {
os.Setenv("AUTH", "true") os.Setenv("AUTH", "true")
defer os.Setenv("AUTH", "false") defer os.Setenv("AUTH", "false")
g := storage.NewGraph() g := storage.NewRateLimitedGraph()
handler := jsonHandler(g) handler := jsonHandler(g)
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil { if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil {

View File

@ -16,7 +16,7 @@ import (
"strings" "strings"
) )
func files(_ storage.Graph, w http.ResponseWriter, r *http.Request) error { func files(_ storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getNamespace(r) namespace, err := getNamespace(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)

View File

@ -36,7 +36,7 @@ func TestFiles(t *testing.T) {
os.Setenv("FILEROOT", d) os.Setenv("FILEROOT", d)
t.Logf("config: %+v", config.New()) t.Logf("config: %+v", config.New())
handler := jsonHandler(storage.Graph{}) handler := jsonHandler(storage.RateLimitedGraph{})
prefix := path.Join(config.New().FilePrefix, "col") prefix := path.Join(config.New().FilePrefix, "col")

View File

@ -15,19 +15,21 @@ import (
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
func JSON(g storage.Graph) error { var GitCommit string
func JSON(g storage.RateLimitedGraph) error {
port := config.New().Port port := config.New().Port
log.Println("listening on", port) log.Println("listening on", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), jsonHandler(g)) err := http.ListenAndServe(fmt.Sprintf(":%d", port), jsonHandler(g))
return err return err
} }
func jsonHandler(g storage.Graph) http.Handler { func jsonHandler(g storage.RateLimitedGraph) http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
routes := []struct { routes := []struct {
path string path string
foo func(g storage.Graph, w http.ResponseWriter, r *http.Request) error foo func(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error
noauth bool noauth bool
}{ }{
{ {
@ -47,6 +49,11 @@ func jsonHandler(g storage.Graph) http.Handler {
path: config.New().FilePrefix + "/", path: config.New().FilePrefix + "/",
foo: files, foo: files,
}, },
{
path: "/version",
foo: version,
noauth: true,
},
} }
for _, route := range routes { for _, route := range routes {
@ -121,3 +128,9 @@ func getNamespace(r *http.Request) (string, error) {
} }
return namespace, nil return namespace, nil
} }
func version(_ storage.RateLimitedGraph, w http.ResponseWriter, _ *http.Request) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(map[string]string{"version": GitCommit})
}

View File

@ -2,7 +2,9 @@ package view
import ( import (
"context" "context"
"encoding/json"
"local/dndex/config" "local/dndex/config"
"local/dndex/storage"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -90,3 +92,36 @@ func TestRateLimited(t *testing.T) {
t.Fatal(ok, tooMany) t.Fatal(ok, tooMany)
} }
} }
func TestVersion(t *testing.T) {
t.Run("no version set", func(t *testing.T) {
w := httptest.NewRecorder()
if err := version(storage.RateLimitedGraph{}, w, nil); err != nil {
t.Fatal(err)
}
var resp struct {
Version string `json:"version"`
}
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatal(err)
} else if resp.Version != "" {
t.Fatal(resp)
}
})
t.Run("version set", func(t *testing.T) {
GitCommit = "my-git-commit"
w := httptest.NewRecorder()
if err := version(storage.RateLimitedGraph{}, w, nil); err != nil {
t.Fatal(err)
}
var resp struct {
Version string `json:"version"`
}
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatal(err)
} else if resp.Version != "my-git-commit" {
t.Fatal(resp)
}
})
}

View File

@ -10,7 +10,7 @@ import (
"github.com/buger/jsonparser" "github.com/buger/jsonparser"
) )
func port(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func port(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
return portGet(g, w, r) return portGet(g, w, r)
@ -22,7 +22,7 @@ func port(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
} }
} }
func portGet(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func portGet(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getNamespace(r) namespace, err := getNamespace(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
@ -37,7 +37,7 @@ func portGet(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
return json.NewEncoder(w).Encode(map[string]interface{}{namespace: ones}) return json.NewEncoder(w).Encode(map[string]interface{}{namespace: ones})
} }
func portPost(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func portPost(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getNamespace(r) namespace, err := getNamespace(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)

View File

@ -25,7 +25,7 @@ func TestPort(t *testing.T) {
os.Setenv("DBURI", f.Name()) os.Setenv("DBURI", f.Name())
os.Setenv("AUTH", "false") os.Setenv("AUTH", "false")
g := storage.NewGraph() g := storage.NewRateLimitedGraph()
reset := func() { reset := func() {
if err := g.Delete(context.TODO(), "col", map[string]string{}); err != nil { if err := g.Delete(context.TODO(), "col", map[string]string{}); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -7,7 +7,7 @@ import (
"net/http" "net/http"
) )
func register(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func register(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
switch r.Method { switch r.Method {
case http.MethodPost: case http.MethodPost:
return registerPost(g, w, r) return registerPost(g, w, r)
@ -17,7 +17,7 @@ func register(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
} }
} }
func registerPost(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func registerPost(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getAuthNamespace(r) namespace, err := getAuthNamespace(r)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)

View File

@ -26,7 +26,7 @@ func TestRegister(t *testing.T) {
os.Setenv("AUTH", "true") os.Setenv("AUTH", "true")
defer os.Setenv("AUTH", "false") defer os.Setenv("AUTH", "false")
g := storage.NewGraph() g := storage.NewRateLimitedGraph()
handler := jsonHandler(g) handler := jsonHandler(g)
t.Run("register: wrong method", func(t *testing.T) { t.Run("register: wrong method", func(t *testing.T) {

View File

@ -25,7 +25,7 @@ const (
queryOrder = "order" queryOrder = "order"
) )
func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error { func who(g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
namespace, err := getNamespace(r) namespace, err := getNamespace(r)
if err != nil { if err != nil {
http.NotFound(w, r) http.NotFound(w, r)
@ -51,7 +51,7 @@ func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
} }
} }
func whoGet(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoGet(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
id, err := getCleanID(r) id, err := getCleanID(r)
if err != nil { if err != nil {
return whoTrace(namespace, g, w, r) return whoTrace(namespace, g, w, r)
@ -108,7 +108,7 @@ func whoGet(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Re
return err return err
} }
func whoPut(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoPut(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
id, err := getID(r) id, err := getID(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -151,7 +151,7 @@ func whoPut(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Re
return whoGet(namespace, g, w, r) return whoGet(namespace, g, w, r)
} }
func whoPost(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoPost(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
id, err := getID(r) id, err := getID(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -174,7 +174,7 @@ func whoPost(namespace string, g storage.Graph, w http.ResponseWriter, r *http.R
return whoGet(namespace, g, w, r) return whoGet(namespace, g, w, r)
} }
func whoDelete(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoDelete(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
id, err := getCleanID(r) id, err := getCleanID(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -193,7 +193,7 @@ func whoDelete(namespace string, g storage.Graph, w http.ResponseWriter, r *http
return json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) return json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
} }
func whoDeleteConnections(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoDeleteConnections(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
id, err := getID(r) id, err := getID(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -218,7 +218,7 @@ func whoDeleteConnections(namespace string, g storage.Graph, w http.ResponseWrit
return whoGet(namespace, g, w, r) return whoGet(namespace, g, w, r)
} }
func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoPatch(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
id, err := getID(r) id, err := getID(r)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
@ -246,7 +246,7 @@ func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http.
return whoGet(namespace, g, w, r) return whoGet(namespace, g, w, r)
} }
func whoTrace(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error { func whoTrace(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter, r *http.Request) error {
ones, err := g.ListCaseInsensitive(r.Context(), namespace) ones, err := g.ListCaseInsensitive(r.Context(), namespace)
if err != nil { if err != nil {
return err return err

View File

@ -36,7 +36,7 @@ func TestWho(t *testing.T) {
t.Logf("config: %+v", config.New()) t.Logf("config: %+v", config.New())
g := storage.NewGraph() g := storage.NewRateLimitedGraph()
ones := fillDB(t, g) ones := fillDB(t, g)
want := ones[len(ones)-1] want := ones[len(ones)-1]
@ -632,7 +632,7 @@ func TestWho(t *testing.T) {
}) })
} }
func fillDB(t *testing.T, g storage.Graph) []entity.One { func fillDB(t *testing.T, g storage.RateLimitedGraph) []entity.One {
ones := make([]entity.One, 13) ones := make([]entity.One, 13)
for i := range ones { for i := range ones {
ones[i] = randomOne() ones[i] = randomOne()