@@ -2,29 +2,126 @@ package service
22
33import (
44 "context"
5+ "fmt"
6+ "html/template"
57 "log/slog"
8+ "net/http"
69 "os"
710 "os/signal"
811 "syscall"
912 "time"
1013
14+ "github.com/dkrizic/feature/ui/constant"
15+ "github.com/dkrizic/feature/ui/meta"
16+ featurev1 "github.com/dkrizic/feature/ui/repository/feature/v1"
17+ metav1 "github.com/dkrizic/feature/ui/repository/meta/v1"
1118 "github.com/urfave/cli/v3"
19+ "google.golang.org/grpc"
20+ "google.golang.org/grpc/credentials/insecure"
1221)
1322
23+ // Server holds the HTTP server and gRPC clients.
24+ type Server struct {
25+ address string
26+ templates * template.Template
27+ featureClient featurev1.FeatureClient
28+ metaClient metav1.MetaClient
29+ backendVersion string
30+ uiVersion string
31+ httpServer * http.Server
32+ }
33+
34+ // Service is the CLI entrypoint for starting the HTTP UI service.
1435func Service (ctx context.Context , cmd * cli.Command ) error {
15- // get the port
16- port := cmd .Int ("port" )
17- slog .InfoContext (ctx , "Starting the feature UI service" , "port" , port )
36+ port := cmd .Int (constant .Port )
37+ endpoint := cmd .String (constant .Endpoint )
1838
19- cancelChan := make (chan os.Signal , 1 )
20- // catch SIGETRM or SIGINTERRUPT
21- signal .Notify (cancelChan , syscall .SIGTERM , syscall .SIGINT )
39+ slog .Info ("Starting the feature UI service" , "port" , port , "endpoint" , endpoint )
40+
41+ // Parse templates
42+ templates := ParseTemplates ()
43+ if templates == nil {
44+ return fmt .Errorf ("failed to parse templates" )
45+ }
46+
47+ // Dial the gRPC backend
48+ conn , err := grpc .NewClient (endpoint , grpc .WithTransportCredentials (insecure .NewCredentials ()))
49+ if err != nil {
50+ slog .Error ("Failed to dial gRPC backend" , "endpoint" , endpoint , "error" , err )
51+ return fmt .Errorf ("failed to dial gRPC backend: %w" , err )
52+ }
53+ defer conn .Close ()
54+
55+ // Initialize gRPC clients
56+ featureClient := featurev1 .NewFeatureClient (conn )
57+ metaClient := metav1 .NewMetaClient (conn )
58+
59+ // Fetch backend version
60+ backendVersion := ""
61+ metaCtx , cancel := context .WithTimeout (ctx , 5 * time .Second )
62+ defer cancel ()
63+ metaResp , err := metaClient .Meta (metaCtx , & metav1.MetaRequest {})
64+ if err != nil {
65+ slog .Warn ("Failed to fetch backend version" , "error" , err )
66+ } else {
67+ backendVersion = metaResp .Version
68+ slog .Info ("Backend version retrieved" , "version" , backendVersion )
69+ }
70+
71+ // Create server
72+ server := & Server {
73+ address : fmt .Sprintf (":%d" , port ),
74+ templates : templates ,
75+ featureClient : featureClient ,
76+ metaClient : metaClient ,
77+ backendVersion : backendVersion ,
78+ uiVersion : meta .Version ,
79+ }
80+
81+ // Setup HTTP routes
82+ mux := http .NewServeMux ()
83+ server .registerHandlers (mux )
84+
85+ // Create HTTP server
86+ server .httpServer = & http.Server {
87+ Addr : server .address ,
88+ Handler : mux ,
89+ }
90+
91+ // Start HTTP server in a goroutine
92+ errChan := make (chan error , 1 )
2293 go func () {
23- slog .Info ("Running" )
24- time .Sleep (5 * time .Second )
94+ slog .Info ("HTTP server listening" , "address" , server .address )
95+ if err := server .httpServer .ListenAndServe (); err != nil && err != http .ErrServerClosed {
96+ errChan <- err
97+ }
2598 }()
26- sig := <- cancelChan
27- slog .Info ("Shutting down feature UI service" , "signal" , sig )
28- return nil
2999
100+ // Setup signal handling for graceful shutdown
101+ cancelChan := make (chan os.Signal , 1 )
102+ signal .Notify (cancelChan , syscall .SIGTERM , syscall .SIGINT )
103+
104+ // Wait for shutdown signal or error
105+ select {
106+ case <- ctx .Done ():
107+ slog .Info ("Context canceled, shutting down" )
108+ case sig := <- cancelChan :
109+ slog .Info ("Received signal, shutting down" , "signal" , sig )
110+ case err := <- errChan :
111+ slog .Error ("HTTP server error" , "error" , err )
112+ return err
113+ }
114+
115+ // Graceful shutdown
116+ shutdownCtx , shutdownCancel := context .WithTimeout (context .Background (), 10 * time .Second )
117+ defer shutdownCancel ()
118+
119+ slog .Info ("Shutting down HTTP server gracefully" )
120+ if err := server .httpServer .Shutdown (shutdownCtx ); err != nil {
121+ slog .Error ("Failed to shutdown HTTP server gracefully" , "error" , err )
122+ return err
123+ }
124+
125+ slog .Info ("Feature UI service stopped" )
126+ return nil
30127}
0 commit comments