|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "os/signal" |
| 10 | + "sync" |
| 11 | + "syscall" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/dnstapir/observation-encoder/internal/api" |
| 15 | + "github.com/dnstapir/observation-encoder/internal/app" |
| 16 | + "github.com/dnstapir/observation-encoder/internal/cert" |
| 17 | + "github.com/dnstapir/observation-encoder/internal/common" |
| 18 | + "github.com/dnstapir/observation-encoder/internal/logger" |
| 19 | +) |
| 20 | + |
| 21 | +/* Rewritten if building with make */ |
| 22 | +var version = "BAD-BUILD" |
| 23 | +var commit = "BAD-BUILD" |
| 24 | + |
| 25 | +type conf struct { |
| 26 | + app.Conf |
| 27 | + Api api.Conf |
| 28 | + Cert cert.Conf `json:"cert"` |
| 29 | +} |
| 30 | + |
| 31 | +func main() { |
| 32 | + var configFile string |
| 33 | + var runVersionCmd bool |
| 34 | + var debugFlag bool |
| 35 | + var mainConf conf |
| 36 | + |
| 37 | + flag.BoolVar(&runVersionCmd, |
| 38 | + "version", |
| 39 | + false, |
| 40 | + "Print version then exit", |
| 41 | + ) |
| 42 | + flag.StringVar(&configFile, |
| 43 | + "config", |
| 44 | + "", |
| 45 | + "Configuration file to use", |
| 46 | + ) |
| 47 | + flag.BoolVar(&debugFlag, |
| 48 | + "debug", |
| 49 | + false, |
| 50 | + "Enable DEBUG logs", |
| 51 | + ) |
| 52 | + flag.Parse() |
| 53 | + |
| 54 | + log, err := logger.Create( |
| 55 | + logger.Conf{ |
| 56 | + Debug: debugFlag, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + panic(fmt.Sprintf("Could not create logger, err: '%s'", err)) |
| 60 | + } |
| 61 | + |
| 62 | + log.Info("observation-encoder version: '%s', commit: '%s'", version, commit) |
| 63 | + if runVersionCmd { |
| 64 | + os.Exit(0) |
| 65 | + } |
| 66 | + |
| 67 | + log.Debug("Debug logging enabled") |
| 68 | + |
| 69 | + if configFile == "" { |
| 70 | + log.Error("No config file specified, exiting...") |
| 71 | + os.Exit(-1) |
| 72 | + } |
| 73 | + |
| 74 | + file, err := os.Open(configFile) |
| 75 | + if err != nil { |
| 76 | + log.Error("Couldn't open config file '%s', exiting...", configFile) |
| 77 | + os.Exit(-1) |
| 78 | + } |
| 79 | + defer file.Close() |
| 80 | + |
| 81 | + confDecoder := json.NewDecoder(file) |
| 82 | + if confDecoder == nil { |
| 83 | + log.Error("Problem decoding config file '%s', exiting...", configFile) |
| 84 | + os.Exit(-1) |
| 85 | + } |
| 86 | + |
| 87 | + confDecoder.DisallowUnknownFields() |
| 88 | + confDecoder.Decode(&mainConf) |
| 89 | + file.Close() // TODO okay to close here while also using defer above? |
| 90 | + |
| 91 | + mainConf.Log = log |
| 92 | + appHandle, err := app.Create(mainConf.Conf) |
| 93 | + if err != nil { |
| 94 | + log.Error("Error creating application: '%s'", err) |
| 95 | + os.Exit(-1) |
| 96 | + } |
| 97 | + |
| 98 | + mainConf.Cert.Log = log |
| 99 | + certHandle, err := cert.Create(mainConf.Cert) |
| 100 | + if err != nil { |
| 101 | + log.Error("Error creating cert manager: '%s'", err) |
| 102 | + os.Exit(-1) |
| 103 | + } |
| 104 | + |
| 105 | + mainConf.Api.Log = log |
| 106 | + mainConf.Api.App = appHandle |
| 107 | + mainConf.Api.Certs = certHandle |
| 108 | + apiHandle, err := api.Create(mainConf.Api) |
| 109 | + if err != nil { |
| 110 | + log.Error("Error creating API: '%s'", err) |
| 111 | + os.Exit(-1) |
| 112 | + } |
| 113 | + |
| 114 | + sigChan := make(chan os.Signal, 1) |
| 115 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 116 | + defer close(sigChan) |
| 117 | + defer signal.Stop(sigChan) |
| 118 | + |
| 119 | + ctx, cancel := context.WithCancel(context.Background()) |
| 120 | + exitCh := make(chan common.Exit) |
| 121 | + |
| 122 | + go appHandle.Run(ctx, exitCh) |
| 123 | + |
| 124 | + go apiHandle.Run(ctx, exitCh) |
| 125 | + go certHandle.Run(ctx, exitCh) |
| 126 | + |
| 127 | + exitLoop := false |
| 128 | + var wg sync.WaitGroup |
| 129 | + wg.Add(1) |
| 130 | + for { |
| 131 | + select { |
| 132 | + case s, ok := <-sigChan: |
| 133 | + if ok { |
| 134 | + log.Info("Got signal '%s'", s) |
| 135 | + exitLoop = true |
| 136 | + } else { |
| 137 | + log.Info("signal channel was closed") |
| 138 | + sigChan = nil |
| 139 | + } |
| 140 | + case exit, ok := <-exitCh: |
| 141 | + if ok { |
| 142 | + if exit.Err != nil { |
| 143 | + log.Error("%s exited with error: '%s'", exit.ID, exit.Err) |
| 144 | + if exit.Err == common.ErrFatal { |
| 145 | + exitLoop = true |
| 146 | + } |
| 147 | + } else { |
| 148 | + log.Info("%s done!", exit.ID) |
| 149 | + } |
| 150 | + } else { |
| 151 | + log.Warning("exit channel closed unexpectedly") |
| 152 | + exitCh = nil |
| 153 | + } |
| 154 | + } |
| 155 | + if exitLoop || (sigChan == nil && exitCh == nil) { |
| 156 | + log.Info("Leaving toplevel loop") |
| 157 | + wg.Done() |
| 158 | + break |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + log.Info("Cancelling, giving threads some time to finish...") |
| 163 | + cancel() |
| 164 | + timeout := make(chan bool, 1) |
| 165 | + go func() { |
| 166 | + time.Sleep(2 * time.Second) |
| 167 | + timeout <- true |
| 168 | + }() |
| 169 | + |
| 170 | +TIMEOUT_LOOP: |
| 171 | + for { |
| 172 | + select { |
| 173 | + case exit, ok := <-exitCh: |
| 174 | + if ok { |
| 175 | + if exit.Err != nil { |
| 176 | + log.Error("%s exited with error: '%s'", exit.ID, exit.Err) |
| 177 | + } else { |
| 178 | + log.Info("%s done!", exit.ID) |
| 179 | + } |
| 180 | + } else { |
| 181 | + log.Info("exit channel was closed during shutdown") |
| 182 | + exitCh = nil |
| 183 | + } |
| 184 | + case <-timeout: |
| 185 | + log.Debug("Time's up. Proceeding with shutdown.") |
| 186 | + break TIMEOUT_LOOP |
| 187 | + } |
| 188 | + if exitCh == nil { |
| 189 | + log.Warning("exit channel closed unexpectedly") |
| 190 | + break TIMEOUT_LOOP |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + close(exitCh) |
| 195 | + wg.Wait() |
| 196 | + log.Info("Exiting...") |
| 197 | + os.Exit(0) |
| 198 | +} |
0 commit comments