From 9d4c10b392a46424b25b3c7ede5a891c3d1688e3 Mon Sep 17 00:00:00 2001 From: alexohneander Date: Tue, 11 Feb 2025 15:42:09 +0100 Subject: [PATCH] feat: Added gorm.io/gorm and gorm.io/driver/sqlite dependencies, initialized service registry database with SQLite backend --- .gitignore | 1 + go.mod | 5 +++++ go.sum | 10 ++++++++++ internal/general-node/node.go | 13 +++++++++++++ internal/general-node/routes.go | 7 +++++++ internal/general-node/service_registry.go | 21 +++++++++++++++++++++ pkg/models/service.go | 12 ++++++++++++ 7 files changed, 69 insertions(+) create mode 100644 pkg/models/service.go diff --git a/.gitignore b/.gitignore index 6f72f89..abacd1a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.dll *.so *.dylib +*.db # Test binary, built with `go test -c` *.test diff --git a/go.mod b/go.mod index 9021dd2..53dc109 100644 --- a/go.mod +++ b/go.mod @@ -17,10 +17,13 @@ require ( github.com/go-playground/validator/v10 v10.24.0 // indirect github.com/goccy/go-json v0.10.5 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.9 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.24 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect @@ -34,4 +37,6 @@ require ( golang.org/x/text v0.22.0 // indirect google.golang.org/protobuf v1.36.5 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/sqlite v1.5.7 // indirect + gorm.io/gorm v1.25.12 // indirect ) diff --git a/go.sum b/go.sum index 701e995..bac67ab 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,10 @@ github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PU github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= @@ -37,6 +41,8 @@ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM= +github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -82,5 +88,9 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I= +gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/internal/general-node/node.go b/internal/general-node/node.go index adbb72e..10a4204 100644 --- a/internal/general-node/node.go +++ b/internal/general-node/node.go @@ -1,10 +1,23 @@ package generalnode import ( + "github.com/alexohneander/flotte/pkg/models" "github.com/gin-gonic/gin" + "gorm.io/driver/sqlite" + "gorm.io/gorm" ) func Start() { + // Init Service Registry Database + db, err := gorm.Open(sqlite.Open("flotte.db"), &gorm.Config{}) + if err != nil { + panic("failed to connect database") + } + + // Migrate the schema + db.AutoMigrate(&models.Service{}) + + // Init API Backend router := gin.Default() router = registerRoutes(router) diff --git a/internal/general-node/routes.go b/internal/general-node/routes.go index 5ef52ad..aa0889a 100644 --- a/internal/general-node/routes.go +++ b/internal/general-node/routes.go @@ -28,6 +28,13 @@ func registerRoutes(router *gin.Engine) *gin.Engine { }) } + err = registerSerivce(req) + if err != nil { + c.JSON(500, gin.H{ + "message": err.Error(), + }) + } + c.JSON(200, respone.ServiceRegister{ ServiceName: req.Name, Status: "registered", diff --git a/internal/general-node/service_registry.go b/internal/general-node/service_registry.go index aab94a4..907eaeb 100644 --- a/internal/general-node/service_registry.go +++ b/internal/general-node/service_registry.go @@ -1 +1,22 @@ package generalnode + +import ( + "fmt" + + "github.com/alexohneander/flotte/pkg/models" + "github.com/alexohneander/flotte/pkg/types/request" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func registerSerivce(req request.ServiceRegister) error { + db, err := gorm.Open(sqlite.Open("flotte.db"), &gorm.Config{}) + if err != nil { + fmt.Println("failed to connect database") + return err + } + + db.Create(&models.Service{Name: req.Name, NodeType: req.NodeType, Address: req.Address, Port: req.Port, Status: "running"}) + + return nil +} diff --git a/pkg/models/service.go b/pkg/models/service.go new file mode 100644 index 0000000..11a81ed --- /dev/null +++ b/pkg/models/service.go @@ -0,0 +1,12 @@ +package models + +import "gorm.io/gorm" + +type Service struct { + gorm.Model + Name string + NodeType string + Address string + Port int + Status string +}