Actually use rate limited storage, add /version with -dirty
parent
5a383a1e71
commit
21fa963085
20
deploy.sh
20
deploy.sh
|
|
@ -7,21 +7,37 @@ function main() {
|
|||
if [ -z "$NOTEST" ]; then
|
||||
go test ./...
|
||||
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
|
||||
scp ./public/swagger/* zach@tickle.lan:./$authed/files/swagger/
|
||||
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"
|
||||
done &
|
||||
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
|
||||
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
|
||||
if [ -n "$BIG" ]; then
|
||||
big
|
||||
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() {
|
||||
pushd ./public/vue/dndex-ui
|
||||
git checkout master
|
||||
|
|
|
|||
5
main.go
5
main.go
|
|
@ -7,10 +7,13 @@ import (
|
|||
"log"
|
||||
)
|
||||
|
||||
var GitCommit string
|
||||
|
||||
func main() {
|
||||
c := config.New()
|
||||
log.Println(c)
|
||||
g := storage.NewGraph()
|
||||
g := storage.NewRateLimitedGraph()
|
||||
view.GitCommit = GitCommit
|
||||
if err := view.JSON(g); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
10
view/auth.go
10
view/auth.go
|
|
@ -24,7 +24,7 @@ const (
|
|||
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 {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@ func Auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
|||
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) {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -45,7 +45,7 @@ func auth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
|||
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)
|
||||
if err != nil {
|
||||
return false
|
||||
|
|
@ -65,7 +65,7 @@ func hasAuth(r *http.Request) bool {
|
|||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -85,7 +85,7 @@ func checkAuth(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
|||
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)
|
||||
if err != nil {
|
||||
http.Error(w, `{"error": "namespace required"}`, http.StatusBadRequest)
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ func TestAuth(t *testing.T) {
|
|||
os.Setenv("AUTH", "true")
|
||||
defer os.Setenv("AUTH", "false")
|
||||
|
||||
g := storage.NewGraph()
|
||||
g := storage.NewRateLimitedGraph()
|
||||
handler := jsonHandler(g)
|
||||
|
||||
if err := g.Insert(context.TODO(), "col."+AuthKey, entity.One{Name: UserKey, Title: "password"}); err != nil {
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import (
|
|||
"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)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ func TestFiles(t *testing.T) {
|
|||
os.Setenv("FILEROOT", d)
|
||||
|
||||
t.Logf("config: %+v", config.New())
|
||||
handler := jsonHandler(storage.Graph{})
|
||||
handler := jsonHandler(storage.RateLimitedGraph{})
|
||||
|
||||
prefix := path.Join(config.New().FilePrefix, "col")
|
||||
|
||||
|
|
|
|||
19
view/json.go
19
view/json.go
|
|
@ -15,19 +15,21 @@ import (
|
|||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
func JSON(g storage.Graph) error {
|
||||
var GitCommit string
|
||||
|
||||
func JSON(g storage.RateLimitedGraph) error {
|
||||
port := config.New().Port
|
||||
log.Println("listening on", port)
|
||||
err := http.ListenAndServe(fmt.Sprintf(":%d", port), jsonHandler(g))
|
||||
return err
|
||||
}
|
||||
|
||||
func jsonHandler(g storage.Graph) http.Handler {
|
||||
func jsonHandler(g storage.RateLimitedGraph) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
routes := []struct {
|
||||
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
|
||||
}{
|
||||
{
|
||||
|
|
@ -47,6 +49,11 @@ func jsonHandler(g storage.Graph) http.Handler {
|
|||
path: config.New().FilePrefix + "/",
|
||||
foo: files,
|
||||
},
|
||||
{
|
||||
path: "/version",
|
||||
foo: version,
|
||||
noauth: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, route := range routes {
|
||||
|
|
@ -121,3 +128,9 @@ func getNamespace(r *http.Request) (string, error) {
|
|||
}
|
||||
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})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ package view
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"local/dndex/config"
|
||||
"local/dndex/storage"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
|
|
@ -90,3 +92,36 @@ func TestRateLimited(t *testing.T) {
|
|||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
"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 {
|
||||
case http.MethodGet:
|
||||
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)
|
||||
if err != nil {
|
||||
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})
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ func TestPort(t *testing.T) {
|
|||
os.Setenv("DBURI", f.Name())
|
||||
os.Setenv("AUTH", "false")
|
||||
|
||||
g := storage.NewGraph()
|
||||
g := storage.NewRateLimitedGraph()
|
||||
reset := func() {
|
||||
if err := g.Delete(context.TODO(), "col", map[string]string{}); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"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 {
|
||||
case http.MethodPost:
|
||||
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)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ func TestRegister(t *testing.T) {
|
|||
os.Setenv("AUTH", "true")
|
||||
defer os.Setenv("AUTH", "false")
|
||||
|
||||
g := storage.NewGraph()
|
||||
g := storage.NewRateLimitedGraph()
|
||||
handler := jsonHandler(g)
|
||||
|
||||
t.Run("register: wrong method", func(t *testing.T) {
|
||||
|
|
|
|||
16
view/who.go
16
view/who.go
|
|
@ -25,7 +25,7 @@ const (
|
|||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
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"})
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
|
|
@ -218,7 +218,7 @@ func whoDeleteConnections(namespace string, g storage.Graph, w http.ResponseWrit
|
|||
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)
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ func TestWho(t *testing.T) {
|
|||
|
||||
t.Logf("config: %+v", config.New())
|
||||
|
||||
g := storage.NewGraph()
|
||||
g := storage.NewRateLimitedGraph()
|
||||
ones := fillDB(t, g)
|
||||
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)
|
||||
for i := range ones {
|
||||
ones[i] = randomOne()
|
||||
|
|
|
|||
Loading…
Reference in New Issue