@@ -86,6 +86,7 @@ type receiveReq struct {
8686// Therefore, we’ll use only two codes: 200 for success and 400 for failure.
8787// Specific error details will be provided in the returned error message.
8888type httpServer struct {
89+ wsAddr string
8990 engine * gin.Engine
9091 db * db.DB
9192 prv * ecdsa.PrivateKey
@@ -286,13 +287,29 @@ func (s *httpServer) receive(c *gin.Context) {
286287 c .Status (http .StatusOK )
287288}
288289
290+ func (s * httpServer ) forwardWs (req * wsapi.CreateTaskReq ) {
291+ jsonData , err := json .Marshal (req )
292+ if err != nil {
293+ slog .Error ("failed to marshal request when forward w3bstream" , "error" , err )
294+ return
295+ }
296+ targetURL := fmt .Sprintf ("https://%s/v1/task" , s .wsAddr )
297+ resp , err := http .Post (targetURL , "application/json" , bytes .NewBuffer (jsonData ))
298+ if err != nil {
299+ slog .Error ("failed to send http request when forward w3bstream" , "error" , err )
300+ return
301+ }
302+ defer resp .Body .Close ()
303+ }
304+
289305func (s * httpServer ) receiveV2 (c * gin.Context ) {
290306 req := & wsapi.CreateTaskReq {}
291307 if err := c .ShouldBindJSON (req ); err != nil {
292308 slog .Error ("failed to bind request" , "error" , err )
293309 c .JSON (http .StatusBadRequest , newErrResp (errors .Wrap (err , "invalid request payload" )))
294310 return
295311 }
312+ s .forwardWs (req )
296313 pid , ok := new (big.Int ).SetString (req .ProjectID , 10 )
297314 if ! ok {
298315 slog .Error ("failed to decode project id string" , "project_id" , req .ProjectID )
@@ -570,8 +587,9 @@ func (s *httpServer) handleSensor(id string, pkg *proto.BinPackage, data *proto.
570587 return nil
571588}
572589
573- func Run (db * db.DB , address string , client * ethclient.Client , prv * ecdsa.PrivateKey ) error {
590+ func Run (db * db.DB , address , wsAddr string , client * ethclient.Client , prv * ecdsa.PrivateKey ) error {
574591 s := & httpServer {
592+ wsAddr : wsAddr ,
575593 engine : gin .Default (),
576594 db : db ,
577595 prv : prv ,
0 commit comments