refactor: general-node and map-node to use flotte types for service registration
This commit is contained in:
parent
eec969fc75
commit
88caecde51
@ -1,24 +1,12 @@
|
|||||||
package generalnode
|
package generalnode
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
router = registerRoutes(router)
|
||||||
// Get Status
|
|
||||||
router.GET("/status", func(c *gin.Context) {
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"message": "pong",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// Add Service
|
|
||||||
router.POST("/service", func(c *gin.Context) {
|
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"serviceName": "service1",
|
|
||||||
"status": "registered",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
router.Run("localhost:8000") // listen and serve on 0.0.0.0:8000
|
router.Run("localhost:8000") // listen and serve on 0.0.0.0:8000
|
||||||
}
|
}
|
||||||
|
38
internal/general-node/routes.go
Normal file
38
internal/general-node/routes.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package generalnode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/alexohneander/flotte/pkg/types/request"
|
||||||
|
respone "github.com/alexohneander/flotte/pkg/types/response"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func registerRoutes(router *gin.Engine) *gin.Engine {
|
||||||
|
// Get Status
|
||||||
|
router.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"message": "pong",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Add Service
|
||||||
|
router.POST("/service", func(c *gin.Context) {
|
||||||
|
defer c.Request.Body.Close()
|
||||||
|
var req request.ServiceRegister
|
||||||
|
err := json.NewDecoder(c.Request.Body).Decode(&req)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{
|
||||||
|
"message": err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, respone.ServiceRegister{
|
||||||
|
ServiceName: req.Name,
|
||||||
|
Status: "registered",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
@ -1,13 +1,57 @@
|
|||||||
package mapnode
|
package mapnode
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/alexohneander/flotte/pkg/types/request"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
|
err := registerAsMapNode()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.GET("/ping", func(c *gin.Context) {
|
router = registerRoutes(router)
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"message": "pong",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
router.Run("localhost:4000") // listen and serve on 0.0.0.0:4000
|
router.Run("localhost:4000") // listen and serve on 0.0.0.0:4000
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registerAsMapNode() error {
|
||||||
|
// http post request to general-node
|
||||||
|
serviceRegisterReq := request.ServiceRegister{
|
||||||
|
Name: "map-01",
|
||||||
|
NodeType: "map",
|
||||||
|
Address: "localhost",
|
||||||
|
Port: 4000,
|
||||||
|
}
|
||||||
|
|
||||||
|
json_data, err := json.Marshal(serviceRegisterReq)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := http.Post("http://localhost:8000/service", "application/json",
|
||||||
|
bytes.NewBuffer(json_data))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
var res map[string]interface{}
|
||||||
|
|
||||||
|
json.NewDecoder(resp.Body).Decode(&res)
|
||||||
|
|
||||||
|
fmt.Println(res)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
14
internal/map-node/routes.go
Normal file
14
internal/map-node/routes.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package mapnode
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func registerRoutes(router *gin.Engine) *gin.Engine {
|
||||||
|
|
||||||
|
router.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"message": "pong",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
@ -3,11 +3,17 @@ package reducenode
|
|||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
func Start() {
|
func Start() {
|
||||||
|
err := registerAsReduceNode()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.GET("/ping", func(c *gin.Context) {
|
router = registerRoutes(router)
|
||||||
c.JSON(200, gin.H{
|
|
||||||
"message": "pong",
|
router.Run("localhost:4001") // listen and serve on 0.0.0.0:4001
|
||||||
})
|
}
|
||||||
})
|
|
||||||
router.Run() // listen and serve on 0.0.0.0:8080
|
func registerAsReduceNode() error {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
14
internal/reduce-node/routes.go
Normal file
14
internal/reduce-node/routes.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package reducenode
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func registerRoutes(router *gin.Engine) *gin.Engine {
|
||||||
|
|
||||||
|
router.GET("/ping", func(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"message": "pong",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
8
pkg/types/request/service_register.go
Normal file
8
pkg/types/request/service_register.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package request
|
||||||
|
|
||||||
|
type ServiceRegister struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
NodeType string `json:"nodeType"`
|
||||||
|
Address string `json:"address"`
|
||||||
|
Port int `json:"port"`
|
||||||
|
}
|
6
pkg/types/response/service_register.go
Normal file
6
pkg/types/response/service_register.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package response
|
||||||
|
|
||||||
|
type ServiceRegister struct {
|
||||||
|
ServiceName string `json:"serviceName"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user