Skip to content

Commit

Permalink
update endpoint api
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 12, 2024
1 parent 5713cd8 commit 592bcc4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func (c command) handleEndpoint(args []string) {
flagset.Var(&env, "env", "Environment variables for this endpoint")
_ = flagset.Parse(args)

if len(runtime) == 0 {
fmt.Println("please provide a valid runtime [--runtime go, --runtime js]")
os.Exit(1)
}
if !types.ValidRuntime(runtime) {
fmt.Printf("invalid runtime %s, only go and js are currently supported\n", runtime)
os.Exit(1)
Expand Down
33 changes: 33 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (s *Server) initRouter() {
s.router.Get("/endpoint/{id}/metrics", makeAPIHandler(s.handleGetEndpointMetrics))
s.router.Post("/endpoint", makeAPIHandler(s.handleCreateEndpoint))
s.router.Post("/endpoint/{id}/deployment", makeAPIHandler(s.handleCreateDeployment))
s.router.Put("/endpoint/{id}", makeAPIHandler(s.handleUpdateEndpoint))
s.router.Post("/publish", makeAPIHandler(s.handlePublish))
}

Expand Down Expand Up @@ -84,6 +85,38 @@ func (p CreateEndpointParams) validate() error {
return nil
}

type UpdateEndpointParams struct {
Environment map[string]string `json:"environment"`
}

func (s *Server) handleUpdateEndpoint(w http.ResponseWriter, r *http.Request) error {
endpointID, err := uuid.Parse(chi.URLParam(r, "id"))
if err != nil {
return writeJSON(w, http.StatusBadRequest, ErrorResponse(err))
}
endpoint, err := s.store.GetEndpoint(endpointID)
if err != nil {
return writeJSON(w, http.StatusNotFound, ErrorResponse(err))
}
var params UpdateEndpointParams
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
return writeJSON(w, http.StatusBadRequest, ErrorResponse(err))
}
defer r.Body.Close()
if len(params.Environment) > 0 {
for k, v := range params.Environment {
endpoint.Environment[k] = v
}
}
updateParams := storage.UpdateEndpointParams{
Environment: endpoint.Environment,
}
if err := s.store.UpdateEndpoint(endpointID, updateParams); err != nil {
return writeJSON(w, http.StatusUnprocessableEntity, ErrorResponse(err))
}
return writeJSON(w, http.StatusOK, map[string]string{"status": "OK"})
}

func (s *Server) handleCreateEndpoint(w http.ResponseWriter, r *http.Request) error {
var params CreateEndpointParams
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ import (
"github.com/stretchr/testify/require"
)

func TestUpdateEndpoint(t *testing.T) {
s := createServer()
endpoint := seedEndpoint(t, s)
expected := map[string]string{"A": "B", "C": "D", "FOO": "BAR"}

params := UpdateEndpointParams{
Environment: map[string]string{"A": "B", "C": "D"},
}
b, err := json.Marshal(params)
require.Nil(t, err)

req := httptest.NewRequest("PUT", "/endpoint/"+endpoint.ID.String(), bytes.NewReader(b))
resp := httptest.NewRecorder()
s.router.ServeHTTP(resp, req)

require.Equal(t, http.StatusOK, resp.Result().StatusCode)
require.Equal(t, expected, endpoint.Environment)
}

func TestCreateEndpoint(t *testing.T) {
s := createServer()

Expand Down

0 comments on commit 592bcc4

Please sign in to comment.