Skip to content

Commit

Permalink
MySQL: extract tlog http API config to function
Browse files Browse the repository at this point in the history
This small refactoring clearly demonstrates the handlers that are configured for the core tlog spec. The method could be moved in the future, but I suggest leaving it here at first to make sure that any other implementation is exactly the same.

Towards transparency-dev#21.
  • Loading branch information
mhutchinson committed Aug 22, 2024
1 parent 5423c83 commit 97d91a4
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions cmd/example-mysql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,41 @@ func main() {
klog.Exitf("Failed to create new MySQL storage: %v", err)
}

http.HandleFunc("GET /checkpoint", func(w http.ResponseWriter, r *http.Request) {
configureTilesReadApi(http.DefaultServeMux, storage)
http.HandleFunc("POST /add", func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() {
if err := r.Body.Close(); err != nil {
klog.Warningf("/add: %v", err)
}
}()
idx, err := storage.Add(r.Context(), tessera.NewEntry(b))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
if _, err = w.Write([]byte(fmt.Sprintf("%d", idx))); err != nil {
klog.Errorf("/add: %v", err)
return
}
})

if err := http.ListenAndServe(*listen, http.DefaultServeMux); err != nil {
klog.Exitf("ListenAndServe: %v", err)
}
}

// configureTilesReadApi adds the API methods from https://c2sp.org/tlog-tiles to the mux,
// routing the requests to the mysql storage.
// This method could be moved into the storage API as it's likely this will be
// the same for any implementation of a personality based on MySQL.
func configureTilesReadApi(mux *http.ServeMux, storage *mysql.Storage) {
mux.HandleFunc("GET /checkpoint", func(w http.ResponseWriter, r *http.Request) {
checkpoint, err := storage.ReadCheckpoint(r.Context())
if err != nil {
klog.Errorf("/checkpoint: %v", err)
Expand All @@ -98,7 +132,7 @@ func main() {
}
})

http.HandleFunc("GET /tile/{level}/{index...}", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /tile/{level}/{index...}", func(w http.ResponseWriter, r *http.Request) {
level, index, width, err := layout.ParseTileLevelIndexWidth(r.PathValue("level"), r.PathValue("index"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -127,7 +161,7 @@ func main() {
}
})

http.HandleFunc("GET /tile/entries/{index...}", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("GET /tile/entries/{index...}", func(w http.ResponseWriter, r *http.Request) {
index, _, err := layout.ParseTileIndexWidth(r.PathValue("index"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
Expand Down Expand Up @@ -155,33 +189,6 @@ func main() {
return
}
})

http.HandleFunc("POST /add", func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer func() {
if err := r.Body.Close(); err != nil {
klog.Warningf("/add: %v", err)
}
}()
idx, err := storage.Add(r.Context(), tessera.NewEntry(b))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
if _, err = w.Write([]byte(fmt.Sprintf("%d", idx))); err != nil {
klog.Errorf("/add: %v", err)
return
}
})

if err := http.ListenAndServe(*listen, http.DefaultServeMux); err != nil {
klog.Exitf("ListenAndServe: %v", err)
}
}

func initDatabaseSchema(ctx context.Context) {
Expand Down

0 comments on commit 97d91a4

Please sign in to comment.