|
| 1 | +// Copyright 2020-2025 Politecnico di Torino |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package main contains the entrypoint for the cloud image registry. |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "flag" |
| 21 | + "net/http" |
| 22 | + "os" |
| 23 | + "os/signal" |
| 24 | + "syscall" |
| 25 | + "time" |
| 26 | + |
| 27 | + "k8s.io/klog/v2" |
| 28 | + |
| 29 | + "github.com/netgroup-polito/CrownLabs/operators/pkg/ciregistry" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + dataRoot = flag.String("data-root", "/data", "Root data path for the server") |
| 34 | + listenerAddr = flag.String("listener-addr", ":8080", "Address for the server to listen on") |
| 35 | + readHeaderTimeoutSeconds = flag.Int("read-header-timeout-secs", 2, "Number of seconds allowed to read request headers") |
| 36 | +) |
| 37 | + |
| 38 | +func main() { |
| 39 | + // Initialize klog |
| 40 | + klog.InitFlags(nil) |
| 41 | + defer klog.Flush() |
| 42 | + |
| 43 | + // Parse flags |
| 44 | + flag.Parse() |
| 45 | + |
| 46 | + // Update ciregistry configuration |
| 47 | + ciregistry.DataRoot = *dataRoot |
| 48 | + |
| 49 | + // Start the server |
| 50 | + server := initializeServer(*listenerAddr, *readHeaderTimeoutSeconds) |
| 51 | + |
| 52 | + // Graceful shutdown setup |
| 53 | + stop := make(chan os.Signal, 1) |
| 54 | + signal.Notify(stop, os.Interrupt, syscall.SIGTERM) |
| 55 | + |
| 56 | + go func() { |
| 57 | + klog.Infof("Starting server on %s", server.Addr) |
| 58 | + klog.Infof("API documentation available at http://localhost%s/docs", server.Addr) |
| 59 | + |
| 60 | + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 61 | + klog.Fatalf("Server failed: %v", err) |
| 62 | + } |
| 63 | + }() |
| 64 | + |
| 65 | + <-stop |
| 66 | + klog.Info("Shutting down server gracefully...") |
| 67 | + |
| 68 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 69 | + defer cancel() |
| 70 | + |
| 71 | + if err := server.Shutdown(ctx); err != nil { |
| 72 | + klog.Fatalf("Server forced to shutdown: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + klog.Info("Server gracefully stopped") |
| 76 | +} |
| 77 | + |
| 78 | +func initializeServer(addr string, readTimeoutSeconds int) *http.Server { |
| 79 | + handler := ciregistry.NewRouter() |
| 80 | + server := &http.Server{ |
| 81 | + Addr: addr, |
| 82 | + Handler: handler, |
| 83 | + ReadHeaderTimeout: time.Duration(readTimeoutSeconds) * time.Second, |
| 84 | + } |
| 85 | + |
| 86 | + return server |
| 87 | +} |
0 commit comments