Skip to content

Commit 5e8635d

Browse files
authored
new gateway schemas and handlers
1 parent e4d947b commit 5e8635d

File tree

13 files changed

+192
-158
lines changed

13 files changed

+192
-158
lines changed

gateway/app.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ServeWebApp() {
3737
}
3838

3939
// Migrate the schema
40-
if err := db.AutoMigrate(&models.DataFile{}, &models.User{}, &models.ToolEntity{}, &models.Job{}); err != nil {
40+
if err := db.AutoMigrate(&models.DataFile{}, &models.User{}, &models.Tool{}, &models.Job{}); err != nil {
4141
panic(fmt.Sprintf("failed to migrate database: %v", err))
4242
}
4343

gateway/handlers/jobs.go

+130-125
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,84 @@
11
package handlers
22

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+
// }
7982

8083
// func RunJobHandler(db *gorm.DB) http.HandlerFunc {
8184
// return func(w http.ResponseWriter, r *http.Request) {
@@ -101,62 +104,64 @@ func InitJobHandler(db *gorm.DB) http.HandlerFunc {
101104
// }
102105
// defer os.RemoveAll(tempDir)
103106

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)
105109
// if err != nil {
106110
// http.Error(w, fmt.Sprintf("Error running job: %v", err), http.StatusInternalServerError)
107111
// return
108112
// }
109113

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
113129
// }
114130

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+
// }
116145
// }
117146
// }
118147

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+
// }

gateway/handlers/tools.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ func AddToolHandler(db *gorm.DB) http.HandlerFunc {
7676
}
7777

7878
// Store serialized Tool in DB
79-
toolEntity := models.ToolEntity{
79+
toolEntry := models.Tool{
8080
CID: cid,
8181
ToolJSON: string(toolJSON),
8282
WalletAddress: walletAddress,
8383
}
8484

85-
result := db.Create(&toolEntity)
85+
result := db.Create(&toolEntry)
8686
if result.Error != nil {
8787
if utils.IsDuplicateKeyError(result.Error) {
8888
http.Error(w, "A tool with the same CID already exists", http.StatusConflict)
@@ -92,7 +92,7 @@ func AddToolHandler(db *gorm.DB) http.HandlerFunc {
9292
return
9393
}
9494

95-
utils.SendJSONResponseWithCID(w, toolEntity.CID)
95+
utils.SendJSONResponseWithCID(w, toolEntry.CID)
9696
}
9797
}
9898

@@ -107,7 +107,7 @@ func GetToolHandler(db *gorm.DB) http.HandlerFunc {
107107
params := mux.Vars(r)
108108
cid := params["cid"]
109109

110-
var tool models.ToolEntity
110+
var tool models.Tool
111111
if result := db.First(&tool, "cid = ?", cid); result.Error != nil {
112112
http.Error(w, fmt.Sprintf("Error fetching tool: %v", result.Error), http.StatusInternalServerError)
113113
return
@@ -128,7 +128,7 @@ func GetToolsHandler(db *gorm.DB) http.HandlerFunc {
128128
return
129129
}
130130

131-
var tools []models.ToolEntity
131+
var tools []models.Tool
132132
if result := db.Find(&tools); result.Error != nil {
133133
http.Error(w, fmt.Sprintf("Error fetching tools: %v", result.Error), http.StatusInternalServerError)
134134
return

gateway/handlers/users.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func AddUserHandler(db *gorm.DB) http.HandlerFunc {
2121
}
2222

2323
var requestData struct {
24-
Username string `json:"username"`
2524
WalletAddress string `json:"walletAddress"`
25+
Email string `json:"email"`
2626
}
2727

2828
if err := utils.ReadRequestBody(r, &requestData); err != nil {
@@ -31,7 +31,7 @@ func AddUserHandler(db *gorm.DB) http.HandlerFunc {
3131
return
3232
}
3333

34-
fmt.Printf("Received request to create user: Username: %s, WalletAddress: %s\n", requestData.Username, requestData.WalletAddress)
34+
fmt.Printf("Received request to create user: WalletAddress: %s, Email: %s,\n", requestData.WalletAddress, requestData.Email)
3535

3636
isValidAddress := web3.IsValidEthereumAddress(requestData.WalletAddress)
3737
if !isValidAddress {
@@ -41,19 +41,19 @@ func AddUserHandler(db *gorm.DB) http.HandlerFunc {
4141
}
4242

4343
var existingUser models.User
44-
if err := db.Where("username = ? AND wallet_address = ?", requestData.Username, requestData.WalletAddress).First(&existingUser).Error; err != nil {
44+
if err := db.Where("wallet_address = ? AND email = ?", requestData.WalletAddress, requestData.Email).First(&existingUser).Error; err != nil {
4545
if err == gorm.ErrRecordNotFound {
4646
// User does not exist, create new user
4747
newUser := models.User{
48-
Username: requestData.Username,
4948
WalletAddress: requestData.WalletAddress,
49+
Email: requestData.Email,
5050
}
5151
if result := db.Create(&newUser); result.Error != nil {
5252
utils.SendJSONError(w, fmt.Sprintf("Error creating user: %v", result.Error), http.StatusInternalServerError)
5353
fmt.Println("Error creating user in database:", result.Error)
5454
return
5555
}
56-
fmt.Printf("Successfully created user with ID: %d, Username: %s\n", newUser.ID, newUser.Username)
56+
fmt.Printf("Successfully created user with WalletAddress: %s, Email: %s\n", newUser.WalletAddress, newUser.Email)
5757
w.Header().Set("Content-Type", "application/json")
5858
w.WriteHeader(http.StatusCreated)
5959
json.NewEncoder(w).Encode(newUser)
@@ -63,8 +63,8 @@ func AddUserHandler(db *gorm.DB) http.HandlerFunc {
6363
fmt.Println("Database error:", err)
6464
}
6565
} else {
66-
// User with given username and wallet address already exists, return that user
67-
fmt.Printf("User already exists with ID: %d, Username: %s\n", existingUser.ID, existingUser.Username)
66+
// User with given wallet address and email already exists, return that user
67+
fmt.Printf("User already exists with WalletAddress: %s, Email: %s\n", existingUser.WalletAddress, existingUser.Email)
6868
w.Header().Set("Content-Type", "application/json")
6969
w.WriteHeader(http.StatusOK)
7070
json.NewEncoder(w).Encode(existingUser)

gateway/models/datafile.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import (
55
)
66

77
type DataFile struct {
8-
// ID uint `gorm:"primaryKey"`
98
CID string `gorm:"primaryKey;column:cid;type:varchar(255);not null"` // column name specified to avoid GORM default snake case
109
WalletAddress string `gorm:"type:varchar(42);not null"`
1110
Filename string `gorm:"type:varchar(255);not null"`
1211
Timestamp time.Time `gorm:""`
13-
Public bool `gorm:"default:true"`
12+
Domain string `gorm:"default:public"`
1413
Visible bool `gorm:"default:true"`
1514
}

0 commit comments

Comments
 (0)