node-api/main.go

111 lines
2.5 KiB
Go
Raw Normal View History

2020-07-08 22:09:19 +00:00
package main
import (
2020-07-11 21:14:45 +00:00
"log"
2020-07-16 17:05:38 +00:00
"time"
2020-07-08 22:09:19 +00:00
"github.com/labstack/echo"
2020-07-13 22:01:42 +00:00
"github.com/rosti-cz/node-api/common"
2020-07-21 21:11:56 +00:00
"github.com/rosti-cz/node-api/node"
2020-07-08 22:09:19 +00:00
)
// JSONIndent Indendation of JSON output format
const JSONIndent = " "
func main() {
// Close database at the end
db := common.GetDBConnection()
defer db.Close()
2020-07-13 22:01:42 +00:00
// Templating
t := &Template{}
2020-07-16 17:05:38 +00:00
// Stats loop
go func() {
2020-07-21 21:11:56 +00:00
for {
log.Println("Stats gathering started")
start := time.Now()
err := gatherStats()
2020-07-21 21:11:56 +00:00
if err != nil {
log.Println("LOOP ERROR:", err.Error())
}
elapsed := time.Since(start)
log.Printf("Stats gathering elapsed time: %.2fs\n", elapsed.Seconds())
time.Sleep(30 * time.Second)
2020-07-21 21:11:56 +00:00
}
}()
// Node stats
go func() {
for {
err := node.Log()
if err != nil {
log.Println("NODE PERFORMANCE LOG ERROR:", err.Error())
}
time.Sleep(5 * time.Minute)
2020-07-16 17:05:38 +00:00
}
}()
2020-07-08 22:09:19 +00:00
// API
e := echo.New()
2020-07-13 22:01:42 +00:00
e.Renderer = t
e.Use(TokenMiddleware)
2020-07-08 22:09:19 +00:00
// UI
e.GET("/", homeHandler)
2020-07-08 22:09:19 +00:00
// Returns list of apps
e.GET("/v1/apps", listAppsHandler)
2020-07-08 22:09:19 +00:00
// Returns one app
e.GET("/v1/apps/:name", getAppHandler)
2020-07-08 22:09:19 +00:00
// Create a new app
2020-08-18 20:52:34 +00:00
// If you add register_only=1 into query string, it won't start or create any container, just adds record into the database.
e.POST("/v1/apps", createAppHandler)
2020-07-08 22:09:19 +00:00
// Update existing app
e.PUT("/v1/apps/:name", updateAppHandler)
2020-07-08 22:09:19 +00:00
// Stop existing app
e.PUT("/v1/apps/:name/stop", stopAppHandler)
2020-07-08 22:09:19 +00:00
// Start existing app
e.PUT("/v1/apps/:name/start", startAppHandler)
2020-07-08 22:09:19 +00:00
// Stop existing app
e.PUT("/v1/apps/:name/restart", restartAppHandler)
2020-07-11 21:14:45 +00:00
// Application processes
e.GET("/v1/apps/:name/processes", getAppProcessesHandler)
2020-07-08 22:09:19 +00:00
// Set password for the app user in the container
e.PUT("/v1/apps/:name/password", setPasswordHandler)
// Copies body of the request into /srv/.ssh/authorized_keys
e.PUT("/v1/apps/:name/keys", setKeysHandler)
e.PUT("/v1/apps/:name/set-services", setServicesHandler)
2020-07-08 22:09:19 +00:00
// Rebuilds existing app, it keeps the data but created the container again
e.PUT("/v1/apps/:name/rebuild", rebuildAppHandler)
2020-07-08 22:09:19 +00:00
2020-07-16 21:24:09 +00:00
// Adds new label
e.POST("/v1/apps/:name/labels", addLabelHandler)
2020-07-16 21:24:09 +00:00
// Removes existing label
e.DELETE("/v1/apps/:name/labels", deleteLabelHandler)
2020-07-16 21:24:09 +00:00
2020-07-08 22:09:19 +00:00
// Delete one app
e.DELETE("/v1/apps/:name", deleteAppHandler)
2020-07-08 22:09:19 +00:00
2020-07-13 22:01:42 +00:00
// Orphans returns directories in /srv that doesn't match any hosted application
e.GET("/v1/orphans", getOrphansHander)
2020-07-13 22:01:42 +00:00
2020-07-08 22:09:19 +00:00
// Return info about the node including performance index
e.GET("/v1/node", getNodeInfoHandler)
2020-07-08 22:09:19 +00:00
e.Logger.Fatal(e.Start(":1323"))
2020-07-08 22:09:19 +00:00
}