Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 19 additions & 1 deletion api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type receiveReq struct {
// Therefore, we’ll use only two codes: 200 for success and 400 for failure.
// Specific error details will be provided in the returned error message.
type httpServer struct {
wsAddr string
engine *gin.Engine
db *db.DB
prv *ecdsa.PrivateKey
Expand Down Expand Up @@ -286,13 +287,29 @@ func (s *httpServer) receive(c *gin.Context) {
c.Status(http.StatusOK)
}

func (s *httpServer) forwardWs(req *wsapi.CreateTaskReq) {
jsonData, err := json.Marshal(req)
if err != nil {
slog.Error("failed to marshal request when forward w3bstream", "error", err)
return
}
targetURL := fmt.Sprintf("https://%s/v1/task", s.wsAddr)
resp, err := http.Post(targetURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
slog.Error("failed to send http request when forward w3bstream", "error", err)
return
}
defer resp.Body.Close()
}

func (s *httpServer) receiveV2(c *gin.Context) {
req := &wsapi.CreateTaskReq{}
if err := c.ShouldBindJSON(req); err != nil {
slog.Error("failed to bind request", "error", err)
c.JSON(http.StatusBadRequest, newErrResp(errors.Wrap(err, "invalid request payload")))
return
}
s.forwardWs(req)
pid, ok := new(big.Int).SetString(req.ProjectID, 10)
if !ok {
slog.Error("failed to decode project id string", "project_id", req.ProjectID)
Expand Down Expand Up @@ -570,8 +587,9 @@ func (s *httpServer) handleSensor(id string, pkg *proto.BinPackage, data *proto.
return nil
}

func Run(db *db.DB, address string, client *ethclient.Client, prv *ecdsa.PrivateKey) error {
func Run(db *db.DB, address, wsAddr string, client *ethclient.Client, prv *ecdsa.PrivateKey) error {
s := &httpServer{
wsAddr: wsAddr,
engine: gin.Default(),
db: db,
prv: prv,
Expand Down
1 change: 0 additions & 1 deletion cmd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type Config struct {
IoIDRegistryContractAddr string `env:"IOID_REGISTRY_CONTRACT_ADDRESS,optional"`
IoIDContractAddr string `env:"IOID_CONTRACT_ADDRESS,optional"`
ProjectContractAddr string `env:"PROJECT_CONTRACT_ADDRESS,optional"`
W3bstreamProjectID string `env:"W3BSTREAM_PROJECT_ID,optional"`
W3bstreamServiceEndpoint string `env:"W3BSTREAM_SERVICE_ENDPOINT,optional"`
env string `env:"-"`
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
}

go func() {
if err := api.Run(db, cfg.ServiceEndpoint, client, prv); err != nil {
if err := api.Run(db, cfg.ServiceEndpoint, cfg.W3bstreamServiceEndpoint, client, prv); err != nil {
log.Fatal(err)
}
}()
Expand Down
Loading