-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
147 lines (117 loc) · 3.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// main.go
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/template/html/v2"
"github.com/pterm/pterm"
"github.com/spf13/afero"
)
var (
currentProject Project
comfyUICmd *exec.Cmd
devMode bool
)
// main is the entry point of the application.
func main() {
flag.BoolVar(&devMode, "devmode", false, "Run the application in development mode")
flag.Parse()
displayBanner()
config, err := loadConfig(localFs, "config.yml")
if err != nil {
logFatalError("Error loading config", err)
}
initializeApplication(config)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
if err := startComfyUI(ctx, config); err != nil {
logFatalError("Failed to start ComfyUI", err)
}
pterm.Info.Printf("Serving frontend on: %s:%s\n", config.ControlHost, config.ControlPort)
pterm.Info.Println("Press Ctrl+C to stop")
modelParams, err := loadModelParams(config)
if err != nil {
logFatalError("Failed to load model parameters", err)
}
runFrontendServer(ctx, config, modelParams)
pterm.Info.Println("Shutdown signal received")
if err := stopComfyUI(ctx); err != nil {
pterm.Error.Println("Failed to stop ComfyUI:", err)
}
os.Exit(0)
}
// runFrontendServer starts the Fiber frontend server.
func runFrontendServer(ctx context.Context, config *AppConfig, modelParams []ModelParams) {
app := createFiberApp(config)
setupRoutes(app, config, modelParams)
go handleGracefulShutdown(ctx, app, config, modelParams)
addr := fmt.Sprintf("%s:%s", config.ControlHost, config.ControlPort)
if err := app.Listen(addr); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("Frontend server failed: %v", err)
}
pterm.Info.Println("Server gracefully shutdown")
}
// createFiberApp initializes and returns a new Fiber application.
func createFiberApp(config *AppConfig) *fiber.App {
basePath := filepath.Join(config.DataPath, "web")
baseFs := afero.NewBasePathFs(afero.NewOsFs(), basePath)
httpFs := afero.NewHttpFs(baseFs)
engine := html.NewFileSystem(httpFs, ".html")
app := fiber.New(fiber.Config{
AppName: "Eternal v0.1.0",
BodyLimit: 100 * 1024 * 1024,
DisableStartupMessage: true,
ServerHeader: "Eternal",
PassLocalsToViews: true,
Views: engine,
StrictRouting: true,
StreamRequestBody: true,
})
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "*",
}))
app.Use("/public", filesystem.New(filesystem.Config{
Root: httpFs,
Index: "index.html",
Browse: true,
}))
app.Static("/", "public")
return app
}
// handleGracefulShutdown handles the graceful shutdown of the application.
func handleGracefulShutdown(ctx context.Context, app *fiber.App, config *AppConfig, modelParams []ModelParams) {
<-ctx.Done()
if devMode {
cleanupDevMode(config, modelParams)
}
if err := app.Shutdown(); err != nil {
log.Fatalf("Server shutdown failed: %v", err)
}
}
// cleanupDevMode performs cleanup tasks specific to development mode.
func cleanupDevMode(config *AppConfig, modelParams []ModelParams) {
deleteFile(filepath.Join(config.DataPath, "search.bleve"))
deleteFile(filepath.Join(config.DataPath, "eternaldata.db"))
for _, model := range modelParams {
if model.Downloaded {
cachePath := filepath.Join(config.DataPath, "models", model.Name, "cache")
if fileExists(cachePath) {
pterm.Warning.Printf("Deleting cache: %s\n", cachePath)
deleteFile(cachePath)
}
}
}
}