Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
91 lines (75 loc) · 1.92 KB
/
Copy pathmain.go
File metadata and controls
91 lines (75 loc) · 1.92 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
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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
var APP_VERSION string = "latest"
var COMMIT_ID string = "undefined"
var ws *WebServer
const (
contentPath = "./content"
outputPath = "./output"
)
func main() {
// Initialize the WebService structure
ws = new(WebServer)
ws.Initialize()
// Determine mode based on command line argument
// No CLI flags - just check os.Args
if len(os.Args) > 1 && os.Args[1] == "serve" {
// Serve mode: Start HTTP server with file watching
runServeMode()
} else {
// CLI mode: Generate static files and exit
runCLIMode()
}
}
func runCLIMode() {
fmt.Println("Running in CLI mode...")
fmt.Printf("Reading content from: %s\n", contentPath)
fmt.Printf("Writing output to: %s\n", outputPath)
// Read content and generate output
err := generateOutput()
if err != nil {
fmt.Printf("Error generating output: %s\n", err)
os.Exit(1)
}
fmt.Println("Generation complete.")
}
func runServeMode() {
fmt.Println("Running in Serve mode...")
// Generate initial output
fmt.Println("Generating initial output...")
if err := generateOutput(); err != nil {
fmt.Printf("Error generating initial output: %s\n", err)
}
// Start file watcher for live reload
watcher, err := startWatcher()
if err != nil {
fmt.Printf("Warning: Failed to start file watcher: %s\n", err)
}
if watcher != nil {
defer watcher.Close()
}
// Create a channel to receive the OS signals
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
// Start the WebService in a separate goroutine
go ws.Start()
// Wait for a signal
<-sc
fmt.Println("Shutting down...")
// Create a context with a timeout for graceful shutdown
shCtx, shCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shCancel()
// Shutdown the HTTP server
err = ws.HTTPServer.Shutdown(shCtx)
if err != nil {
fmt.Printf("Server shutdown error: %s", err)
os.Exit(1)
}
}