Skip to content

Commit 8e7002f

Browse files
authored
forward to w3bstream (#67)
1 parent 9ac6ab1 commit 8e7002f

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

api/http.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.
8888
type 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+
289305
func (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,

cmd/server/config/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ type Config struct {
2323
IoIDRegistryContractAddr string `env:"IOID_REGISTRY_CONTRACT_ADDRESS,optional"`
2424
IoIDContractAddr string `env:"IOID_CONTRACT_ADDRESS,optional"`
2525
ProjectContractAddr string `env:"PROJECT_CONTRACT_ADDRESS,optional"`
26-
W3bstreamProjectID string `env:"W3BSTREAM_PROJECT_ID,optional"`
2726
W3bstreamServiceEndpoint string `env:"W3BSTREAM_SERVICE_ENDPOINT,optional"`
2827
env string `env:"-"`
2928
}

cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
}
6262

6363
go func() {
64-
if err := api.Run(db, cfg.ServiceEndpoint, client, prv); err != nil {
64+
if err := api.Run(db, cfg.ServiceEndpoint, cfg.W3bstreamServiceEndpoint, client, prv); err != nil {
6565
log.Fatal(err)
6666
}
6767
}()

0 commit comments

Comments
 (0)