Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Job Creator support endpoints to the Solver #498

Merged
merged 6 commits into from
Jan 29, 2025
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions pkg/solver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (solverServer *solverServer) ListenAndServe(ctx context.Context, cm *system
subrouter.HandleFunc("/job_offers", http.GetHandler(solverServer.getJobOffers)).Methods("GET")
subrouter.HandleFunc("/job_offers", http.PostHandler(solverServer.addJobOffer)).Methods("POST")

subrouter.HandleFunc("/job_offers/{id}", http.GetHandler(solverServer.getJobOffer)).Methods("GET")
subrouter.HandleFunc("/job_offers/{id}/results", solverServer.getJobOfferResult).Methods("GET")
kelindi marked this conversation as resolved.
Show resolved Hide resolved

subrouter.HandleFunc("/resource_offers", http.GetHandler(solverServer.getResourceOffers)).Methods("GET")
subrouter.HandleFunc("/resource_offers", http.PostHandler(solverServer.addResourceOffer)).Methods("POST")

Expand Down Expand Up @@ -244,6 +247,17 @@ func (solverServer *solverServer) getDeals(res corehttp.ResponseWriter, req *cor
*
*
*/

func (solverServer *solverServer) getJobOffer(res corehttp.ResponseWriter, req *corehttp.Request) (data.JobOfferContainer, error) {
vars := mux.Vars(req)
id := vars["id"]
jobOffer, err := solverServer.store.GetJobOffer(id)
if err != nil {
return data.JobOfferContainer{}, err
}
return *jobOffer, nil
}

func (solverServer *solverServer) getDeal(res corehttp.ResponseWriter, req *corehttp.Request) (data.DealContainer, error) {
vars := mux.Vars(req)
id := vars["id"]
Expand All @@ -270,6 +284,111 @@ func (solverServer *solverServer) getResult(res corehttp.ResponseWriter, req *co
return *result, nil
}

func (solverServer *solverServer) getJobOfferResult(res corehttp.ResponseWriter, req *corehttp.Request) {
vars := mux.Vars(req)
id := vars["id"]

err := func() *http.HTTPError {
jobOffer, err := solverServer.store.GetJobOffer(id)
if err != nil {
log.Error().Err(err).Msgf("error loading job offer")
return &http.HTTPError{
Message: err.Error(),
StatusCode: corehttp.StatusInternalServerError,
}
}
if jobOffer == nil {
return &http.HTTPError{
Message: err.Error(),
StatusCode: corehttp.StatusNotFound,
}
}

signerAddress, err := http.CheckSignature(req)
if err != nil {
log.Error().Err(err).Msgf("error checking signature")
return &http.HTTPError{
Message: errors.New("not authorized").Error(),
StatusCode: corehttp.StatusUnauthorized,
}
}

if signerAddress != jobOffer.JobCreator {
log.Error().Err(err).Msgf("job creator address does not match signer address")
return &http.HTTPError{
Message: errors.New("not authorized").Error(),
StatusCode: corehttp.StatusUnauthorized,
}
}

// Get the directory path
dirPath := GetDealsFilePath(jobOffer.DealID)

// Read directory contents
files, err := os.ReadDir(dirPath)
if err != nil {
return &http.HTTPError{
Message: fmt.Sprintf("error reading directory: %s", err.Error()),
StatusCode: corehttp.StatusNotFound,
}
}

// Find the first regular file
var targetFile os.DirEntry
for _, file := range files {
info, err := file.Info()
if err != nil {
continue
}
if info.Mode().IsRegular() {
targetFile = file
break
}
}

if targetFile == nil {
return &http.HTTPError{
Message: "no regular files found in directory",
StatusCode: corehttp.StatusNotFound,
}
}

// Get the actual filename
filename := targetFile.Name()
filePath := filepath.Join(dirPath, filename)

// Open the file
file, err := os.Open(filePath)
if err != nil {
return &http.HTTPError{
Message: err.Error(),
StatusCode: corehttp.StatusInternalServerError,
}
}
defer file.Close()

// Set appropriate headers using the actual filename
res.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
res.Header().Set("Content-Type", "application/x-tar")

// Copy the file directly to the response
_, err = io.Copy(res, file)
if err != nil {
return &http.HTTPError{
Message: err.Error(),
StatusCode: corehttp.StatusInternalServerError,
}
}

return nil
}()

if err != nil {
log.Ctx(req.Context()).Error().Msgf("error for route: %s", err.Error())
corehttp.Error(res, err.Error(), err.StatusCode)
}
}

kelindi marked this conversation as resolved.
Show resolved Hide resolved
/*
*
*
Expand Down
Loading