node-api/main.go
Adam Štrauch 66476275e4
All checks were successful
continuous-integration/drone/push Build is passing
Fix of snapshotProcessor initiation
Second try
2021-11-01 01:17:35 +01:00

175 lines
4.0 KiB
Go

package main
import (
"fmt"
"log"
"time"
"github.com/labstack/echo"
"github.com/nats-io/nats.go"
"github.com/rosti-cz/node-api/apps"
"github.com/rosti-cz/node-api/apps/drivers"
"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
var snapshotProcessor apps.SnapshotProcessor
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)
}
// Database migrations
processor := apps.AppsProcessor{
DB: common.GetDBConnection(),
}
processor.Init()
// Prepare snapshot processor
snapshotProcessor = apps.SnapshotProcessor{
AppsPath: config.AppsPath,
TmpSnapshotPath: config.SnapshotsPath,
IndexLabel: config.SnapshotsIndexLabel,
Driver: drivers.S3Driver{
S3AccessKey: config.SnapshotsS3AccessKey,
S3SecretKey: config.SnapshotsS3SecretKey,
S3Endpoint: config.SnapshotsS3Endpoint,
S3SSL: config.SnapshotsS3SSL,
Bucket: config.SnapshotsS3Bucket,
},
}
}
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)
err := e.Start(":1323")
// fmt.Println(err.Error())
// if strings.Contains(err.Error(), "SIGTERM") {
// e.Logger.Info(err)
// os.Exit(0)
// }
e.Logger.Fatal(err)
}