package main import ( "fmt" "log" "time" "github.com/labstack/echo" "github.com/nats-io/nats.go" "github.com/rosti-cz/node-api/common" "github.com/rosti-cz/node-api/node" ) // JSONIndent Indendation of JSON output format const JSONIndent = " " var config common.Config var nc *nats.Conn func _init() { var err error // Load config from environment variables config = *common.GetConfig() // Connect to the NATS service nc, err = nats.Connect(config.NATSURL) if err != nil { log.Fatalln(err) } } func main() { _init() defer nc.Drain() // 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(300 * 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) // NATS handling // admin.apps.ALIAS.events // admin.apps.ALIAS.states subjectEvents := fmt.Sprintf("admin.apps.%s.requests", config.NATSAlias) log.Println("> listening on " + subjectEvents) nc.Subscribe(subjectEvents, messageHandler) // 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) // Enable one of the supported technologies or services (python, node, redis, ...) e.PUT("/v1/apps/:name/set-services", setServicesHandler) // Rebuilds existing app, it keeps the data but creates 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")) }