add app state, and fix a few bugs

This commit is contained in:
Zoe
2025-05-03 19:03:09 -05:00
parent c891c24843
commit 4ab58f6324
12 changed files with 143 additions and 83 deletions

View File

@@ -482,6 +482,14 @@ func (flux *FluxServer) StartApp(w http.ResponseWriter, r *http.Request) {
return
}
app.State = "running"
_, err = flux.db.ExecContext(r.Context(), "UPDATE apps SET state = ? WHERE id = ?", app.State, app.Id[:])
if err != nil {
flux.logger.Errorw("Failed to update app state", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = app.Deployment.Start(r.Context(), flux.docker)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -529,6 +537,14 @@ func (flux *FluxServer) StopApp(w http.ResponseWriter, r *http.Request) {
return
}
app.State = "stopped"
_, err = flux.db.ExecContext(r.Context(), "UPDATE apps SET state = ? WHERE id = ?", app.State, app.Id[:])
if err != nil {
flux.logger.Errorw("Failed to update app state", zap.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = app.Deployment.Stop(r.Context(), flux.docker)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -572,5 +588,7 @@ func (flux *FluxServer) DeleteDeployHandler(w http.ResponseWriter, r *http.Reque
return
}
flux.proxy.RemoveDeployment(app.Deployment.URL)
w.WriteHeader(http.StatusOK)
}

View File

@@ -7,12 +7,14 @@ CREATE TABLE IF NOT EXISTS deployments (
CREATE TABLE IF NOT EXISTS apps (
id BLOB PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
state TEXT NOT NULL,
deployment_id INTEGER,
FOREIGN KEY(deployment_id) REFERENCES deployments(id)
);
CREATE TABLE IF NOT EXISTS containers (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
friendly_name TEXT NOT NULL,
container_id TEXT NOT NULL,
head BOOLEAN NOT NULL,
deployment_id INTEGER NOT NULL,

View File

@@ -43,7 +43,7 @@ type FluxServer struct {
db *sql.DB
docker *docker.DockerClient
// TODO: implement
proxy *proxyManagerService.ProxyManager
appManager *appManagerService.AppManager
@@ -147,7 +147,10 @@ func NewServer() *FluxServer {
flux.proxy = proxyManagerService.NewProxyManager(flux.logger)
flux.appManager = appManagerService.NewAppManager(flux.db, flux.docker, flux.proxy, flux.logger)
flux.appManager.Init()
err = flux.appManager.Init()
if err != nil {
flux.logger.Fatalw("Failed to initialize apps", zap.Error(err))
}
return flux
}