Skip to content

Commit

Permalink
Merge pull request #70 from JackalLabs/marston/router-display
Browse files Browse the repository at this point in the history
Displaying API outline
  • Loading branch information
dahn510 authored Sep 20, 2024
2 parents 512c1c1 + c512afe commit 4eac159
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
40 changes: 24 additions & 16 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"time"

"github.com/JackalLabs/sequoia/api/types"

"github.com/rs/cors"

"github.com/JackalLabs/sequoia/file_system"
Expand Down Expand Up @@ -45,27 +47,33 @@ func (a *API) Close() error {
func (a *API) Serve(rd *recycle.RecycleDepot, f *file_system.FileSystem, p *proofs.Prover, wallet *wallet.Wallet, chunkSize int64) error {
defer log.Info().Msg("API module stopped")
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler(wallet.AccAddress()))
r.HandleFunc("/upload", PostFileHandler(f, p, wallet, chunkSize))
r.HandleFunc("/download/{merkle}", DownloadFileHandler(f))

r.HandleFunc("/list", ListFilesHandler(f))
r.HandleFunc("/api/client/list", ListFilesHandler(f))
r.HandleFunc("/api/data/fids", LegacyListFilesHandler(f))
r.HandleFunc("/api/client/space", SpaceHandler(wallet.Client, wallet.AccAddress()))
outline := types.NewOutline()

outline.RegisterGetRoute(r, "/", IndexHandler(wallet.AccAddress()))

outline.RegisterPostRoute(r, "/upload", PostFileHandler(f, p, wallet, chunkSize))
outline.RegisterGetRoute(r, "/download/{merkle}", DownloadFileHandler(f))

outline.RegisterGetRoute(r, "/list", ListFilesHandler(f))
outline.RegisterGetRoute(r, "/api/client/list", ListFilesHandler(f))
outline.RegisterGetRoute(r, "/api/data/fids", LegacyListFilesHandler(f))
outline.RegisterGetRoute(r, "/api/client/space", SpaceHandler(wallet.Client, wallet.AccAddress()))

outline.RegisterGetRoute(r, "/ipfs/peers", IPFSListPeers(f))
outline.RegisterGetRoute(r, "/ipfs/hosts", IPFSListHosts(f))
outline.RegisterGetRoute(r, "/ipfs/cids", IPFSListCids(f))
outline.RegisterGetRoute(r, "/ipfs/cid_map", IPFSMapCids(f))
outline.RegisterPostRoute(r, "/ipfs/make_folder", PostIPFSFolder(f))

r.HandleFunc("/ipfs/peers", IPFSListPeers(f))
r.HandleFunc("/ipfs/hosts", IPFSListHosts(f))
r.HandleFunc("/ipfs/cids", IPFSListCids(f))
r.HandleFunc("/ipfs/cid_map", IPFSMapCids(f))
r.HandleFunc("/ipfs/make_folder", PostIPFSFolder(f))
outline.RegisterGetRoute(r, "/dump", DumpDBHandler(f))

r.HandleFunc("/dump", DumpDBHandler(f))
outline.RegisterGetRoute(r, "/version", VersionHandler(wallet))
outline.RegisterGetRoute(r, "/network", NetworkHandler(wallet))

r.HandleFunc("/version", VersionHandler(wallet))
r.HandleFunc("/network", NetworkHandler(wallet))
outline.RegisterGetRoute(r, "/recycle/salvage", RecycleSalvageHandler(rd))

r.HandleFunc("/recycle/salvage", RecycleSalvageHandler(rd))
outline.RegisterGetRoute(r, "/api", outline.OutlineHandler())

r.Handle("/metrics", promhttp.Handler())
r.Use(loggingMiddleware)
Expand Down
50 changes: 50 additions & 0 deletions api/types/outline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package types

import (
"encoding/json"
"net/http"

"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)

type APIOutline struct {
Routes []RouteOutline `json:"routes"`
}

type RouteOutline struct {
Method string `json:"method"`
Path string `json:"path"`
}

func NewOutline() *APIOutline {
return &APIOutline{
Routes: make([]RouteOutline, 0),
}
}

func (o *APIOutline) RegisterRoute(router *mux.Router, method string, path string, f func(http.ResponseWriter, *http.Request)) {
o.Routes = append(o.Routes, RouteOutline{
Method: method,
Path: path,
})
router.HandleFunc(path, f)
}

func (o *APIOutline) RegisterGetRoute(router *mux.Router, path string, f func(http.ResponseWriter, *http.Request)) {
o.RegisterRoute(router, "GET", path, f)
}

func (o *APIOutline) RegisterPostRoute(router *mux.Router, path string, f func(http.ResponseWriter, *http.Request)) {
o.RegisterRoute(router, "POST", path, f)
}

func (o *APIOutline) OutlineHandler() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
err := json.NewEncoder(w).Encode(o.Routes)
if err != nil {
log.Error().Err(err)
return
}
}
}

0 comments on commit 4eac159

Please sign in to comment.