main.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "os/signal"
  6. "syscall"
  7. "nettool/internal/config"
  8. "nettool/internal/deviceinfo"
  9. "nettool/internal/discovery"
  10. "nettool/internal/httpserver"
  11. "nettool/internal/logger"
  12. applyexecsvc "nettool/internal/network/applyexec"
  13. configreadersvc "nettool/internal/network/configreader"
  14. interfacesvc "nettool/internal/network/interfaces"
  15. netplansvc "nettool/internal/network/netplan"
  16. validatorsvc "nettool/internal/network/validator"
  17. "nettool/internal/systemaction"
  18. "nettool/internal/tasks"
  19. )
  20. func main() {
  21. ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
  22. defer stop()
  23. log := logger.New()
  24. cfg := config.Load(os.Args[1:])
  25. log.Info("server starting", "version", cfg.ServerVersion)
  26. if os.Geteuid() != 0 {
  27. log.Warn("server is not running as root; netplan write/apply and system actions will fail")
  28. }
  29. deviceSvc := deviceinfo.New(cfg)
  30. interfaceSvc := interfacesvc.New(cfg)
  31. configSvc := configreadersvc.New()
  32. validatorSvc := validatorsvc.New()
  33. netplanSvc := netplansvc.New()
  34. applySvc := applyexecsvc.New()
  35. taskSvc := tasks.New()
  36. systemSvc := systemaction.New()
  37. httpSrv := httpserver.New(cfg, log, deviceSvc, interfaceSvc, configSvc, validatorSvc, netplanSvc, applySvc, taskSvc, systemSvc)
  38. udpSrv := discovery.New(cfg, log, deviceSvc)
  39. errCh := make(chan error, 2)
  40. go func() { errCh <- httpSrv.Run(ctx) }()
  41. go func() { errCh <- udpSrv.Run(ctx) }()
  42. select {
  43. case <-ctx.Done():
  44. log.Info("server shutting down")
  45. case err := <-errCh:
  46. if err != nil {
  47. log.Error("server stopped with error", "error", err.Error())
  48. }
  49. }
  50. }