-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathswitch_mode.go
More file actions
49 lines (40 loc) · 1.51 KB
/
switch_mode.go
File metadata and controls
49 lines (40 loc) · 1.51 KB
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
package server
import "errors"
// CreateTemplateServer switches from Internal to External mode.
// It generates the server files (if not present), compiles, and runs them.
// This implements the transition from "Internal" to "External" mode.
func (h *ServerHandler) CreateTemplateServer() error {
h.strategyMu.Lock()
if !h.executionInternal {
h.strategyMu.Unlock()
h.log("Server is already in external mode.")
return nil
}
h.log("Stopping Internal Server...")
// Stop the current internal server
if err := h.strategy.Stop(); err != nil {
return errors.Join(errors.New("failed to stop internal server"), err)
}
h.log("Generating server files...")
// Generate the physical files for the server
if err := h.generateServerFromEmbeddedMarkdown(); err != nil {
return errors.Join(errors.New("failed to generate server files"), err)
}
h.log("Switching to External Process Strategy...")
// Switch strategy state
h.executionInternal = false
h.strategy = newExternalStrategy(h)
// Capture strategy to call Start on it without needing the lock
s := h.strategy
h.strategyMu.Unlock()
h.log("Starting External Server...")
// Start the new external server (compiles and runs)
// We pass nil for wg because this is a runtime transition, not application startup
if err := s.Start(nil); err != nil {
// If start fails, should we try to revert?
// For now, return the error.
return errors.Join(errors.New("failed to start external server"), err)
}
h.log("Server successfully switched to External mode.")
return nil
}