Skip to content

Commit

Permalink
cli and api list endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 3, 2024
1 parent 1f4bca4 commit 0dfb38f
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 8 deletions.
31 changes: 26 additions & 5 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ deploy Deploy an app to the cloud [deploy <endpointID path/to/app.wasm>]
help Show usage
`)
os.Exit(0)
}

type stringList []string
Expand Down Expand Up @@ -64,7 +65,6 @@ func main() {
args := flagset.Args()
if len(args) == 0 {
printUsage()
return
}

c := client.New(client.NewConfig().WithURL(config.GetApiUrl()))
Expand All @@ -74,7 +74,17 @@ func main() {

switch args[0] {
case "endpoint":
command.handleCreateEndpoint(args[1:])
if len(args) < 2 {
printUsage()
}
switch args[1] {
case "create":
command.handleCreateEndpoint(args)
case "list":
command.handleListEndpoints(args)
default:
printUsage()
}
case "deploy":
command.handleDeploy(args[1:])
case "help":
Expand All @@ -88,13 +98,24 @@ type command struct {
client *client.Client
}

func (c command) handleListEndpoints(args []string) {
endpoints, err := c.client.ListEndpoints()
if err != nil {
printErrorAndExit(err)
}
b, err := json.MarshalIndent(endpoints, "", " ")
if err != nil {
printErrorAndExit(err)
}
fmt.Println(string(b))
}

func (c command) handleCreateEndpoint(args []string) {
if len(args) != 1 {
if len(args) != 3 {
printUsage()
return
}
params := api.CreateEndpointParams{
Name: args[0],
Name: args[2],
Environment: makeEnvMap(env),
}
endpoint, err := c.client.CreateEndpoint(params)
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (s *Server) initRouter() {
s.router = chi.NewRouter()
s.router.Get("/status", handleStatus)
s.router.Get("/endpoint/{id}", makeAPIHandler(s.handleGetEndpoint))
s.router.Get("/endpoint", makeAPIHandler(s.handleGetEndpoints))
s.router.Get("/endpoint/{id}/metrics", makeAPIHandler(s.handleGetEndpointMetrics))
s.router.Post("/endpoint", makeAPIHandler(s.handleCreateEndpoint))
s.router.Post("/endpoint/{id}/deploy", makeAPIHandler(s.handleCreateDeploy))
Expand Down Expand Up @@ -135,6 +136,14 @@ func (s *Server) handleGetEndpoint(w http.ResponseWriter, r *http.Request) error
return writeJSON(w, http.StatusOK, endpoint)
}

func (s *Server) handleGetEndpoints(w http.ResponseWriter, r *http.Request) error {
endpoints, err := s.store.GetEndpoints()
if err != nil {
return writeJSON(w, http.StatusNotFound, ErrorResponse(err))
}
return writeJSON(w, http.StatusOK, endpoints)
}

// CreateRollbackParams holds all the necessary fields to rollback your application
// to a specific deploy id (version).
type CreateRollbackParams struct {
Expand Down
22 changes: 22 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func (c *Client) CreateEndpoint(params api.CreateEndpointParams) (*types.Endpoin
func (c *Client) CreateDeploy(endpointID uuid.UUID, blob io.Reader, params api.CreateDeployParams) (*types.Deploy, error) {
url := fmt.Sprintf("%s/endpoint/%s/deploy", c.config.url, endpointID)
req, err := http.NewRequest("POST", url, blob)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/octet-stream")
resp, err := c.Do(req)
if err != nil {
Expand All @@ -82,3 +85,22 @@ func (c *Client) CreateDeploy(endpointID uuid.UUID, blob io.Reader, params api.C
}
return &deploy, nil
}

func (c *Client) ListEndpoints() ([]types.Endpoint, error) {
url := fmt.Sprintf("%s/endpoint", c.config.url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.Do(req)
if err != nil {
return nil, err
}
var endpoints []types.Endpoint
if err := json.NewDecoder(resp.Body).Decode(&endpoints); err != nil {
return nil, err
}

return endpoints, nil
}
43 changes: 40 additions & 3 deletions pkg/storage/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"github.com/vmihailenco/msgpack/v5"
)

// RedisStore is the Redis implementation of a Storage interface.
type RedisStore struct {
client *redis.Client
}

// NewRedisStore returns a new RedisStore.
//
// This will return an error if we failed to ping the Redis server.
func NewRedisStore() (*RedisStore, error) {
client := redis.NewClient(&redis.Options{})
err := client.Ping(context.Background()).Err()
Expand All @@ -31,19 +35,47 @@ func (s *RedisStore) CreateEndpoint(endpoint *types.Endpoint) error {
if err != nil {
return err
}
return s.client.Set(context.Background(), endpoint.ID.String(), b, 0).Err()
key := makeKey("endpoint", endpoint.ID)
return s.client.Set(context.Background(), key, b, 0).Err()
}

func (s *RedisStore) GetEndpoint(id uuid.UUID) (*types.Endpoint, error) {
key := makeKey("endpoint", id)
return s.getEndpoint(key)
}

func (s *RedisStore) getEndpoint(id string) (*types.Endpoint, error) {
var endpoint types.Endpoint
b, err := s.client.Get(context.Background(), id.String()).Bytes()
b, err := s.client.Get(context.Background(), id).Bytes()
if err != nil {
return nil, err
}
err = msgpack.Unmarshal(b, &endpoint)
return &endpoint, err
}

func (s *RedisStore) GetEndpoints() ([]types.Endpoint, error) {
var (
cursor uint64
pattern = "endpoint_*"
)
keys, cursor, err := s.client.Scan(context.Background(), cursor, pattern, 0).Result()
if err != nil {
return []types.Endpoint{}, err
}

endpoints := make([]types.Endpoint, len(keys))
for i, key := range keys {
endpoint, err := s.getEndpoint(key)
if err != nil {
return nil, err
}
endpoints[i] = *endpoint
}

return endpoints, nil
}

func (s *RedisStore) UpdateEndpoint(id uuid.UUID, params UpdateEndpointParams) error {
endpoint, err := s.GetEndpoint(id)
if err != nil {
Expand All @@ -64,7 +96,8 @@ func (s *RedisStore) UpdateEndpoint(id uuid.UUID, params UpdateEndpointParams) e
if err != nil {
return err
}
return s.client.Set(context.Background(), endpoint.ID.String(), b, 0).Err()
key := makeKey("endpoint", endpoint.ID)
return s.client.Set(context.Background(), key, b, 0).Err()
}

func (s *RedisStore) GetDeploy(id uuid.UUID) (*types.Deploy, error) {
Expand Down Expand Up @@ -102,3 +135,7 @@ func (s *RedisStore) GetRuntimeMetrics(id uuid.UUID) ([]types.RuntimeMetric, err
err = msgpack.Unmarshal(b, &metrics)
return metrics, err
}

func makeKey(prefix string, id uuid.UUID) string {
return fmt.Sprintf("%s_%s", prefix, id)
}
1 change: 1 addition & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Store interface {
CreateEndpoint(*types.Endpoint) error
UpdateEndpoint(uuid.UUID, UpdateEndpointParams) error
GetEndpoint(uuid.UUID) (*types.Endpoint, error)
GetEndpoints() ([]types.Endpoint, error)
CreateDeploy(*types.Deploy) error
GetDeploy(uuid.UUID) (*types.Deploy, error)
}
Expand Down

0 comments on commit 0dfb38f

Please sign in to comment.