|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "embed" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/signal" |
| 11 | + "syscall" |
| 12 | + "time" |
| 13 | + |
| 14 | + "ignite/handlers" |
| 15 | + "ignite/routes" |
| 16 | + "ignite/tftp" |
| 17 | +) |
| 18 | + |
| 19 | +// Application represents the main application with embedded static files |
| 20 | +type Application struct { |
| 21 | + container *Container |
| 22 | + httpServer *http.Server |
| 23 | + tftpServer *tftp.Server |
| 24 | + staticFS embed.FS |
| 25 | + staticHandler *handlers.StaticHandlers |
| 26 | +} |
| 27 | + |
| 28 | +// NewApplicationWithStatic creates a new application instance with embedded static files |
| 29 | +func NewApplicationWithStatic(staticFS embed.FS) (*Application, error) { |
| 30 | + container, err := NewContainer() |
| 31 | + if err != nil { |
| 32 | + return nil, fmt.Errorf("failed to create container: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + // Create static file handler |
| 36 | + staticHandler := handlers.NewStaticHandlers(staticFS, container.Config.HTTP.Dir) |
| 37 | + |
| 38 | + return &Application{ |
| 39 | + container: container, |
| 40 | + staticFS: staticFS, |
| 41 | + staticHandler: staticHandler, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +// Start starts all application services including static file serving |
| 46 | +func (a *Application) Start() error { |
| 47 | + // Start TFTP server |
| 48 | + a.tftpServer = tftp.NewServer(a.container.Config.TFTP.Dir) |
| 49 | + if err := a.tftpServer.Start(); err != nil { |
| 50 | + return fmt.Errorf("failed to start TFTP server: %w", err) |
| 51 | + } |
| 52 | + log.Printf("TFTP server started on port 69, serving from %s", a.container.Config.TFTP.Dir) |
| 53 | + |
| 54 | + // Setup HTTP handlers with dependency injection |
| 55 | + handlerContainer := &handlers.Container{ |
| 56 | + ServerService: a.container.ServerService, |
| 57 | + LeaseService: a.container.LeaseService, |
| 58 | + Config: a.container.Config, |
| 59 | + } |
| 60 | + |
| 61 | + // Create HTTP router with injected dependencies and static file handling |
| 62 | + router := routes.SetupWithContainerAndStatic(handlerContainer, a.staticHandler) |
| 63 | + log.Printf("Embedded HTTP server configured") |
| 64 | + |
| 65 | + // Create HTTP server |
| 66 | + a.httpServer = &http.Server{ |
| 67 | + Addr: ":" + a.container.Config.HTTP.Port, |
| 68 | + Handler: router, |
| 69 | + } |
| 70 | + |
| 71 | + // Start HTTP server |
| 72 | + go func() { |
| 73 | + log.Printf("HTTP API server started on port %s", a.container.Config.HTTP.Port) |
| 74 | + if err := a.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 75 | + log.Fatalf("HTTP server failed: %v", err) |
| 76 | + } |
| 77 | + }() |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// Rest of the Application methods remain the same... |
| 83 | +func (a *Application) Stop() error { |
| 84 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + if a.httpServer != nil { |
| 88 | + if err := a.httpServer.Shutdown(ctx); err != nil { |
| 89 | + log.Printf("Error shutting down HTTP server: %v", err) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if a.tftpServer != nil { |
| 94 | + a.tftpServer.Stop() |
| 95 | + } |
| 96 | + |
| 97 | + if err := a.container.Close(); err != nil { |
| 98 | + log.Printf("Error closing container: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func (a *Application) Run() error { |
| 105 | + if err := a.Start(); err != nil { |
| 106 | + return fmt.Errorf("failed to start application: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + quit := make(chan os.Signal, 1) |
| 110 | + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) |
| 111 | + |
| 112 | + log.Println("Application started. Press Ctrl+C to stop.") |
| 113 | + <-quit |
| 114 | + log.Println("Shutting down application...") |
| 115 | + |
| 116 | + if err := a.Stop(); err != nil { |
| 117 | + return fmt.Errorf("failed to stop application: %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + log.Println("Application stopped") |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// GetContainer returns the application's container for access to services |
| 125 | +func (a *Application) GetContainer() *handlers.Container { |
| 126 | + return &handlers.Container{ |
| 127 | + ServerService: a.container.ServerService, |
| 128 | + LeaseService: a.container.LeaseService, |
| 129 | + Config: a.container.Config, |
| 130 | + } |
| 131 | +} |
0 commit comments