coredhcp.go.template 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. package main
  5. import (
  6. "flag"
  7. "time"
  8. "github.com/coredhcp/coredhcp"
  9. "github.com/coredhcp/coredhcp/config"
  10. "github.com/coredhcp/coredhcp/logger"
  11. "github.com/coredhcp/coredhcp/plugins"
  12. {{range $plugin := .}}
  13. {{if eq $plugin "github.com/coredhcp/coredhcp/plugins/range"}}rangepl "{{$plugin}}"{{else}}"{{$plugin}}"{{end}}
  14. {{end}}
  15. "github.com/sirupsen/logrus"
  16. )
  17. var (
  18. flagLogFile = flag.String("logfile", "", "Name of the log file to append to. Default: stdout/stderr only")
  19. flagLogNoStdout = flag.Bool("nostdout", false, "Disable logging to stdout/stderr")
  20. flagDebug = flag.Bool("debug", false, "Enable debug output")
  21. )
  22. var desiredPlugins = []*plugins.Plugin{
  23. {{range $plugin := .}}
  24. {{$import := importname $plugin}}&{{if eq $import "range"}}rangepl{{else}}{{$import}}{{end}}.Plugin,
  25. {{end}}
  26. }
  27. func main() {
  28. flag.Parse()
  29. log := logger.GetLogger("main")
  30. if *flagDebug {
  31. log.Logger.SetLevel(logrus.DebugLevel)
  32. log.Infof("Enabled debug logging")
  33. }
  34. if *flagLogFile != "" {
  35. log.Infof("Logging to file %s", *flagLogFile)
  36. logger.WithFile(log, *flagLogFile)
  37. }
  38. if *flagLogNoStdout {
  39. log.Infof("Disabling logging to stdout/stderr")
  40. logger.WithNoStdOutErr(log)
  41. }
  42. config, err := config.Load()
  43. if err != nil {
  44. log.Fatal(err)
  45. }
  46. // register plugins
  47. for _, plugin := range desiredPlugins {
  48. if err := plugins.RegisterPlugin(plugin); err != nil {
  49. log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
  50. }
  51. }
  52. // start server
  53. server := coredhcp.NewServer(config)
  54. if err := server.Start(); err != nil {
  55. log.Fatal(err)
  56. }
  57. if err := server.Wait(); err != nil {
  58. log.Error(err)
  59. }
  60. time.Sleep(time.Second)
  61. }