mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-08-19 09:21:29 +02:00
Incorporated gin server.
Each node communicates in the mesh
This commit is contained in:
17
pkg/ctrlserver/api/api.go
Normal file
17
pkg/ctrlserver/api/api.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
ctrlserver "github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
"github.com/tim-beatham/wgmesh/pkg/ctrlserver/api/mesh"
|
||||
)
|
||||
|
||||
func RunAPI(server *ctrlserver.MeshCtrlServer) *gin.Engine {
|
||||
r := gin.Default()
|
||||
|
||||
r.POST("/mesh", func(ctx *gin.Context) {
|
||||
mesh.JoinMesh(ctx, server)
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
23
pkg/ctrlserver/api/mesh/mesh.go
Normal file
23
pkg/ctrlserver/api/mesh/mesh.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package mesh
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
ctrlserver "github.com/tim-beatham/wgmesh/pkg/ctrlserver"
|
||||
)
|
||||
|
||||
type JoinMeshInput struct {
|
||||
MeshId string `json:"mesh-id" binding:"required`
|
||||
}
|
||||
|
||||
func JoinMesh(c *gin.Context, server *ctrlserver.MeshCtrlServer) {
|
||||
var input JoinMeshInput
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
||||
}
|
48
pkg/ctrlserver/ctrlserver.go
Normal file
48
pkg/ctrlserver/ctrlserver.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package ctrlserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
/*
|
||||
* Create a new control server instance running
|
||||
* on the provided port.
|
||||
*/
|
||||
func NewCtrlServer(host string, port int) *MeshCtrlServer {
|
||||
ctrlServer := new(MeshCtrlServer)
|
||||
ctrlServer.Port = port
|
||||
ctrlServer.Meshes = make(map[string]MeshNode)
|
||||
ctrlServer.Host = host
|
||||
return ctrlServer
|
||||
}
|
||||
|
||||
/*
|
||||
* Given the meshid returns true if the node is in the mesh
|
||||
* false otherwise.
|
||||
*/
|
||||
func (server *MeshCtrlServer) IsInMesh(meshId string) bool {
|
||||
_, inMesh := server.Meshes[meshId]
|
||||
return inMesh
|
||||
}
|
||||
|
||||
func (server *MeshCtrlServer) GetEndpoint() string {
|
||||
return server.Host + ":" + strconv.Itoa(server.Port)
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the gin server instance
|
||||
*/
|
||||
func (server *MeshCtrlServer) Run() bool {
|
||||
r := gin.Default()
|
||||
r.GET("/hello", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "hello",
|
||||
})
|
||||
})
|
||||
|
||||
r.Run(server.GetEndpoint())
|
||||
return true
|
||||
}
|
24
pkg/ctrlserver/ctrltypes.go
Normal file
24
pkg/ctrlserver/ctrltypes.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package ctrlserver
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
/*
|
||||
* Represents a WireGuard node
|
||||
*/
|
||||
type MeshNode struct {
|
||||
Host string
|
||||
CtrlPort string
|
||||
WgPort string
|
||||
WgHost string
|
||||
GinServer gin.Engine
|
||||
}
|
||||
|
||||
/*
|
||||
* Defines the mesh control server this node
|
||||
* is running
|
||||
*/
|
||||
type MeshCtrlServer struct {
|
||||
Host string
|
||||
Port int
|
||||
Meshes map[string]MeshNode
|
||||
}
|
Reference in New Issue
Block a user