coredhcp.go.template 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. {{/* This file is the template source. The following comment obviously doesn't apply here */ -}}
  5. // This is a generated file, edits should be made in the corresponding source file
  6. // And this file regenerated using `coredhcp-generator -from core-plugins.txt`
  7. package main
  8. import (
  9. "flag"
  10. "fmt"
  11. "io/ioutil"
  12. "time"
  13. "github.com/coredhcp/coredhcp/config"
  14. "github.com/coredhcp/coredhcp/logger"
  15. "github.com/coredhcp/coredhcp/server"
  16. "github.com/coredhcp/coredhcp/plugins"
  17. {{- range $plugin := .}}
  18. {{- /* We import all plugins as pl_<pluginname> to avoid conflicts with reserved keywords */}}
  19. {{importname $plugin}} "{{$plugin}}"
  20. {{- end}}
  21. "github.com/sirupsen/logrus"
  22. )
  23. var (
  24. flagLogFile = flag.String("logfile", "", "Name of the log file to append to. Default: stdout/stderr only")
  25. flagLogNoStdout = flag.Bool("nostdout", false, "Disable logging to stdout/stderr")
  26. flagLogLevel = flag.String("loglevel", "info", fmt.Sprintf("Log level. One of %v", getLogLevels()))
  27. )
  28. var logLevels = map[string]func(*logrus.Logger){
  29. "none": func(l *logrus.Logger) { l.SetOutput(ioutil.Discard) },
  30. "debug": func(l *logrus.Logger) { l.SetLevel(logrus.DebugLevel) },
  31. "info": func(l *logrus.Logger) { l.SetLevel(logrus.InfoLevel) },
  32. "warning": func(l *logrus.Logger) { l.SetLevel(logrus.WarnLevel) },
  33. "error": func(l *logrus.Logger) { l.SetLevel(logrus.ErrorLevel) },
  34. "fatal": func(l *logrus.Logger) { l.SetLevel(logrus.FatalLevel) },
  35. }
  36. func getLogLevels() []string {
  37. var levels []string
  38. for k := range logLevels {
  39. levels = append(levels, k)
  40. }
  41. return levels
  42. }
  43. var desiredPlugins = []*plugins.Plugin{
  44. {{- range $plugin := .}}
  45. &{{importname $plugin}}.Plugin,
  46. {{- end}}
  47. }
  48. func main() {
  49. flag.Parse()
  50. log := logger.GetLogger("main")
  51. fn, ok := logLevels[*flagLogLevel]
  52. if !ok {
  53. log.Fatalf("Invalid log level '%s'. Valid log levels are %v", *flagLogLevel, getLogLevels())
  54. }
  55. fn(log.Logger)
  56. log.Infof("Setting log level to '%s'", *flagLogLevel)
  57. if *flagLogFile != "" {
  58. log.Infof("Logging to file %s", *flagLogFile)
  59. logger.WithFile(log, *flagLogFile)
  60. }
  61. if *flagLogNoStdout {
  62. log.Infof("Disabling logging to stdout/stderr")
  63. logger.WithNoStdOutErr(log)
  64. }
  65. config, err := config.Load()
  66. if err != nil {
  67. log.Fatalf("Failed to load configuration: %v", err)
  68. }
  69. // register plugins
  70. for _, plugin := range desiredPlugins {
  71. if err := plugins.RegisterPlugin(plugin); err != nil {
  72. log.Fatalf("Failed to register plugin '%s': %v", plugin.Name, err)
  73. }
  74. }
  75. // start server
  76. srv, err := server.Start(config)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. if err := srv.Wait(); err != nil {
  81. log.Print(err)
  82. }
  83. time.Sleep(time.Second)
  84. }