node-api/main.go
2021-04-02 18:27:57 +02:00

114 lines
2.5 KiB
Go

package main
import (
"log"
"time"
"github.com/labstack/echo"
"github.com/rosti-cz/node-api/common"
"github.com/rosti-cz/node-api/node"
)
// JSONIndent Indendation of JSON output format
const JSONIndent = " "
func main() {
// Close database at the end
db := common.GetDBConnection()
defer db.Close()
// Templating
t := &Template{}
// Stats loop
go func() {
for {
log.Println("Stats gathering started")
start := time.Now()
err := gatherStats()
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)
}
}()
// Node stats
go func() {
for {
err := node.Log()
if err != nil {
log.Println("NODE PERFORMANCE LOG ERROR:", err.Error())
}
time.Sleep(5 * time.Minute)
}
}()
// API
e := echo.New()
e.Renderer = t
e.Use(TokenMiddleware)
// UI
e.GET("/", homeHandler)
// Prometheus metrics
e.GET("/metrics", metricsHandler)
// Returns list of apps
e.GET("/v1/apps", listAppsHandler)
// Returns one app
e.GET("/v1/apps/:name", getAppHandler)
// Create a new app
// 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)
// Update existing app
e.PUT("/v1/apps/:name", updateAppHandler)
// Stop existing app
e.PUT("/v1/apps/:name/stop", stopAppHandler)
// Start existing app
e.PUT("/v1/apps/:name/start", startAppHandler)
// Stop existing app
e.PUT("/v1/apps/:name/restart", restartAppHandler)
// Application processes
e.GET("/v1/apps/:name/processes", getAppProcessesHandler)
// 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)
// Rebuilds existing app, it keeps the data but created the container again
e.PUT("/v1/apps/:name/rebuild", rebuildAppHandler)
// Adds new label
e.POST("/v1/apps/:name/labels", addLabelHandler)
// Removes existing label
e.DELETE("/v1/apps/:name/labels", deleteLabelHandler)
// Delete one app
e.DELETE("/v1/apps/:name", deleteAppHandler)
// Orphans returns directories in /srv that doesn't match any hosted application
e.GET("/v1/orphans", getOrphansHander)
// Return info about the node including performance index
e.GET("/v1/node", getNodeInfoHandler)
e.Logger.Fatal(e.Start(":1323"))
}