-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (129 loc) · 4.39 KB
/
main.go
File metadata and controls
156 lines (129 loc) · 4.39 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
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
148
149
150
151
152
153
154
155
156
// REST API for job queue management
// @title ScaleODM Job Queue API
// @version 0.2.0
// @description NodeODM-compatible API for managing distributed ODM jobs via Argo Workflows
// @contact.name Sam Woodcock
// @contact.url https://slack.hotosm.org
// @license.name AGPL-3.0-only
// @license.url https://opensource.org/licenses/agpl-v3
// @host localhost:31100
// @BasePath /api/v1
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/danielgtaylor/huma/v2/humacli"
"github.com/hotosm/scaleodm/app/api"
"github.com/hotosm/scaleodm/app/config"
"github.com/hotosm/scaleodm/app/db"
"github.com/hotosm/scaleodm/app/meta"
"github.com/hotosm/scaleodm/app/workflows"
)
// Huma CLI Options
type Options struct {
Port int `help:"Port to listen on" short:"p" default:"31100"`
}
func main() {
// Log to stdout for Docker (unbuffered)
log.SetOutput(os.Stdout)
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
startTime := time.Now()
log.Println("Starting ScaleODM...")
// Graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
// Validate environment variables
log.Println("Validating environment variables...")
config.ValidateEnv()
log.Printf("Environment validation complete (took %v)", time.Since(startTime))
// Database connection
log.Println("Connecting to database...")
dbStart := time.Now()
database, err := db.NewDB(config.SCALEODM_DATABASE_URL)
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
defer database.Close()
log.Printf("Database connection established (took %v)", time.Since(dbStart))
// Initialize schema
log.Println("Initializing database schema...")
schemaStart := time.Now()
if err := database.InitSchema(ctx); err != nil {
log.Fatalf("Failed to initialize schema: %v", err)
}
log.Printf("Schema initialization complete (took %v)", time.Since(schemaStart))
// Create metadata store
log.Println("Creating metadata store...")
metaStart := time.Now()
metadataStore := meta.NewStore(database)
log.Printf("Metadata store initialized (took %v)", time.Since(metaStart))
docsOnly := strings.EqualFold(os.Getenv("SCALEODM_DOCS_ONLY"), "true")
var wfClient workflows.WorkflowClient
if docsOnly {
log.Println("SCALEODM_DOCS_ONLY=true, skipping Argo Workflows client initialization")
} else {
// Initialize Argo Workflows client
log.Println("Initializing Argo Workflows client...")
k8sStart := time.Now()
wfClient, err = workflows.NewClient(config.KUBECONFIG_PATH, config.K8S_NAMESPACE)
if err != nil {
log.Fatalf("Failed to initialize Argo Workflows client: %v", err)
}
log.Printf("Argo Workflows client ready (took %v)", time.Since(k8sStart))
}
// === HUMA CLI ===
// Channel to communicate the *http.Server back from the OnStart hook so
// we can shut it down gracefully when we receive a signal.
serverCh := make(chan *http.Server, 1)
cli := humacli.New(func(hooks humacli.Hooks, options *Options) {
// Create API (register routes and get the HTTP handler)
apiObj, handler := api.NewAPI(metadataStore, wfClient)
_ = apiObj
srv := &http.Server{
Addr: fmt.Sprintf(":%d", options.Port),
Handler: handler,
ReadHeaderTimeout: 10 * time.Second,
}
hooks.OnStart(func() {
log.Printf("API server starting on :%d", options.Port)
log.Printf(" Docs: http://localhost:%d/", options.Port)
log.Printf(" OpenAPI: http://localhost:%d/openapi.json", options.Port)
serverCh <- srv
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("HTTP server failed: %v", err)
}
})
// Graceful shutdown
hooks.OnStop(func() {
log.Println("Shutting down API server...")
})
})
// Start CLI in background
go cli.Run()
// Wait for shutdown signal
<-sigCh
log.Println("Received shutdown signal...")
// Cancel context
cancel()
// Gracefully shut down the HTTP server with a deadline
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()
select {
case srv := <-serverCh:
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("HTTP server shutdown error: %v", err)
}
default:
// Server never started
}
log.Println("Shutdown complete")
}