Skip to content

Commit

Permalink
feat: add seperate endpoint for upload
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Dec 19, 2024
1 parent 7fae2d1 commit 0841f96
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/proplet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func checkRegistryConnectivity(ctx context.Context, registryURL string, registry
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("fegistry returned unexpected status: %d", resp.StatusCode)
return fmt.Errorf("registry returned unexpected status: %d", resp.StatusCode)
}

return nil
Expand Down
21 changes: 20 additions & 1 deletion manager/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func MakeHandler(svc manager.Service, logger *slog.Logger, instanceID string) ht
api.EncodeResponse,
opts...,
), "update-task").ServeHTTP)
r.Put("/upload", otelhttp.NewHandler(kithttp.NewServer(
updateTaskEndpoint(svc),
decodeUploadTaskFileReq,
api.EncodeResponse,
opts...,
), "upload-task-file").ServeHTTP)
r.Delete("/", otelhttp.NewHandler(kithttp.NewServer(
deleteTaskEndpoint(svc),
decodeEntityReq("taskID"),
Expand Down Expand Up @@ -110,6 +116,19 @@ func decodeEntityReq(key string) kithttp.DecodeRequestFunc {
}

func decodeTaskReq(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.Join(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
}

var req taskReq
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Join(err, apiutil.ErrValidation)
}

return req, nil
}

func decodeUploadTaskFileReq(_ context.Context, r *http.Request) (interface{}, error) {
var req taskReq
if err := r.ParseMultipartForm(maxFileSize); err != nil {
return nil, err
Expand All @@ -128,7 +147,7 @@ func decodeTaskReq(_ context.Context, r *http.Request) (interface{}, error) {
return nil, err
}
req.File = data
req.Name = header.Filename
req.Task.ID = chi.URLParam(r, "taskID")

return req, nil
}
Expand Down
11 changes: 9 additions & 2 deletions manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,15 @@ func (svc *service) UpdateTask(ctx context.Context, t task.Task) (task.Task, err
return task.Task{}, err
}
dbT.UpdatedAt = time.Now()
dbT.Name = t.Name
dbT.Inputs = t.Inputs
if t.Name != "" {
dbT.Name = t.Name
}
if t.Inputs != nil {
dbT.Inputs = t.Inputs
}
if t.File != nil {
dbT.File = t.File
}

if err := svc.tasksDB.Update(ctx, dbT.ID, dbT); err != nil {
return task.Task{}, err
Expand Down

0 comments on commit 0841f96

Please sign in to comment.