diff --git a/go.mod b/go.mod index bf3072c..d02208a 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,35 @@ module jannex/robot-control-manager go 1.21.0 require ( - github.com/go-sql-driver/mysql v1.7.0 // indirect + git.ex.umbach.dev/Alex/roese-utils v1.0.8 + github.com/gofiber/fiber/v2 v2.49.2 + github.com/joho/godotenv v1.5.1 + gorm.io/driver/mysql v1.5.1 + gorm.io/gorm v1.25.4 +) + +require ( + github.com/andybalholm/brotli v1.0.5 // indirect + github.com/gabriel-vasile/mimetype v1.4.2 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.15.5 // indirect + github.com/go-sql-driver/mysql v1.7.1 // indirect + github.com/google/uuid v1.3.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/joho/godotenv v1.5.1 // indirect - gorm.io/driver/mysql v1.5.1 // indirect - gorm.io/gorm v1.25.4 // indirect -) + github.com/klauspost/compress v1.17.0 // indirect + github.com/leodido/go-urn v1.2.4 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/rivo/uniseg v0.4.4 // indirect + github.com/rs/zerolog v1.31.0 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasthttp v1.50.0 // indirect + github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/crypto v0.7.0 // indirect + golang.org/x/net v0.8.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/text v0.8.0 // indirect +) diff --git a/go.sum b/go.sum index 37eec79..50f43c0 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,83 @@ -github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +git.ex.umbach.dev/Alex/roese-utils v1.0.8 h1:ph18N52R9UsJ0AsJW95W6afa9qKlMDMh60xE4wlfFi8= +git.ex.umbach.dev/Alex/roese-utils v1.0.8/go.mod h1:tjq6m6lNFo0TzAvq8jHhlK48IGxi+SrlrQwf9WYg408= +github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= +github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= +github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.15.5 h1:LEBecTWb/1j5TNY1YYG2RcOUN3R7NLylN+x8TTueE24= +github.com/go-playground/validator/v10 v10.15.5/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs= +github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts= +github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= +github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM= +github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= +github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= +github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M= +github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= +github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= diff --git a/main.go b/main.go index 02f56ae..124e8fc 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,57 @@ package main import ( "fmt" + "jannex/robot-control-manager/modules/config" "jannex/robot-control-manager/modules/database" + "jannex/robot-control-manager/modules/utils" + "jannex/robot-control-manager/routers/router" + "os" + "time" + + "git.ex.umbach.dev/Alex/roese-utils/rsconfig" + "git.ex.umbach.dev/Alex/roese-utils/rslogger" + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/cors" + "github.com/gofiber/fiber/v2/middleware/logger" ) func init() { + rsconfig.CreateEnvConfigFileIfNotExists(`DEBUG=false +COLORIZED_OUTPUT=true +HOST=127.0.0.1 +PORT=8080 + +# MariaDB +MARIADB_HOSTNAME=127.0.0.1 +MARIADB_PORT=3306 +MARIADB_USERNAME=db_user +MARIADB_PASSWORD=db_password +MARIADB_DATABASE_NAME=db_database_name`) + + config.LoadConfig() + rslogger.InitLogger(config.Cfg.Debug, config.Cfg.ColorizedOutput, config.Cfg.LogManagerServerUrl) + + if os.Getenv("DOCKER") != "" { + fmt.Println("Waiting for mariadb docker") + time.Sleep(10 * time.Second) + } + database.InitDatabase() + utils.ValidatorInit() } func main() { - fmt.Println("Hello, World!") + app := fiber.New() + + app.Use(cors.New()) + + if config.Cfg.Debug { + app.Use(logger.New(logger.Config{ + Format: "${pid} ${locals:requestid} ${status} - ${latency} ${method} ${path}​\n", + })) + } + + router.SetupRoutes(app) + + app.Listen(config.Cfg.Host + ":" + config.Cfg.Port) } diff --git a/modules/cache/robots.go b/modules/cache/robots.go new file mode 100644 index 0000000..fbf9129 --- /dev/null +++ b/modules/cache/robots.go @@ -0,0 +1,23 @@ +package cache + +import ( + "jannex/robot-control-manager/modules/structs" + "sync" +) + +var robots []*structs.Robot +var rMu sync.RWMutex + +func AddRobot(robot *structs.Robot) { + rMu.Lock() + defer rMu.Unlock() + + robots = append(robots, robot) +} + +func GetRobots() []*structs.Robot { + rMu.RLock() + defer rMu.RUnlock() + + return robots +} diff --git a/modules/config/config.go b/modules/config/config.go index 45fc27a..7cebd00 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -14,6 +14,7 @@ type Config struct { ColorizedOutput bool Host string Port string + LogManagerServerUrl string MariaDB MariaDB } @@ -35,10 +36,10 @@ func LoadConfig() { } Cfg = Config{ - Debug: os.Getenv("DEBUG") == "true", - ColorizedOutput: os.Getenv("COLORIZED_OUTPUT") == "true", - Host: os.Getenv("HOST"), - Port: os.Getenv("PORT"), + Debug: os.Getenv("DEBUG") == "true", + ColorizedOutput: os.Getenv("COLORIZED_OUTPUT") == "true", + Host: os.Getenv("HOST"), + Port: os.Getenv("PORT"), MariaDB: MariaDB{ Hostname: os.Getenv("MARIADB_HOSTNAME"), Port: os.Getenv("MARIADB_PORT"), diff --git a/modules/logger/logger.go b/modules/logger/logger.go new file mode 100644 index 0000000..1a68568 --- /dev/null +++ b/modules/logger/logger.go @@ -0,0 +1,21 @@ +package logger + +import ( + "fmt" + "jannex/robot-control-manager/modules/config" + + "git.ex.umbach.dev/Alex/roese-utils/rslogger" + "github.com/gofiber/fiber/v2" +) + +func AddSystemLog(format string, v ...any) { + go rslogger.LogManagerRequestClient(fiber.MethodPost, config.Cfg.LogManagerServerUrl+"/v1/log", rslogger.LogManagerRequestBody{ + Type: "robots", + Logs: []string{"I " + rslogger.GetTime() + fmt.Sprintf(format, v...)}}) +} + +func AddRexLog(format string, v ...any) { + go rslogger.LogManagerRequestClient(fiber.MethodPost, config.Cfg.LogManagerServerUrl+"/v1/log", rslogger.LogManagerRequestBody{ + Type: "rex", + Logs: []string{"I " + rslogger.GetTime() + fmt.Sprintf(format, v...)}}) +} diff --git a/modules/structs/rex.go b/modules/structs/rex.go deleted file mode 100644 index 0737d61..0000000 --- a/modules/structs/rex.go +++ /dev/null @@ -1,8 +0,0 @@ -package structs - -type Rex struct { - Id string - Address string - JobId string - Task string -} diff --git a/modules/structs/robot.go b/modules/structs/robot.go new file mode 100644 index 0000000..63c0dc6 --- /dev/null +++ b/modules/structs/robot.go @@ -0,0 +1,23 @@ +package structs + +import "time" + +type Robot struct { + Id string + Type uint8 // robot type (0 = rex, 1 = yeet) + Name string + Status uint8 + Address string + CurrentJobId string + CurrentTask string + ConnectedAt time.Time +} + +type FirstRequestBody struct { + // robot id + Id string + // robot type (0 = rex, 1 = yeet) + Type uint8 + // connected modul like a gripper + //ConnectedModul uint8 +} diff --git a/modules/utils/validator.go b/modules/utils/validator.go new file mode 100644 index 0000000..7ba25d0 --- /dev/null +++ b/modules/utils/validator.go @@ -0,0 +1,5 @@ +package utils + +func ValidatorInit() { + +} diff --git a/routers/api/v1/robots/robots.go b/routers/api/v1/robots/robots.go new file mode 100644 index 0000000..66ffcb0 --- /dev/null +++ b/routers/api/v1/robots/robots.go @@ -0,0 +1,18 @@ +package robots + +import ( + "jannex/robot-control-manager/modules/structs" + + "git.ex.umbach.dev/Alex/roese-utils/rsutils" + "github.com/gofiber/fiber/v2" +) + +func FirstRequest(c *fiber.Ctx) error { + var body structs.FirstRequestBody + + if err := rsutils.BodyParserHelper(c, &body); err != nil { + return c.SendStatus(fiber.StatusBadRequest) + } + + return c.JSON(fiber.Map{}) +} diff --git a/routers/router/router.go b/routers/router/router.go new file mode 100644 index 0000000..3b9756f --- /dev/null +++ b/routers/router/router.go @@ -0,0 +1,14 @@ +package router + +import ( + "jannex/robot-control-manager/routers/api/v1/robots" + + "github.com/gofiber/fiber/v2" +) + +func SetupRoutes(app *fiber.App) { + v1 := app.Group("/v1") + + r := v1.Group("/robots") + r.Get("/", robots.FirstRequest) +}