From eb6bafab5e474a2f667825e709049ad62194a9fc Mon Sep 17 00:00:00 2001 From: Martin Hutchinson Date: Mon, 15 Jun 2026 10:01:16 +0000 Subject: [PATCH] [VIndex] Refactor web API handling into common handler --- vindex/cmd/logandmap/main.go | 5 +- vindex/cmd/sumdbindex/main.go | 5 +- vindex/cmd/sumdbindex/web.go | 78 ------------------- vindex/{cmd/logandmap => internal/web}/web.go | 10 +-- 4 files changed, 11 insertions(+), 87 deletions(-) delete mode 100644 vindex/cmd/sumdbindex/web.go rename vindex/{cmd/logandmap => internal/web}/web.go (88%) diff --git a/vindex/cmd/logandmap/main.go b/vindex/cmd/logandmap/main.go index 38539e4..e0dd361 100644 --- a/vindex/cmd/logandmap/main.go +++ b/vindex/cmd/logandmap/main.go @@ -42,6 +42,7 @@ import ( "github.com/transparency-dev/formats/log" fnote "github.com/transparency-dev/formats/note" "github.com/transparency-dev/incubator/vindex" + "github.com/transparency-dev/incubator/vindex/internal/web" "github.com/transparency-dev/tessera" "github.com/transparency-dev/tessera/api" "github.com/transparency-dev/tessera/client" @@ -305,14 +306,14 @@ func submitEntries(ctx context.Context, appender *tessera.Appender) { } func runWebServer(vi *vindex.VerifiableIndex, inLogDir, outLogDir string) (func(context.Context) error, error) { - web := NewServer(vi.Lookup) + srv := web.NewServer(vi.Lookup) ilfs := http.FileServer(http.Dir(inLogDir)) olfs := http.FileServer(http.Dir(outLogDir)) r := mux.NewRouter() r.PathPrefix("/inputlog/").Handler(http.StripPrefix("/inputlog/", ilfs)) r.PathPrefix("/outputlog/").Handler(http.StripPrefix("/outputlog/", olfs)) - web.registerHandlers(r) + srv.RegisterHandlers(r) listener, err := net.Listen("tcp", *listen) if err != nil { diff --git a/vindex/cmd/sumdbindex/main.go b/vindex/cmd/sumdbindex/main.go index 3cf046b..e575427 100644 --- a/vindex/cmd/sumdbindex/main.go +++ b/vindex/cmd/sumdbindex/main.go @@ -38,6 +38,7 @@ import ( fnote "github.com/transparency-dev/formats/note" "github.com/transparency-dev/incubator/sumdb" "github.com/transparency-dev/incubator/vindex" + "github.com/transparency-dev/incubator/vindex/internal/web" "github.com/transparency-dev/tessera" "go.opentelemetry.io/otel/exporters/prometheus" sdkmetric "go.opentelemetry.io/otel/sdk/metric" @@ -216,13 +217,13 @@ func maintainMap(ctx context.Context, vi *vindex.VerifiableIndex) { } func runWebServer(inLog http.Handler, vi *vindex.VerifiableIndex, outLogDir string) (func(context.Context) error, error) { - web := NewServer(vi.Lookup) + srv := web.NewServer(vi.Lookup) olfs := http.FileServer(http.Dir(outLogDir)) r := mux.NewRouter() r.PathPrefix("/inputlog/").Handler(inLog) r.PathPrefix("/outputlog/").Handler(http.StripPrefix("/outputlog/", olfs)) - web.registerHandlers(r) + srv.RegisterHandlers(r) listener, err := net.Listen("tcp", *listen) if err != nil { diff --git a/vindex/cmd/sumdbindex/web.go b/vindex/cmd/sumdbindex/web.go deleted file mode 100644 index 7fc62c2..0000000 --- a/vindex/cmd/sumdbindex/web.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2025 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package main - -import ( - "context" - "crypto/sha256" - _ "embed" - "encoding/hex" - "encoding/json" - "fmt" - "net/http" - - "github.com/gorilla/mux" - "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/transparency-dev/incubator/vindex/api" - "k8s.io/klog/v2" -) - -func NewServer(lookup func(context.Context, [sha256.Size]byte) (api.LookupResponse, error)) Server { - return Server{ - lookup: lookup, - } -} - -type Server struct { - lookup func(context.Context, [sha256.Size]byte) (api.LookupResponse, error) -} - -// handleLookup handles GET requests for looking up map entries. -func (s Server) handleLookup(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - hashStr, ok := vars["hash"] - if !ok { - http.Error(w, "hash parameter not found", http.StatusBadRequest) - return - } - - h, err := hex.DecodeString(hashStr) - if err != nil { - http.Error(w, fmt.Sprintf("invalid hex hash: %v", err), http.StatusBadRequest) - return - } - if l := len(h); l != sha256.Size { - http.Error(w, fmt.Sprintf("hash wrong length (decoded %d bytes)", l), http.StatusBadRequest) - return - } - - klog.V(2).Infof("Received hash from request: '%s'", h) - - resp, err := s.lookup(r.Context(), [sha256.Size]byte(h)) - if err != nil { - http.Error(w, fmt.Sprintf("lookup failed: %v", err), http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - if err := json.NewEncoder(w).Encode(resp); err != nil { - klog.Warningf("failed to encode response: %v", err) - } -} - -func (s Server) registerHandlers(r *mux.Router) { - r.HandleFunc("/vindex/lookup/{hash}", s.handleLookup).Methods("GET") - r.Handle("/metrics", promhttp.Handler()) -} diff --git a/vindex/cmd/logandmap/web.go b/vindex/internal/web/web.go similarity index 88% rename from vindex/cmd/logandmap/web.go rename to vindex/internal/web/web.go index 7fc62c2..4795f81 100644 --- a/vindex/cmd/logandmap/web.go +++ b/vindex/internal/web/web.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package main +package web import ( "context" @@ -39,8 +39,8 @@ type Server struct { lookup func(context.Context, [sha256.Size]byte) (api.LookupResponse, error) } -// handleLookup handles GET requests for looking up map entries. -func (s Server) handleLookup(w http.ResponseWriter, r *http.Request) { +// HandleLookup handles GET requests for looking up map entries. +func (s Server) HandleLookup(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) hashStr, ok := vars["hash"] if !ok { @@ -72,7 +72,7 @@ func (s Server) handleLookup(w http.ResponseWriter, r *http.Request) { } } -func (s Server) registerHandlers(r *mux.Router) { - r.HandleFunc("/vindex/lookup/{hash}", s.handleLookup).Methods("GET") +func (s Server) RegisterHandlers(r *mux.Router) { + r.HandleFunc("/vindex/lookup/{hash}", s.HandleLookup).Methods("GET") r.Handle("/metrics", promhttp.Handler()) }