From 837f629d8d0ec9eee58fdb93333bae4e0725bc5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Thu, 9 Jul 2020 00:09:19 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 ++ README.md | 0 apps/main.go | 87 ++++++++++++++++++++++++++++++++++++ apps/types.go | 26 +++++++++++ common/db.go | 25 +++++++++++ go.mod | 10 +++++ go.sum | 56 +++++++++++++++++++++++ main.go | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++ nodes/main.go | 15 +++++++ types.go | 5 +++ 10 files changed, 347 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 apps/main.go create mode 100644 apps/types.go create mode 100644 common/db.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 nodes/main.go create mode 100644 types.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae00835 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +rosti.db +apps-api +.history/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/apps/main.go b/apps/main.go new file mode 100644 index 0000000..0f794ca --- /dev/null +++ b/apps/main.go @@ -0,0 +1,87 @@ +package apps + +import "github.com/rosti-cz/apps-api/common" + +func init() { + db := common.GetDBConnection() + db.AutoMigrate(Label{}) + db.AutoMigrate(App{}) +} + +// Get returns one app +func Get(name string) (*App, error) { + var app App + + db := common.GetDBConnection() + + err := db.First(&app).Where("name = ?", name).Error + if err != nil { + return nil, err + } + + return &app, nil +} + +// List returns all apps located on this node +func List() (*[]App, error) { + var apps []App + + db := common.GetDBConnection() + + err := db.Find(&apps).Error + if err != nil { + return nil, err + } + + return &apps, nil +} + +// New creates new record about application in the database +func New(name string, SSHPort int, HTTPPort int, image string, CPU string, memory int) error { + app := App{ + Name: name, + SSHPort: SSHPort, + HTTPPort: HTTPPort, + Image: image, + CPU: CPU, + Memory: memory, + } + + db := common.GetDBConnection() + + if err := db.Create(app).Error; err != nil { + return err + } + + return nil +} + +// Update changes value about app in the database +func Update(name string, SSHPort int, HTTPPort int, image string, CPU string, memory int) error { + var app App + + db := common.GetDBConnection() + + err := db.First(&app).Where("name = ?", name).Error + if err != nil { + return err + } + + err = db.Model(&app).Updates(App{ + SSHPort: SSHPort, + HTTPPort: HTTPPort, + Image: image, + CPU: CPU, + Memory: memory, + }).Error + + return err +} + +// Delete removes records about one app from the database +func Delete(name string) error { + db := common.GetDBConnection() + + err := db.Delete(App{}).Where("name = ?", name).Error + return err +} diff --git a/apps/types.go b/apps/types.go new file mode 100644 index 0000000..1b40e19 --- /dev/null +++ b/apps/types.go @@ -0,0 +1,26 @@ +package apps + +import "github.com/jinzhu/gorm" + +// Label holds metadata about the application +type Label struct { + Value string `json:"value"` +} + +// App keeps info about hosted application +type App struct { + gorm.Model + + Name string `json:"name" gorm:"primary_key"` + SSHPort int `json:"ssh_port"` + HTTPPort int `json:"http_port"` + Image string `json:"image"` + Status string `json:"status"` // running, data (no container, data only), stopped + CPU string `json:"cpu"` + Memory int `json:"memory"` // Limit in MB + Labels []Label `json:"labels"` // username:cx or user_id:1 + + MemoryUsage int `json:"memory_usage"` // Usage in MB + DiskUsage int `json:"disk_usage"` // Usage in MB + +} diff --git a/common/db.go b/common/db.go new file mode 100644 index 0000000..249866c --- /dev/null +++ b/common/db.go @@ -0,0 +1,25 @@ +package common + +import ( + "log" + + "github.com/jinzhu/gorm" + // This is line from GORM documentation that imports database dialect + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +var db *gorm.DB + +func init() { + var err error + + db, err = gorm.Open("sqlite3", "rosti.db") + if err != nil { + log.Fatalln(err) + } +} + +// GetDBConnection returns opened connection to the database +func GetDBConnection() *gorm.DB { + return db +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5f75b66 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/rosti-cz/apps-api + +go 1.14 + +require ( + github.com/jinzhu/gorm v1.9.14 + github.com/labstack/echo v3.3.10+incompatible + github.com/labstack/gommon v0.3.0 // indirect + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c8221a9 --- /dev/null +++ b/go.sum @@ -0,0 +1,56 @@ +github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= +github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= +github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/jinzhu/gorm v1.9.14 h1:Kg3ShyTPcM6nzVo148fRrcMO6MNKuqtOUwnzqMgVniM= +github.com/jinzhu/gorm v1.9.14/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= +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.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/labstack/echo v1.4.4 h1:1bEiBNeGSUKxcPDGfZ/7IgdhJJZx8wV/pICJh4W2NJI= +github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg= +github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s= +github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= +github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= +github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= +github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= +github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +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/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8= +github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k= +golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6167659 --- /dev/null +++ b/main.go @@ -0,0 +1,120 @@ +package main + +import ( + "net/http" + + "github.com/labstack/echo" + "github.com/rosti-cz/apps-api/apps" + "github.com/rosti-cz/apps-api/common" + "github.com/rosti-cz/apps-api/nodes" +) + +// JSONIndent Indendation of JSON output format +const JSONIndent = " " + +func main() { + // Close database at the end + db := common.GetDBConnection() + defer db.Close() + + // API + e := echo.New() + + // Returns list of apps + e.GET("/v1/apps", func(c echo.Context) error { + apps, err := apps.List() + + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, apps) + }) + + // Returns one app + e.GET("/v1/apps/:name", func(c echo.Context) error { + name := c.Param("name") + + app, err := apps.Get(name) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, app) + }) + + // Create a new app + e.POST("/v1/apps", func(c echo.Context) error { + app := apps.App{} + err := c.Bind(&app) + if err != nil { + return c.JSONPretty(http.StatusBadRequest, Message{Message: err.Error()}, JSONIndent) + } + + err = apps.New(app.Name, app.SSHPort, app.HTTPPort, app.Image, app.CPU, app.Memory) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, map[string]string{}) + }) + + // Update existing app + e.PUT("/v1/apps/:name", func(c echo.Context) error { + name := c.Param("name") + + app := apps.App{} + err := c.Bind(&app) + if err != nil { + return c.JSONPretty(http.StatusBadRequest, Message{Message: err.Error()}, JSONIndent) + } + + err = apps.Update(name, app.SSHPort, app.HTTPPort, app.Image, app.CPU, app.Memory) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, map[string]string{}) + }) + + // Stop existing app + e.PUT("/v1/apps/:name/stop", func(c echo.Context) error { + return c.JSON(http.StatusOK, map[string]string{}) + }) + + // Start existing app + e.PUT("/v1/apps/:name/start", func(c echo.Context) error { + return c.JSON(http.StatusOK, map[string]string{}) + }) + + // Stop existing app + e.PUT("/v1/apps/:name/restart", func(c echo.Context) error { + return c.JSON(http.StatusOK, map[string]string{}) + }) + + // Rebuilds existing app, it keeps the data but created the container again + e.PUT("/v1/apps/:name/rebuild", func(c echo.Context) error { + return c.JSON(http.StatusOK, map[string]string{}) + }) + + // Delete one app + e.DELETE("/v1/apps/:name", func(c echo.Context) error { + name := c.Param("name") + + err := apps.Delete(name) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, Message{Message: "deleted"}) + }) + + // Return info about the node including performance index + e.GET("/v1/node", func(c echo.Context) error { + node := nodes.GetNodeInfo() + + return c.JSON(http.StatusOK, node) + }) + + e.Logger.Fatal(e.Start(":1323")) +} diff --git a/nodes/main.go b/nodes/main.go new file mode 100644 index 0000000..709fb6e --- /dev/null +++ b/nodes/main.go @@ -0,0 +1,15 @@ +package nodes + +// Node keeps info about server +type Node struct { + PerformanceIndex int +} + +// GetNodeInfo returns information about this node +func GetNodeInfo() *Node { + node := Node{ + PerformanceIndex: 0, + } + + return &node +} diff --git a/types.go b/types.go new file mode 100644 index 0000000..1751eee --- /dev/null +++ b/types.go @@ -0,0 +1,5 @@ +package main + +type Message struct { + Message string `json:"message"` +}