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 1 commit
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
32 changes: 32 additions & 0 deletions cmd/node/apis/apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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
return &Server{
endpoint: ep,
engine: gin.Default(),
msgHandler: mh,
}
}

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

func (s *Server) Run() error {
saitofun marked this conversation as resolved.
Show resolved Hide resolved
s.engine.POST("/message", s.handleRequest)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

del this line, run shouldn't have path register logic


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 := HandleReq{}
if err := c.ShouldBindJSON(req); err != nil {
c.JSON(http.StatusBadRequest, &HandleErrRsp{err.Error()})
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, &HandleErrRsp{err.Error()})
return
}

c.Status(http.StatusOK)
}
10 changes: 3 additions & 7 deletions cmd/node/common.go → cmd/node/apis/types.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package main
package apis

type errResp struct {
type HandleErrRsp struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use errResp ? handle is verb, maybe not suitable for struct name, but for function name.
rename to private

Error string `json:"error,omitempty"`
}

func newErrResp(err error) *errResp {
return &errResp{Error: err.Error()}
}

type msgReq struct {
type HandleReq struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

ProjectID string `json:"projectID" binding:"required"`
ProjectVersion string `json:"projectVersion" binding:"required"`
Data string `json:"data" binding:"required"`
Expand Down
38 changes: 7 additions & 31 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
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/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 @@ -24,41 +19,22 @@ func main() {
vm.Halo2: viper.GetString(enums.EnvKeyHalo2ServerEndpoint),
},
)

msgHandler := handler.New(
vmHandler,
viper.GetString(enums.EnvKeyChainEndpoint),
viper.GetString(enums.EnvKeyOperatorPrivateKey),
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)
}
}()

done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
<-done

}
7 changes: 7 additions & 0 deletions vm/type.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package vm

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

type Type string

const (
Risc0 Type = "risc0"
Halo2 Type = "halo2"
)

var vmEndpointConfigEnvKeyMap = map[string]Type{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same not used in this pr, maybe can added when is used

enums.EnvKeyRisc0ServerEndpoint: Risc0,
enums.EnvKeyHalo2ServerEndpoint: Halo2,
}