Skip to content

Commit

Permalink
feat: add storage proxy http configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Aug 7, 2024
1 parent b531ed6 commit f5736f6
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions provider/storage/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package storage

import (
"context"
"github.com/axone-protocol/axone-sdk/auth"
"github.com/axone-protocol/axone-sdk/auth/jwt"
axonehttp "github.com/axone-protocol/axone-sdk/http"
"github.com/gorilla/mux"
"io"
"net/http"
"time"
)

func (s *Proxy) HTTPConfigurator(jwtSecretKey []byte, jwtTTL time.Duration) axonehttp.Option {
jwtFactory := jwt.NewFactory(jwtSecretKey, s.key.DID, jwtTTL)

return axonehttp.WithOptions(
axonehttp.WithRoute(http.MethodGet, "/authenticate", jwtFactory.HTTPAuthHandler(s)),
axonehttp.WithRoute(http.MethodGet, "/{path}}", jwtFactory.VerifyHTTPMiddleware(s.HTTPReadHandler())),
axonehttp.WithRoute(http.MethodPost, "/{path}}", jwtFactory.VerifyHTTPMiddleware(s.HTTPStoreHandler())),
)
}

func (s *Proxy) HTTPReadHandler() auth.AuthenticatedHandler {
return func(id *auth.Identity, writer http.ResponseWriter, request *http.Request) {
resource, err := s.Read(context.Background(), id, mux.Vars(request)["path"])
if err != nil {
// ...
return
}

writer.WriteHeader(http.StatusOK)
io.Copy(writer, resource)
}
}

func (s *Proxy) HTTPStoreHandler() auth.AuthenticatedHandler {
return func(id *auth.Identity, writer http.ResponseWriter, request *http.Request) {
vc, err := s.Store(context.Background(), id, mux.Vars(request)["path"], request.Body)
if err != nil {
// ...
return
}

writer.WriteHeader(http.StatusOK)
io.Copy(writer, vc)
}
}

0 comments on commit f5736f6

Please sign in to comment.