node-api/main.go

181 lines
4.7 KiB
Go
Raw Normal View History

2020-07-08 22:09:19 +00:00
package main
import (
"net/http"
"github.com/labstack/echo"
"github.com/rosti-cz/apps-api/apps"
"github.com/rosti-cz/apps-api/common"
2020-07-11 11:04:37 +00:00
"github.com/rosti-cz/apps-api/docker"
2020-07-08 22:09:19 +00:00
"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()
2020-07-11 11:04:37 +00:00
// db.AutoMigrate(apps.Label{})
// db.AutoMigrate(apps.App{})
2020-07-08 22:09:19 +00:00
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 {
2020-07-09 22:27:23 +00:00
if validationError, ok := err.(apps.ValidationError); ok {
return c.JSONPretty(http.StatusBadRequest, Message{Errors: validationError.Errors}, JSONIndent)
}
2020-07-08 22:09:19 +00:00
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
2020-07-11 11:04:37 +00:00
container := docker.Container{
App: app,
}
err = container.Create()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
err = container.Start()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
return c.JSON(http.StatusOK, Message{Message: "ok"})
2020-07-08 22:09:19 +00:00
})
// 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)
}
2020-07-11 11:04:37 +00:00
appPointer, err := apps.Update(name, app.SSHPort, app.HTTPPort, app.Image, app.CPU, app.Memory)
2020-07-08 22:09:19 +00:00
if err != nil {
2020-07-09 22:27:23 +00:00
if validationError, ok := err.(apps.ValidationError); ok {
return c.JSONPretty(http.StatusBadRequest, Message{Errors: validationError.Errors}, JSONIndent)
}
2020-07-08 22:09:19 +00:00
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
2020-07-11 11:04:37 +00:00
app = *appPointer
container := docker.Container{
App: app,
}
err = container.Destroy()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
err = container.Create()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
err = container.Start()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
2020-07-08 22:09:19 +00:00
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 {
2020-07-11 11:04:37 +00:00
name := c.Param("name")
app, err := apps.Get(name)
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
container := docker.Container{
App: *app,
}
err = container.Start()
if err != nil {
return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent)
}
return c.JSON(http.StatusOK, Message{Message: "ok"})
2020-07-08 22:09:19 +00:00
})
// 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"))
}