1
1
package handlers
2
2
3
- import (
4
- "bytes"
5
- "encoding/json"
6
- "fmt"
7
- "net/http"
8
- "os"
9
-
10
- "github.com/gorilla/mux"
11
- "github.com/labdao/plex/gateway/models"
12
- "github.com/labdao/plex/gateway/utils"
13
- "github.com/labdao/plex/internal/ipfs"
14
- "github.com/labdao/plex/internal/ipwl"
15
-
16
- "gorm.io/gorm"
17
- )
18
-
19
- func InitJobHandler (db * gorm.DB ) http.HandlerFunc {
20
- return func (w http.ResponseWriter , r * http.Request ) {
21
-
22
- var requestData struct {
23
- ToolPath string `json:"toolPath"`
24
- ScatteringMethod string `json:"scatteringMethod"`
25
- InputVectors map [string ][]string `json:"inputVectors"`
26
- }
27
-
28
- if err := utils .ReadRequestBody (r , & requestData ); err != nil {
29
- http .Error (w , "Error parsing request body" , http .StatusBadRequest )
30
- return
31
- }
32
-
33
- ioList , err := ipwl .InitializeIo (requestData .ToolPath , requestData .ScatteringMethod , requestData .InputVectors )
34
- if err != nil {
35
- http .Error (w , fmt .Sprintf ("Error initializing IO: %v" , err ), http .StatusInternalServerError )
36
- return
37
- }
38
-
39
- // Iterate over the ioList
40
- for _ , io := range ioList {
41
- // Convert the IO object to JSON
42
- ioJson , err := json .Marshal (io )
43
- if err != nil {
44
- http .Error (w , fmt .Sprintf ("Error converting IO to JSON: %v" , err ), http .StatusInternalServerError )
45
- return
46
- }
47
-
48
- // Write the IO JSON to a temporary file
49
- tempFile , err := utils .CreateAndWriteTempFile (bytes .NewReader (ioJson ), "io.json" )
50
- if err != nil {
51
- http .Error (w , fmt .Sprintf ("Error creating temporary file: %v" , err ), http .StatusInternalServerError )
52
- return
53
- }
54
- defer os .Remove (tempFile .Name ())
55
-
56
- // Pin the temporary file to IPFS to get the CID
57
- cid , err := ipfs .PinFile (tempFile .Name ())
58
- if err != nil {
59
- http .Error (w , fmt .Sprintf ("Error pinning file to IPFS: %v" , err ), http .StatusInternalServerError )
60
- return
61
- }
62
-
63
- // Create a new Job object and store it in the database
64
- job := models.Job {
65
- InitialIoCID : cid ,
66
- InitialIoJson : string (ioJson ),
67
- Status : "initialized" ,
68
- }
69
-
70
- if result := db .Create (& job ); result .Error != nil {
71
- http .Error (w , fmt .Sprintf ("Error creating job: %v" , result .Error ), http .StatusInternalServerError )
72
- return
73
- }
74
- }
75
-
76
- utils .SendJSONResponseWithCID (w , "Jobs initialized successfully" )
77
- }
78
- }
3
+ // import (
4
+ // "bytes"
5
+ // "encoding/json"
6
+ // "fmt"
7
+ // "io/ioutil"
8
+ // "net/http"
9
+ // "os"
10
+
11
+ // "github.com/gorilla/mux"
12
+ // "github.com/labdao/plex/gateway/models"
13
+ // "github.com/labdao/plex/gateway/utils"
14
+ // "github.com/labdao/plex/internal/ipfs"
15
+ // "github.com/labdao/plex/internal/ipwl"
16
+
17
+ // "gorm.io/gorm"
18
+ // )
19
+
20
+ // func InitJobHandler(db *gorm.DB) http.HandlerFunc {
21
+ // return func(w http.ResponseWriter, r *http.Request) {
22
+
23
+ // var requestData struct {
24
+ // ToolPath string `json:"toolPath"`
25
+ // ScatteringMethod string `json:"scatteringMethod"`
26
+ // InputVectors map[string][]string `json:"inputVectors"`
27
+ // }
28
+
29
+ // if err := utils.ReadRequestBody(r, &requestData); err != nil {
30
+ // http.Error(w, "Error parsing request body", http.StatusBadRequest)
31
+ // return
32
+ // }
33
+
34
+ // ioList, err := ipwl.InitializeIo(requestData.ToolPath, requestData.ScatteringMethod, requestData.InputVectors)
35
+ // if err != nil {
36
+ // http.Error(w, fmt.Sprintf("Error initializing IO: %v", err), http.StatusInternalServerError)
37
+ // return
38
+ // }
39
+
40
+ // // we should wait to create the Jobs in the DB until
41
+
42
+ // // Iterate over the ioList
43
+ // for _, io := range ioList {
44
+ // // Convert the IO object to JSON
45
+ // ioJson, err := json.Marshal(io)
46
+ // if err != nil {
47
+ // http.Error(w, fmt.Sprintf("Error converting IO to JSON: %v", err), http.StatusInternalServerError)
48
+ // return
49
+ // }
50
+
51
+ // // Write the IO JSON to a temporary file
52
+ // tempFile, err := utils.CreateAndWriteTempFile(bytes.NewReader(ioJson), "io.json")
53
+ // if err != nil {
54
+ // http.Error(w, fmt.Sprintf("Error creating temporary file: %v", err), http.StatusInternalServerError)
55
+ // return
56
+ // }
57
+ // defer os.Remove(tempFile.Name())
58
+
59
+ // // Pin the temporary file to IPFS to get the CID
60
+ // cid, err := ipfs.PinFile(tempFile.Name())
61
+ // if err != nil {
62
+ // http.Error(w, fmt.Sprintf("Error pinning file to IPFS: %v", err), http.StatusInternalServerError)
63
+ // return
64
+ // }
65
+
66
+ // // Create a new Job object and store it in the database
67
+ // job := models.Job{
68
+ // InitialIoCID: cid,
69
+ // InitialIoJson: string(ioJson),
70
+ // Status: "initialized",
71
+ // }
72
+
73
+ // if result := db.Create(&job); result.Error != nil {
74
+ // http.Error(w, fmt.Sprintf("Error creating job: %v", result.Error), http.StatusInternalServerError)
75
+ // return
76
+ // }
77
+ // }
78
+
79
+ // utils.SendJSONResponseWithCID(w, "Jobs initialized successfully")
80
+ // }
81
+ // }
79
82
80
83
// func RunJobHandler(db *gorm.DB) http.HandlerFunc {
81
84
// return func(w http.ResponseWriter, r *http.Request) {
@@ -101,62 +104,64 @@ func InitJobHandler(db *gorm.DB) http.HandlerFunc {
101
104
// }
102
105
// defer os.RemoveAll(tempDir)
103
106
104
- // completedIoJsonCid, ioJsonPath, err := ipwl.RunIO(requestData.IoJsonCid, requestData.OutputDir, false, false, 60, 1, requestData.Annotations)
107
+ // // TODO: optional value for selector label
108
+ // completedIoJsonCid, ioJsonPath, err := ipwl.RunIO(requestData.IoJsonCid, tempDir, "labdao", false, false, 60, 1, requestData.Annotations)
105
109
// if err != nil {
106
110
// http.Error(w, fmt.Sprintf("Error running job: %v", err), http.StatusInternalServerError)
107
111
// return
108
112
// }
109
113
110
- // responseData := map[string]string{
111
- // "completedIoJsonCid": completedIoJsonCid,
112
- // "ioJsonPath": ioJsonPath,
114
+ // // responseData := map[string]string{
115
+ // // "cid": completedIoJsonCid,
116
+ // // "ioJsonPath": ioJsonPath,
117
+ // // }
118
+
119
+ // utils.SendJSONResponseWithCID(w, completedIoJsonCid)
120
+ // fmt.Println("IO JSON Path saved at:", ioJsonPath)
121
+ // }
122
+ // }
123
+
124
+ // func GetJobHandler(db *gorm.DB) http.HandlerFunc {
125
+ // return func(w http.ResponseWriter, r *http.Request) {
126
+ // if err := utils.CheckRequestMethod(r, http.MethodGet); err != nil {
127
+ // utils.SendJSONError(w, err.Error(), http.StatusBadRequest)
128
+ // return
113
129
// }
114
130
115
- // utils.SendJSONResponse(w, responseData)
131
+ // params := mux.Vars(r)
132
+ // cid := params["cid"]
133
+
134
+ // var job models.Job
135
+ // if result := db.Where("initial_io_cid = ?", cid).First(&job); result.Error != nil {
136
+ // utils.SendJSONError(w, fmt.Sprintf("Error fetching job: %v", result.Error), http.StatusInternalServerError)
137
+ // return
138
+ // }
139
+
140
+ // w.Header().Set("Content-Type", "application/json")
141
+ // if err := json.NewEncoder(w).Encode(job); err != nil {
142
+ // utils.SendJSONError(w, "Error encoding job to JSON", http.StatusInternalServerError)
143
+ // return
144
+ // }
116
145
// }
117
146
// }
118
147
119
- func GetJobHandler (db * gorm.DB ) http.HandlerFunc {
120
- return func (w http.ResponseWriter , r * http.Request ) {
121
- if err := utils .CheckRequestMethod (r , http .MethodGet ); err != nil {
122
- utils .SendJSONError (w , err .Error (), http .StatusBadRequest )
123
- return
124
- }
125
-
126
- params := mux .Vars (r )
127
- cid := params ["cid" ]
128
-
129
- var job models.Job
130
- if result := db .Where ("initial_io_cid = ?" , cid ).First (& job ); result .Error != nil {
131
- utils .SendJSONError (w , fmt .Sprintf ("Error fetching job: %v" , result .Error ), http .StatusInternalServerError )
132
- return
133
- }
134
-
135
- w .Header ().Set ("Content-Type" , "application/json" )
136
- if err := json .NewEncoder (w ).Encode (job ); err != nil {
137
- utils .SendJSONError (w , "Error encoding job to JSON" , http .StatusInternalServerError )
138
- return
139
- }
140
- }
141
- }
142
-
143
- func GetJobsHandler (db * gorm.DB ) http.HandlerFunc {
144
- return func (w http.ResponseWriter , r * http.Request ) {
145
- if err := utils .CheckRequestMethod (r , http .MethodGet ); err != nil {
146
- utils .SendJSONError (w , err .Error (), http .StatusBadRequest )
147
- return
148
- }
149
-
150
- var jobs []models.Job
151
- if result := db .Find (& jobs ); result .Error != nil {
152
- utils .SendJSONError (w , fmt .Sprintf ("Error fetching jobs: %v" , result .Error ), http .StatusInternalServerError )
153
- return
154
- }
155
-
156
- w .Header ().Set ("Content-Type" , "application/json" )
157
- if err := json .NewEncoder (w ).Encode (jobs ); err != nil {
158
- utils .SendJSONError (w , "Error encoding jobs to JSON" , http .StatusInternalServerError )
159
- return
160
- }
161
- }
162
- }
148
+ // func GetJobsHandler(db *gorm.DB) http.HandlerFunc {
149
+ // return func(w http.ResponseWriter, r *http.Request) {
150
+ // if err := utils.CheckRequestMethod(r, http.MethodGet); err != nil {
151
+ // utils.SendJSONError(w, err.Error(), http.StatusBadRequest)
152
+ // return
153
+ // }
154
+
155
+ // var jobs []models.Job
156
+ // if result := db.Find(&jobs); result.Error != nil {
157
+ // utils.SendJSONError(w, fmt.Sprintf("Error fetching jobs: %v", result.Error), http.StatusInternalServerError)
158
+ // return
159
+ // }
160
+
161
+ // w.Header().Set("Content-Type", "application/json")
162
+ // if err := json.NewEncoder(w).Encode(jobs); err != nil {
163
+ // utils.SendJSONError(w, "Error encoding jobs to JSON", http.StatusInternalServerError)
164
+ // return
165
+ // }
166
+ // }
167
+ // }
0 commit comments