Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): add apis package #46

Merged
merged 4 commits into from
Nov 16, 2023
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ postgres
.env
docker-compose.local.yaml
docker-compose.local.yml
docker-compose.debug.yaml
docker-compose.debug.yml
33 changes: 33 additions & 0 deletions cmd/node/apis/apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package apis

import (
"github.com/gin-gonic/gin"
msghandler "github.com/machinefi/w3bstream-mainnet/msg/handler"
"github.com/machinefi/w3bstream-mainnet/vm"
"github.com/pkg/errors"
)

func NewServer(ep string, mh *msghandler.Handler) *Server {
saitofun marked this conversation as resolved.
Show resolved Hide resolved
s := &Server{
endpoint: ep,
engine: gin.Default(),
msgHandler: mh,
}
s.engine.POST("/message", s.handleRequest)
return s
}

type Server struct {
engine *gin.Engine
endpoint string
msgHandler *msghandler.Handler
vmHandler *vm.Handler
}

// this func will block caller
func (s *Server) Run() error {
saitofun marked this conversation as resolved.
Show resolved Hide resolved
if err := s.engine.Run(s.endpoint); err != nil {
return errors.Wrap(err, "start http server failed")
}
return nil
}
27 changes: 27 additions & 0 deletions cmd/node/apis/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package apis

import (
"github.com/gin-gonic/gin"
"github.com/machinefi/w3bstream-mainnet/msg"
"log/slog"
"net/http"
)

func (s *Server) handleRequest(c *gin.Context) {
req := &msgReq{}
if err := c.ShouldBindJSON(req); err != nil {
c.JSON(http.StatusBadRequest, newErrResp(err))
return
}
slog.Debug("received your message, handling")
if err := s.msgHandler.Handle(&msg.Msg{
ProjectID: req.ProjectID,
ProjectVersion: req.ProjectVersion,
Data: req.Data,
}); err != nil {
c.JSON(http.StatusInternalServerError, newErrResp(err))
return
}

c.Status(http.StatusOK)
}
2 changes: 1 addition & 1 deletion cmd/node/common.go → cmd/node/apis/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package apis

type errResp struct {
Error string `json:"error,omitempty"`
Expand Down
38 changes: 6 additions & 32 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package main

import (
"github.com/machinefi/w3bstream-mainnet/cmd/node/apis"
"github.com/machinefi/w3bstream-mainnet/enums"
"github.com/machinefi/w3bstream-mainnet/msg/handler"
"github.com/machinefi/w3bstream-mainnet/vm"
"github.com/spf13/viper"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/machinefi/w3bstream-mainnet/enums"
"github.com/machinefi/w3bstream-mainnet/project"

"github.com/gin-gonic/gin"
"github.com/spf13/viper"

"github.com/machinefi/w3bstream-mainnet/msg"
"github.com/machinefi/w3bstream-mainnet/msg/handler"
"github.com/machinefi/w3bstream-mainnet/vm"
)

func main() {
Expand All @@ -36,29 +31,8 @@ func main() {
viper.GetString(enums.EnvKeyProjectConfigPath),
)

router := gin.Default()
router.POST("/message", func(c *gin.Context) {
var req msgReq
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, newErrResp(err))
return
}
msg := &msg.Msg{
ProjectID: req.ProjectID,
ProjectVersion: req.ProjectVersion,
Data: req.Data,
}
slog.Debug("received your message, handling")
if err := msgHandler.Handle(msg); err != nil {
c.JSON(http.StatusInternalServerError, newErrResp(err))
return
}

c.Status(http.StatusOK)
})

go func() {
if err := router.Run(viper.Get("ENDPOINT").(string)); err != nil {
if err := apis.NewServer(viper.GetString(enums.EnvKeyServiceEndpoint), msgHandler).Run(); err != nil {
log.Fatal(err)
}
}()
Expand Down